SQL server組合查詢+sql常用函數(shù)有哪些?
文章主要描述的是SQL server組合查詢+sql常用函數(shù),其中包括字符串函數(shù),數(shù)學(xué)函數(shù)以及日期函數(shù)與系統(tǒng)函數(shù)這些函數(shù)的描述,在某些情況下,這些函數(shù)是經(jīng)常被用到的,如下情況此問題分解出來是這樣的:
有2個表,A和B它們通過一個id關(guān)聯(lián),都有一個時間列。要求列出B表時間列大于A表時間列5天的數(shù)據(jù),并且要求A表的id列的第9位不等于7,想了很久沒有解決,后來請教同事才做好:
- select B.id from B join A on A.id=B.id where substring(B.id,9,1)!='7' and dateadd(dy,5,A.time)<B.time
這些都用到sql的常用函數(shù),下面列出常用的函數(shù)以方便大家:
SQL server組合查詢+sql常用函數(shù)之1.字符串函數(shù)
長度與分析用
datalength(Char_expr) 返回字符串包含字符數(shù),但不包含后面的空格
substring(expression,start,length) 不多說了,取子串
right(char_expr,int_expr) 返回字符串右邊int_expr個字符
字符操作類
upper(char_expr) 轉(zhuǎn)為大寫
lower(char_expr) 轉(zhuǎn)為小寫
space(int_expr) 生成int_expr個空格
replicate(char_expr,int_expr)復(fù)制字符串int_expr次
reverse(char_expr) 反轉(zhuǎn)字符串
stuff(char_expr1,start,length,char_expr2) 將字符串char_expr1中的從
start開始的length個字符用char_expr2代替
ltrim(char_expr) rtrim(char_expr) 取掉空格
ascii(char) char(ascii) 兩函數(shù)對應(yīng),取ascii碼,根據(jù)ascii嗎取字符
字符串查找
charindex(char_expr,expression) 返回char_expr的起始位置
patindex("%pattern%",expression) 返回指定模式的起始位置,否則為0
SQL server組合查詢+sql常用函數(shù)之2.數(shù)學(xué)函數(shù)
abs(numeric_expr) 求絕對值
ceiling(numeric_expr) 取大于等于指定值的最小整數(shù)
exp(float_expr) 取指數(shù)
floor(numeric_expr) 小于等于指定值得***整數(shù)
pi() 3.1415926.........
power(numeric_expr,power) 返回power次方
rand([int_expr]) 隨機(jī)數(shù)產(chǎn)生器
round(numeric_expr,int_expr) 安int_expr規(guī)定的精度四舍五入
sign(int_expr) 根據(jù)正數(shù),0,負(fù)數(shù),,返回+1,0,-1
sqrt(float_expr) 平方根
SQL server組合查詢+sql常用函數(shù)之3.日期函數(shù)
getdate() 返回日期
datename(datepart,date_expr) 返回名稱如 June
datepart(datepart,date_expr) 取日期一部份
datediff(datepart,date_expr1.dateexpr2) 日期差
dateadd(datepart,number,date_expr) 返回日期加上 number
上述函數(shù)中datepart的
寫法 取值和意義
yy 1753-9999 年份
qq 1-4 刻
mm 1-12 月
dy 1-366 日
dd 1-31 日
wk 1-54 周
dw 1-7 周幾
hh 0-23 小時
mi 0-59 分鐘
ss 0-59 秒
ms 0-999 毫秒
日期轉(zhuǎn)換
convert()
SQL server組合查詢+sql常用函數(shù)之4.系統(tǒng)函數(shù)
suser_name() 用戶登錄名
user_name() 用戶在數(shù)據(jù)庫中的名字
user 用戶在數(shù)據(jù)庫中的名字
show_role() 對當(dāng)前用戶起作用的規(guī)則
db_name() 數(shù)據(jù)庫名
object_name(obj_id) 數(shù)據(jù)庫對象名
col_name(obj_id,col_id) 列名
col_length(objname,colname) 列長度
valid_name(char_expr) 是否是有效標(biāo)識符
以上的相關(guān)內(nèi)容就是對SQL server組合查詢+sql常用函數(shù)的介紹,望你能有所收獲。
【編輯推薦】