Sql常见函数大全
select?*?from?student;
?
--顯示字符[串]的方式
select?1;
select?'a';
select?'12345';
?
select?DISTINCT?*?from?student
where?name?<>?'liujiayu';
where?age?between?'20'?and?'30';
?
--聚合函數
select?count(*)?as?num?from?student;
select?count(*)?from?student;
select?max(age)?as?MaxOfAge?from?student;
select?min(age)?as?MinOfAge?from?student;
select?avg(age)?as?AvgOfAge?from?student;
?
?
?
--字符串函數
--ASCII(expr):返回expr的最左端的字符的ascii碼值,如果expr是純數字,如,則可以寫成ascii(),但如果不是純數字,如a1234,則必須加引號,如ascii('a1234');
select?ascii(1);
select?ascii('a1');
--CHAR(expr):將ascii碼轉換成字符,如果不存在ascii碼為expr的字符,則返回null;
select?char(97);
?
select?lower('WERTsdfghETcvb');
select?upper('WERTsdfghETcvb');
?
--STR(expr,[length,[decimal]]):把數值型數據轉換為字符型數據。length?指定返回的字符串的長度,decimal?指定返回的小數位數。如果沒有指定長度,缺省的length?值為,decimal?缺省值為。當length?或者decimal?為負值時,返回NULL;?當length?小于小數點左邊(包括符號位)的位數時,返回length?個*;?先服從length?,再取decimal?;?當返回的字符串位數小于length?,左邊補足空格。
select?str(1234.5678,6,3);
?
--去空格
select?ltrim('?tyu?');
select?ltrim('?tyu?');
?
--取子字符
select?left('123456789',2);
select?right('123456789',3);
select?substring('123456789',3,2);
?
--尋找字符串,返回字符串中某個指定的子串出現的開始位置。
--其中substring?_expression?是所要查找的字符表達式,expression?可為字符串也可為列名表達式。如果沒有發現子串,則返回值。
--此函數不能用于TEXT?和IMAGE?數據類型。
select?charindex('45','45fgf123456789');
select?charindex('45','45fgf123456789',2);
--其中子串表達式前后必須有百分號“%”否則返回值為。
--與CHARINDEX?函數不同的是,PATINDEX函數的子串中可以使用通配符,且此函數可用于CHAR、VARCHAR?和TEXT?數據類型。
select?patindex('%4_%','fgf123456789');
?
--重復一個指定次數的字符串。
select?replicate('1234',2);
--字符串逆置
select?reverse('12345167890');
--替換字符串
select?replace('12345167890','1','liujiayu');
--返回一個有指定長度的空白字符串。
select?space(3);
select?replace(space(3),'?','a');
--用另一子串替換字符串指定位置、長度的子串。
--STUFF?(<character_expression1>,<start_?position>,<length>,<character_expression2>)
select?stuff('1234567890',2,6,'aaaa');
?
--轉換格式
/*
CAST(expression?AS?data_type)
CONVERT(data_type,expression[,style])
convert一般在時間類型(datetime,smalldatetime)與字符串類型
*/
select?CAST(123?as?char(10));
select?CAST('123'?as?int);
SELECT?CAST('123.4'?AS?decimal(5,2));
Select?
CONVERT(varchar(30),getdate(),101);
/*
?1??101?mm/dd/yy
102?yy-mm-dd
103?dd/mm/yy
104?dd-mm-yy
105?dd-mm-yy
106?dd?mon?yy
107?mon?dd,yy
108?hh:mm:ss
or?109?mon?dd?yyyy?hh:mi:ss:mmmmAM(或PM)
110?mm-dd-yy
111?yy/mm/dd
112?yymmdd
or?113?dd?mon?yyyy?hh:mi:ss:mmm(24小時制)
114?hh:mi:ss:mmm(24小時制)
or?120?yyyy-mm-dd?hh:mi:ss(24小時制)
?21?or?121?yyyy-mm-dd?hh:mi:ss:mmm(24小時制)
*/
?
--時間日期函數
select?getdate();
--向后算,比如后天十幾號
select?dateadd(day,2,getdate());
select?dateadd(day,-10,getdate());
?
--計算差距
select?datediff(day,'2004-09-01','2004-09-18')
?
select?datepart(day,getdate())
select?datepart(month,getdate())
select?datepart(year,getdate())
?
select?datepart(hour,getdate())
select?datepart(minute,getdate())
select?datepart(second,getdate())
?
?
?
?
?
?
?
--數學函數
select?ceiling(56.57);--返回不小于n的最小整數
select?floor(56.57);--返回不大于n的最大整數
select?round(56.5776456,3);--:四舍五入,參數為小數的位數
select?rand();--隨機生成[0,1]之間的float類型值
select?sign(56.57);--當expr為整數、、負數時分別返回、、-1
select?sqrt(65);--返回expr的平方根
select?abs(-345);--返回expr的絕對值
select?power(3,2);--返回expr的n次方的值
?
?
?
?
--系統函數
?
select?suser_name();?--??用戶登錄名
select?user_name();?--??用戶在數據庫中的名字
select?user;?--??用戶登錄名
select?db_name();?--??數據庫名
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
- 上一篇: Sqlserver中char,nchar
- 下一篇: 深入sql server中的事务