數據庫常用函數的總結


--函數共有四個參數,其功能是將expression1_Str中自startIndex位置起刪除lengthInt個字符,
--然後將expression2插入到expression1_Str中的startIndex位置。
Stuff(expression1_Str,startIndex,lengthInt,expression2_Str)
select stuff('aaa',1,0,'1224777')
select stuff('444',2,2,'8888')


--返回近的整數(判斷總頁數經常會用到它)
select ceiling(13*1.0/12) -- 2
select ceiling (12.1) --13
select ceiling(12*1.0 /12)--1

--用指定的符號將前面的字符括起來
select quotename('aaa','{')--{aaa}
select quotename('456','<')--<456>

--去掉字符串右邊空格
select rtrim(' ss s  ')--' ss s'
select ltrim('      ww  ') --'ww  '
select ltrim(rtrim('  7896   s ')) --'7896   s'

--返回當前字符在後面字符串當中的下標
select charindex('s','458s')--4
select charindex('9','458s')--0

--前面的字符是否與後面的字符全匹配,如果匹配返回所以位置,
select patindex('ss','ssss');-- 0
select patindex('123','123') --1
select patindex('123ss','123') --0

--將小寫字母轉換爲大寫字母
select upper('sss')--SSS

--將大寫字母轉換爲小寫字母
select lower('AA雙') -- aa雙

--顛倒一組字符的順序
select reverse('abcew') -- wecba
select reverse('123456789')--987654321


--將指定的字符替換中特定的字符
select replace('uioop','o','2') --ui22p
select replace('uioop','','2') --uioop

--返回指定長度的空白字符串
select space(9) --'         ' 長度爲9空白字符
select space(3)--'   ' 長度爲3的空白字符

--數據類型轉換 cast(要進行轉換的值 as 具體數據類型)
select cast(122+15 as numeric(12,1)), convert(varchar(10),getdate(),120)
select cast('a'+'b'as varchar(1)) --a
select cast('a'+'b'as varchar) --ab

--得到日期的年份
select year(getdate()) -- 2009
--得到日期的月份
select month(getdate()) -- 7
--返回日期的天
select day('2006-11-05')  --5

--兩個日期之差有'年/月/日/時/分/秒'的差別
select datediff(year,'2004-02-18',getdate())--相差5年
select datediff(yy,'2004-02-18',getdate())--相差5年
--month m mm
select datediff(month,'2004-02-18',getdate())--相差65個月
select datediff(m,'2004-02-18',getdate())--相差65個月
select datediff(mm,'2004-02-18',getdate())--相差65個月
--day d y
select datediff(day,'2004-02-18',getdate())--相差1963天
select datediff(d,'2004-02-18',getdate())--相差1963天
select datediff(y,'2004-02-18',getdate())--相差1963天
--hour hh
select datediff(hour,'2004-02-08',getdate())--相差 47362 小時
select datediff(hh,'2004-02-08',getdate())--相差 47362 小時

-- minute mi
select datediff(minute,'2006-02-15 12:15:14','2006-02-15 12:28:24')--相差13分鐘
select datediff(mi,'2006-02-15 12:15:14','2006-02-15 12:28:24')--相差13分鐘

-- ss s
select datediff(ss,'2006-02-15 12:15:14','2006-02-15 12:16:24')--相差70秒
select datediff(s,'2006-02-15 12:15:14','2006-02-15 12:16:24')--相差70秒

select datediff(DAY,convert(datetime,'2006-02-05'),getdate())
SELECT DATEADD(yy,DATEDIFF(yy,0,getdate()),0) -- 2009-01-01 00:00:00.000 本年的第一天
select DATEDIFF(YY,1,GETDATE())--109

--替換指定的字符串 :
--expression1_Str 參數
--char1 被替換的字符 (該字符可以在expression1_Str當中出現,也可以不出現)
--char2 要替換 char1的字符
--replace(expression1_Str,char1,char2)
select replace(convert(varchar(12),getdate(),23),N'-0',N'-')--2009-7-4

--結合cast datename dateparet返回形式爲"2009年7月4日"的日期格式
select cast(cast(datename(yy,getdate()) as varchar)+'年'+cast(datepart(month,getdate()) as varchar)+'月'+cast(datename(day,getdate()) as varchar)+'日' as varchar) AS CurrDate

--判斷傳遞的參數是否爲正確的日期型
select isdate(getdate()) --1
select isdate('2006-29-15') --0
select isdate('2006-9-15') --1

--判斷參數是否爲NULL並進行處理,如果爲NULL返回後面的參數,如果不爲NULL則返回該參數
select isnull('123',1) -- 123
select isnull(null,1) --1

--判斷該參數是否數字 是返回 1 不是數字返回 0
select isnumeric('-1236.14569') --1
select isnumeric('-123ssss6.14569') --0

--判斷兩個字符串是否相等,相等返回NULL,不相等則返回第一個參數的值
select nullif('123','123456') --123
select nullif('123','123') -- null

select cast('13' as decimal)

--這些函數不能執行
select int('14')
select datetime(getdate())
select find('11111','4')

select APP_NAME()
 --找出表TableName當中字段ColName的長度
--執行此語句時必須在該表所屬 的數據庫當中執行並用字段和表名都要正確
select col_length('TableName','ColName')--6

--(datalength)返回字符串的長度包括所有空格
--(Len)返回字符串的長度不包括後面空格

select datalength('123')--3
select Len('123')--3

select datalength(' 123 ')--5
select Len(' 123 ')--4

select datalength(N'123 ')--8
select Len(N' 123 ')--4

select datalength(N'123')--6
select Len(N'123')--3

 


select datalength(N'456 ') -- 8


select Len('123 ')--3
select
select Len(N'123 ')--3

select host_id()
select host_name()

select identity(int ,1,1) AS SN , Name, sex,folk
into #Temp
from (
 select '王興如' As name,'男' As sex ,'漢族' AS folk
 union all
 select '劉淵源' As name,'女' As sex ,'土族' AS folk
 union
 select '馬秀良' As name,'女' As sex ,'苗族' AS folk
   ) As c

select * from  #Temp
drop table #Temp

select newid()

發佈了45 篇原創文章 · 獲贊 8 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章