A. 使用sql編寫一個函數
CREATE PROC Get_Sumdays
@YYMMDD1 datetime,@YYMMDD2 datetime
AS
BEGIN
Declare @Days int
select @Days=datediff(week,@YYMMDD1,@YYMMDD2) +(case when datepart(weekday,@YYMMDD1)>1 then 0 else 1 end)-(case when datepart(weekday,@YYMMDD2)>=1 then 0 else 1 end)
PRINT @Days
END
執行存儲過程:Get_Sumdays '2009-08-01','2009-08-30'
@YYMMDD1 是開始日期 @YYMMDD2是結束日期
B. 以我下面的SQL語句!關於函數怎麼寫
create proc mypr1
@pe int
As
select a.name name1,a.*,b.* from piwik_site a,piwik_archive_numeric_2013_10 b where a.idsite=b.idsite and b.name='nb_visits' and period=@pe order by b.value desc
C. 用sql寫函數,給定一個日期返回其為該年的第幾天
#include<stdio.h>int
main(void){
int
year,month,day;
int
day_of_year(int
year,int
month,int
day);
while(scanf("%d%d%d",&year,&month,&day)!=eof){
printf("%d",day_of_year(year,month,day));
}
return
0;}
int
day_of_year(int
year,int
month,int
day){
int
k,leap,d=day;
//d沒初始化
int
tab[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
//二維數組初始化不正確
leap=(year%4==0&&year%100!=0||year%400==0);
for(k=1;k<month;k++)
//邏輯錯誤,當前月只有day天,配合d=day,這里要去掉當前月
d=d+tab[leap][k];
return
d;}int
day_of_year2(int
year,int
month,int
day)
//稍微簡化一下{
int
k,leap,d=day;
int
tab[12]={31,28,31,30,31,30,31,31,30,31,30,31};
leap=(year%4==0&&year%100!=0||year%400==0);
for(k=0;k<month-1;k++)
d=d+tab[k];
if(leap==1&&month>2)d++;
//閏年並且輸入月份大於2,加1天
return
d;}
D. 自己編寫sql函數
create function [dbo].[Get_StrArrayStrOfIndex]
(
@str varchar(1024), --要分割的字元串
@split varchar(10), --分隔符號
@index int --取第幾個元素
)
returns varchar(1024)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int
set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)
set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =len(@str)+1
--這兒存在兩種情況:1、字元串不存在分隔符號 2、字元串中存在分隔符號,跳出while循環後,@location為0,那默認為字元串後邊有一個分隔符號。
return substring(@str,@start,@location-@start)
end
使用舉例:
select id,code=dbo.Get_StrArrayStrOfIndex(code,'.',4) from code_table
E. 在線等,求SQL自定義函數寫法
create function functionname
@part [類型],
@part [類型]
as
--[操作,計算]
return 1--[函數輸出值]
---只允許查數據和計算,不可以有增、刪、改的操作
select dbo.goods(商品信息.銷售價格,進貨單.進貨單價,'2011-1-1',GETDATE,())
from 商品信息 inner join 進貨單
on 商品信息.商品編號=進貨單.商品編號
F. SQL中如何調用一個函數啊
1、首先是找到標量函數,右鍵單擊選擇新建。
G. sql怎麼樣寫自定函數的格式例如newid()格式一樣
SQL裡面有個funcation
就是函數
create function
test(@Num
varchar(20))--@Num
參數,你這里應該就不用了,粗略看了下覺得是個隨機七星彩之類的玩意。
returns
varchar(50)
--返回值類型
as
begin
declare
@table
table
(
c1
varchar(10)
)
declare
@i
int
set
@i=0
while
(@i<=9)
begin
insert
into
@table
select
@i
set
@i=@i+1
end
declare
@s
varchar(10)
set
@s
=2
select
@s
=@s+c1
from
(select
top
7
c1
from
@table
order
by
newid())
T
select
@s
return
@s
end
--調用函數
select
dbo.test()--傳遞參數
而即使上因為你沒有穿入參數,存儲過程這些也能做這些事
H. sql中如何寫函數
/*
獲取兩個數值的大小
0:小於;1:大於;2:等於
*/
CREATE function GetMax(@qty1 varchar(50),@qty2 varchar(50))
returns int
begin
declare @Num int
if (convert(decimal(18,5),@qty1)-convert(decimal(18,5),@qty2) >0)
begin
set @Num=1
end
else if (convert(decimal(18,5),@qty1)-convert(decimal(18,5),@qty2) <0)
begin
set @Num=0
end
else
begin
set @Num=2
end
return @Num
end
I. 0的數字型函數sql怎麼寫
當COUNT(B)的結果為0時,恰好與第二個給定的參數0相等,這個時候NULLIF函數就會返回NULL,而SUM(A)在除以NULL時結果為NULL,外層使用ISNULL函數再對NULL值進行判斷,這樣最終結果就是0了。
SQL是一種具有數據操縱,數據定義等多種功能的資料庫語言。SQL具有交互性特點,能為用戶提供極大的便利,利用SQL語言,可以提高計算機應用系統的工作質量與效率。
結構化查詢語言簡稱SQL。結構化查詢語言是高級的非過程化編程語言,允許用戶在高層數據結構上工作。它不要求用戶指定對數據的存放方法,也不需要用戶了解具體的數據存放方式,所以具有完全不同底層結構的不同資料庫系統,可以使用相同的結構化查詢語言作為數據輸入與管理的介面。
J. sql函數的編寫格式是怎樣的
函數的格式如下:
如下函數名為inttochar(),使用時這樣:select dbo.inttochar(1234,10)
CREATE FUNCTION [dbo].[inttochar] (@int int=0, @length int=1)
RETURNS varchar(250) AS
--將@int轉換成長度為@length的字元串,長度不足時加前導0
BEGIN
declare @tmp varchar(250)
select @tmp=cast(@int as varchar(250))
while len(@tmp)<@length begin
select @tmp='0'+@tmp
end
return @tmp
END