一些有用的SQL Server函数
?
字符串函數
select substring('abcde',3,2)
select upper('abCD123')
select lower('abCD123')
select ltrim('? abc')
select rtrim('??? abc? ')
?
--將一個字符串插入到另一字符串。在插入新字符串時將從原來的字符串中刪除指定長度的字符
--stuff(string, insertion position, delete count, string inserted)
select stuff('abcdef',3,2,'1234')
?
--Replace(source, search, replace)
select replace('abcd','cd','fe')
?
時間函數
--獲取當前日期
select getdate()
--對日期進行減運算
select DateDiff(yy,'1984/5/20',GetDate())AS MarriedYears
--對日期進行加運算
select DateAdd(hh,100,GetDate()) AS [100HoursFromNow]
--返回datetime值的指定日期部分的整數值
select DatePart(DayofYear,GetDate())As DayCount
--從日期中獲取年,月,日
select day('03/12/1998') as [Day] ,month('03/12/1998') as [Month], year('03/12/1998') as [Year]
?
數學函數
--求絕對值
select abs(-1),abs(0),abs(1)
--求正弦,余弦
select sin(1),cos(1)
--求冪
select power(2,3) as cube
--ROUND(numeric_expression , length [,function]),返回數字表達式并四舍五入為指定的長度或精度
--當 length 為正數時,numeric_expression四舍五入為 length 所指定的小數位數。當length為負數時,--numeric_expression則按length所指定的在小數點的左邊四舍五入。?
select round(46.533,2),round(562.5,-1)
?
SELECT FLOOR(12.9273)
?
SELECT CEILING(12.9273)
--求平方根
select sqrt(6.25)
--求平方
select square(5.5)
?
轉換函數
select cast(getdate() as char(10))
select cast(1.5 as int)
?
--convert(data type, expression[style,])
select convert(char(10),getdate(),120)
?
Null函數
--ISNULL (<check_expression>, <replacement_value>),ISNULL() 函數將表達式中的NULL 值用指定值替換.
--如果check_expresssion 不是NULL, 則返回其原來的值,否則,返回replacement_value 的值.
select IsNull('NickName', 'none')
select IsNull(null,'none')
?
--NULLIF (<expression1>, <expression2>)
--它在expression1 與expression2 相等時返回NULL 值,若不相等時則返回expression1 的值.
select nullif('abc','sql')
select nullif('a','a')
?
--COALESCE()函數返回眾多表達式中第一個非NULL 表達式的值。如果所有的表達式均為NULL,則COALESCE() 函數返回NULL 值
select coalesce(null,'abe',null)
select coalesce('a',null)
轉載于:https://www.cnblogs.com/linsond/archive/2009/06/12/1502004.html
總結
以上是生活随笔為你收集整理的一些有用的SQL Server函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 建立在Windows XP欢迎屏幕中隐藏
- 下一篇: 初探SQL Server 2008商业智