1.字符串函數
長度與分析用
datalength(Char_expr) 返回字符串包含字符數,但不包含後面的空格
例:--重複字符串'01'五次
select replicate('01',5)
substring(expression,start,length) 不多說了,取子串
right(char_expr,int_expr) 返回字符串右邊int_expr個字符
字符操作類
upper(char_expr) 轉爲大寫
lower(char_expr) 轉爲小寫
space(int_expr) 生成int_expr個空格
replicate(char_expr,int_expr)複製字符串int_expr次
reverse(char_expr) 反轉字符串
例:--反轉字符串'HelloWorld'
select reverse('hello world')
stuff(char_expr1,start,length,char_expr2) 將字符串char_expr1中的從
例:
--替換字符串'helloworld'的1到3位爲'Res....'(Sql種字符索引從1開始)
select stuff('Hello world',1,3,'Result is ---->')
start開始的length個字符用char_expr2代替
ltrim(char_expr) rtrim(char_expr) 取掉空格
ascii(char) char(ascii) 兩函數對應,取ascii碼,根據ascii嗎取字符
例:--轉換字符或字符串的第一個字符成ascii(中文無效)
select ascii('2')
--轉換0-256數字到char
select char(50)
字符串查找
charindex(char_expr,expression) 返回char_expr的起始位置
patindex("%pattern%",expression) 返回指定模式的起始位置,否則爲0
2.數學函數
abs(numeric_expr) 求絕對值
ceiling(numeric_expr) 取大於等於指定值的最小整數
exp(float_expr) 取指數
floor(numeric_expr) 小於等於指定值得最大整數
pi() 3.1415926.........
power(numeric_expr,power) 返回power次方
rand([int_expr]) 隨機數產生器
round(numeric_expr,int_expr) 安int_expr規定的精度四捨五入
sign(int_expr) 根據正數,0,負數,,返回+1,0,-1
sqrt(float_expr) 平方根
3.日期函數
getdate() 返回日期
datename(datepart,date_expr) 返回名稱如 June
datepart(datepart,date_expr) 取日期一部份
datediff(datepart,date_expr1.dateexpr2) 日期差
參數:datepart 是規定了應在日期的哪一部分計算差額的參數。下表列出了 Microsoft? SQL Server? 識別的日期部分和縮寫。
日期部分 縮寫
year yy, yyyy
quarter qq, q
Month mm, m
dayofyear dy, y
Day dd, d
Week wk, ww
Hour hh
minute mi, n
second ss, s
millisecond ms
例:select datediff(minute,'2007-07-11','2007-07-12')
dateadd(datepart,number,date_expr) 返回日期加上 number
日期轉換
convert()
例:格式化事件爲yyyy-MM-dd
Select Substring(Convert(Nvarchar,Getdate(),120),1,10)
select convert(varchar(10),getdate(),120)
cast()
例:
cast('20070711' as datetime)
4.系統函數
suser_name() 用戶登錄名
user_name() 用戶在數據庫中的名字
user 用戶在數據庫中的名字
show_role() 對當前用戶起作用的規則
db_name() 數據庫名
object_name(obj_id) 數據庫對象名
col_name(obj_id,col_id) 列名
col_length(objname,colname) 列長度
valid_name(char_expr) 是否是有效標識符