1、datediff函數(shù)語(yǔ)法用法:
創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),雁江網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:雁江等地區(qū)。雁江做網(wǎng)站價(jià)格咨詢:13518219792
表達(dá)式DateDiff(timeinterval,date1,date2 [, firstdayofweek [, firstweekofyear]])
允許數(shù)據(jù)類型: timeinterval 表示相隔時(shí)間的類型,代碼為:年份 yy、yyyy 季度 qq、q;月份 mm、m;每年的某一日 dy、y;日期 dd、d;星期 wk、ww;工作日 dw;小時(shí) hh;分鐘 mi、n;秒 ss、s;毫秒 ms。
2、datediff函數(shù)用法示例:
%
fromDate = #9/9/00#
toDate = #1/1/2000#
response.write "There are " _
DateDiff("d",fromDate,toDate) _
" days to millenium from 9/9/00."
%
返回結(jié)果: 從9/9/00 到2000年還有 150 天。
sql server 自定義函數(shù)的使用
自定義函數(shù)
用戶定義自定義函數(shù)像內(nèi)置函數(shù)一樣返回標(biāo)量值,也可以將結(jié)果集用表格變量返回
用戶自定義函數(shù)的類型:
標(biāo)量函數(shù):返回一個(gè)標(biāo)量值
表格值函數(shù){內(nèi)聯(lián)表格值函數(shù)、多表格值函數(shù)}:返回行集(即返回多個(gè)值)
1、標(biāo)量函數(shù)
Create function 函數(shù)名(參數(shù))
Returns 返回值數(shù)據(jù)類型
[with {Encryption | Schemabinding }]
[as]
begin
SQL語(yǔ)句(必須有return 變量或值)
End
Schemabinding :將函數(shù)綁定到它引用的對(duì)象上(注:函數(shù)一旦綁定,則不能刪除、修改,除非刪除綁定)
Create function AvgResult(@scode varchar(10))
Returns real
As
Begin
Declare @avg real
Declare @code varchar(11)
Set @code=@scode + ‘%’
Select @avg=avg(result) from LearnResult_baijiali
Where scode like @code
Return @avg
End
執(zhí)行用戶自定義函數(shù)
select 用戶名。函數(shù)名 as 字段別名
select dbo.AvgResult(‘s0002’) as result
用戶自定義函數(shù)返回值可放到局部變量中,用set ,select,exec賦值
declare @avg1 real ,@avg2 real ,@avg3 real
select @avg1= dbo.AvgResult(‘s0002’)
set @avg2= dbo.AvgResult(‘s0002’)
exec @avg3= dbo.AvgResult ‘s0002’
select @avg1 as avg1 ,@avg2 as avg2 ,@avg3 as avg3
函數(shù)引用
create function code(@scode varchar(10))
returns varchar(10)
as
begin
declare @ccode varchar(10)
set @scode = @scode + ‘%’
select @ccode=ccode from cmessage
where ccode like @scode
return @ccode
end
select name from class where ccode = dbo.code(‘c001’)
2、表格值函數(shù)
a、 內(nèi)聯(lián)表格值函數(shù)
格式:
create function 函數(shù)名(參數(shù))
returns table
[with {Encryption | Schemabinding }]
as
return(一條SQL語(yǔ)句)
create function tabcmess(@code varchar(10))
returns table
as
return(select ccode,scode from cmessage where ccode like @ccode)
b、 多句表格值函數(shù)
create function 函數(shù)名(參數(shù))
returns 表格變量名table (表格變量定義)
[with {Encryption | Schemabinding }]
as
begin
SQL語(yǔ)句
end
多句表格值函數(shù)包含多條SQL語(yǔ)句,至少有一條在表格變量中填上數(shù)據(jù)值
表格變量格式
returns @變量名 table (column 定義| 約束定義 [,…])
對(duì)表格變量中的行可執(zhí)行select,insert,update,delete , 但select into 和 insert 語(yǔ)句的結(jié)果集是從存儲(chǔ)過程插入。
Create function tabcmessalot (@code varchar(10))
Returns @ctable table(code varchar(10) null,cname varchar(100) null)
As
Begin
Insert @ctable
Select ccode,explain from cmessage
Where scode like @code
return
End
Select * from tabcmessalot(‘s0003’)
來自:
C#調(diào)用SQL自定義函數(shù)返回值
代碼
1 --SQL自定義函數(shù):
2
3 CREATE FUNCTION [GetProjectID] (@headStr nvarchar(10),@date datetime)
4 )
5
6 RETURNS NVARCHAR(200)
7
8 AS
9
10 BEGIN
11
12 --不能在自定義函數(shù)中用INSERT INTO
13
14 --insert into emos_cust(cust_name,dates)values(
15
16 --@headStr,@date
17
18 --)
19
20 return 'TEST BY HANSHU'
21 END
代碼
1 /// summary
2 /// 獲取項(xiàng)目文件編號(hào) 涂聚文
3 /// /summary
4 private void FileNo()
5 {
6
7 SqlConnection conn = new SqlConnection(connectionString);
8 string strSql = "GetProjectID"; //自定SQL函數(shù)
9 SqlCommand cmd = new SqlCommand(strSql, conn);
10 cmd.CommandType = CommandType.StoredProcedure;
11 cmd.Parameters.Add("@headStr", SqlDbType.NVarChar).Value = "ZQ3"; //輸入?yún)?shù)
12 cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = System.DateTime.Now.ToShortDateString(); //輸入?yún)?shù)
13 cmd.Parameters.Add("@returnString", SqlDbType.NVarChar);
14 cmd.Parameters["@returnString"].Direction = ParameterDirection.ReturnValue; //返回參數(shù)
15 try
16 {
17 conn.Open();
18 object o= cmd.ExecuteScalar();
19
20 this.txtAFileNO.Text = cmd.Parameters["@returnString"].Value.ToString();
21
22 //Response.Write("");
23
24 }
25 catch (Exception ex)
26 {
27
28 this.txtAFileNO.Text = ex.Message;
29
30 }
31 finally
32 {
33
34 if (!(conn.State == ConnectionState.Closed))
35 {
36
37 conn.Close();
38
39
40 }
41
42 }
43
44
45 }
返回單個(gè)值的函數(shù), 可以當(dāng)變量用, 比如 select dbo.函數(shù)名(參數(shù)1, 參數(shù)2, ...) from ...where abc = dbo.函數(shù)名(參數(shù)1, 參數(shù)2...)
返回表的函數(shù), 可以當(dāng)數(shù)據(jù)表來使用, 比如 select * from dbo.函數(shù)名(參數(shù)1, 參數(shù)2, ...)
表值函數(shù)這樣寫(需要先定義一個(gè)表,然后向定義的表中插入記錄即可)
CREATE function dbo.GetPersonTable(@personID int, @needSelf int)
returns @table table(fielda int,fieldb varchar(10))--定義返回表結(jié)構(gòu),要與你后面查詢中字段一致
begin
if @needSelf = 0
insert into @table SELECT a.* from dbo.v_Person as a
inner join dbo.m_SysUser_Popedom as b on a.i_deptid=b.deptid and b.userid = @personID
else
insert into @table
SELECT a.* from dbo.v_Person as a inner join dbo.m_SysUser_Popedom as b on a.i_deptid=b.deptid and b.userid = @personID
union
select c.* from dbo.v_Person as c where c.personid = @personid
return
end
go
sqlserver中和java中indexof類似的函數(shù)是字符串函數(shù)中的charindex。
CHARINDEX函數(shù)返回字符或者字符串在另一個(gè)字符串中的起始位置。
CHARINDEX函數(shù)調(diào)用方法如下:
CHARINDEX ( expression1 , expression2 [ , start_location ] )
------------------
一個(gè)修改字符串的sql語(yǔ)句用到了charindex,substring等函數(shù)的綜合使用
update [UpdateString]
set b=
substring(b,0,charindex(',',b,0))+','+
cast(cast(substring(substring(b,charindex(',',b,0)+1,len(b)),
0,charindex(',',substring(b,charindex(',',b,0)+1,len(b)),0)) as int)+2 as varchar)+','+
substring(substring(b,charindex(',',b,0)+1,len(b)),
charindex(',',substring(b,charindex(',',b,0)+1,len(b)),0)+1,
len(substring(b,charindex(',',b,0)+1,len(b))))