Sql常见函数大全
select?*?from?student;
?
--顯示字符[串]的方式
select?1;
select?'a';
select?'12345';
?
select?DISTINCT?*?from?student
where?name?<>?'liujiayu';
where?age?between?'20'?and?'30';
?
--聚合函數(shù)
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;
?
?
?
--字符串函數(shù)
--ASCII(expr):返回expr的最左端的字符的ascii碼值,如果expr是純數(shù)字,如,則可以寫成ascii(),但如果不是純數(shù)字,如a1234,則必須加引號(hào),如ascii('a1234');
select?ascii(1);
select?ascii('a1');
--CHAR(expr):將ascii碼轉(zhuǎn)換成字符,如果不存在ascii碼為expr的字符,則返回null;
select?char(97);
?
select?lower('WERTsdfghETcvb');
select?upper('WERTsdfghETcvb');
?
--STR(expr,[length,[decimal]]):把數(shù)值型數(shù)據(jù)轉(zhuǎn)換為字符型數(shù)據(jù)。length?指定返回的字符串的長(zhǎng)度,decimal?指定返回的小數(shù)位數(shù)。如果沒有指定長(zhǎng)度,缺省的length?值為,decimal?缺省值為。當(dāng)length?或者decimal?為負(fù)值時(shí),返回NULL;?當(dāng)length?小于小數(shù)點(diǎn)左邊(包括符號(hào)位)的位數(shù)時(shí),返回length?個(gè)*;?先服從length?,再取decimal?;?當(dāng)返回的字符串位數(shù)小于length?,左邊補(bǔ)足空格。
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);
?
--尋找字符串,返回字符串中某個(gè)指定的子串出現(xiàn)的開始位置。
--其中substring?_expression?是所要查找的字符表達(dá)式,expression?可為字符串也可為列名表達(dá)式。如果沒有發(fā)現(xiàn)子串,則返回值。
--此函數(shù)不能用于TEXT?和IMAGE?數(shù)據(jù)類型。
select?charindex('45','45fgf123456789');
select?charindex('45','45fgf123456789',2);
--其中子串表達(dá)式前后必須有百分號(hào)“%”否則返回值為。
--與CHARINDEX?函數(shù)不同的是,PATINDEX函數(shù)的子串中可以使用通配符,且此函數(shù)可用于CHAR、VARCHAR?和TEXT?數(shù)據(jù)類型。
select?patindex('%4_%','fgf123456789');
?
--重復(fù)一個(gè)指定次數(shù)的字符串。
select?replicate('1234',2);
--字符串逆置
select?reverse('12345167890');
--替換字符串
select?replace('12345167890','1','liujiayu');
--返回一個(gè)有指定長(zhǎng)度的空白字符串。
select?space(3);
select?replace(space(3),'?','a');
--用另一子串替換字符串指定位置、長(zhǎng)度的子串。
--STUFF?(<character_expression1>,<start_?position>,<length>,<character_expression2>)
select?stuff('1234567890',2,6,'aaaa');
?
--轉(zhuǎn)換格式
/*
CAST(expression?AS?data_type)
CONVERT(data_type,expression[,style])
convert一般在時(shí)間類型(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小時(shí)制)
114?hh:mi:ss:mmm(24小時(shí)制)
or?120?yyyy-mm-dd?hh:mi:ss(24小時(shí)制)
?21?or?121?yyyy-mm-dd?hh:mi:ss:mmm(24小時(shí)制)
*/
?
--時(shí)間日期函數(shù)
select?getdate();
--向后算,比如后天十幾號(hào)
select?dateadd(day,2,getdate());
select?dateadd(day,-10,getdate());
?
--計(jì)算差距
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())
?
?
?
?
?
?
?
--數(shù)學(xué)函數(shù)
select?ceiling(56.57);--返回不小于n的最小整數(shù)
select?floor(56.57);--返回不大于n的最大整數(shù)
select?round(56.5776456,3);--:四舍五入,參數(shù)為小數(shù)的位數(shù)
select?rand();--隨機(jī)生成[0,1]之間的float類型值
select?sign(56.57);--當(dāng)expr為整數(shù)、、負(fù)數(shù)時(shí)分別返回、、-1
select?sqrt(65);--返回expr的平方根
select?abs(-345);--返回expr的絕對(duì)值
select?power(3,2);--返回expr的n次方的值
?
?
?
?
--系統(tǒng)函數(shù)
?
select?suser_name();?--??用戶登錄名
select?user_name();?--??用戶在數(shù)據(jù)庫(kù)中的名字
select?user;?--??用戶登錄名
select?db_name();?--??數(shù)據(jù)庫(kù)名
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結(jié)
- 上一篇: Sqlserver中char,nchar
- 下一篇: SQLServer权限