sybase函數(shù)匯總
來源:程序員人生 發(fā)布時(shí)間:2016-09-30 10:58:36 閱讀次數(shù):3534次
datalength(char_expr)
在char_expr中返回字符的長(zhǎng)度值,疏忽尾空
char_length
返回表達(dá)式中字符的數(shù)量。對(duì)可變長(zhǎng)度的列和變量,它將返回字符的個(gè)數(shù)(包括尾部的空格);對(duì)文字和固定長(zhǎng)度的字符列和變量,就是聲明的長(zhǎng)度。所以1般我們要求字符串的長(zhǎng)度,最后不要用這個(gè)。
len 返回指定字符串表達(dá)式(不包括尾隨空白)的字符數(shù)(而不是字節(jié)數(shù))
len(string_expression)
例如:select len(‘中國’) 結(jié)果:2
注意: char_length,char,len之間的區(qū)分
substring(expression,start,length)
返回部份字符串
str_replace 將第1個(gè)字符串表達(dá)式中出現(xiàn)的第2個(gè)字符串表達(dá)式的所有實(shí)例替換為第3個(gè)表達(dá)式。
例如:select str_replace(‘a(chǎn)aabbbccc’,’bb’,’mm’) 結(jié)果:aaammbccc
compare 直接比較兩個(gè)字符串,如果不1樣,則返回⑴
right(char_expr,int_expr)
返回char_expr右側(cè)的int_expr字符
left(char_expr,int_expr)
返回char_expr左側(cè)的int_expr字符
upper(char_expr)
把char_expr轉(zhuǎn)換成大寫情勢(shì)
lower(char_expr)
把char_expr轉(zhuǎn)換成小寫情勢(shì)
space(int_expr)
生成有int_expr個(gè)空格的字符串
replicate(char_expr,int_expr)
重復(fù)char_expr,int_expr次
stuff(expr1,start,length,expr2)
用expr2代替epxr1中start起始長(zhǎng)為length的字符串
reverse(char_expr)
反寫char_expr中的文本
ltrim(char_expr)
刪除頭空
rtrim(char_expr)
刪除尾空
ascii(char_expr)
返回char_expr中第1個(gè)字符的ASCII值
char(int_expr)
把ASCII碼轉(zhuǎn)換為字符
str(float_expr[,length[,decimal]])
進(jìn)行數(shù)值型到字符型轉(zhuǎn)換
soundex(char_expr)
返回char_expr的soundex值
difference(char_expr1,char_expr2)
返回表達(dá)式soundex值之差
charindex(char_expr,expression)
返回指定char_expr的開始位置,否則為0
patindex("%pattern%",expression)
返回指定樣式的開始位置,否則為0
注:datalength用于肯定可變字符串的長(zhǎng)度
soundex用于肯定字符串是不是發(fā)音相似
difference返回0⑷之間的值,0表示最不相似,4表示最相似
通配符
% 匹配任何數(shù)量的字符或無字符
_ 匹配任何單個(gè)字符(空間占位符)
[] 規(guī)定有效范圍,或某個(gè)"OR"條件
[ABG] A,B,G
[A-C] A,B,C
[A-CE-G] A,B,C,E,F,G
[^ABG] 除A,B,G
[^A-C] 除A,B,C
escape子句
用某個(gè)轉(zhuǎn)義字符可在搜索字符串時(shí)將通配符作為文字來包括。
ANSI⑻9 SQL 標(biāo)準(zhǔn)定義了escape子句指定某個(gè)轉(zhuǎn)義字符
缺省情況下,[]來轉(zhuǎn)義某個(gè)通配符,例:
select * from test_tab
where description like "%20[%]%"
語法:
like char_expression escape escape_character
例
select * from test_tab
where description like "%20#%%" escape "#"
+ 可用于串接字符
select au_laname+","+au_fname from authors
數(shù)學(xué)函數(shù)
abs(numeric_expr)
返回指定值的絕對(duì)值
ceiling(numeric_expr)
返回大于或等于指定值的最小整數(shù)
exp(float_expr)
給出指定值的指數(shù)值
floor(numeric_expr)
返回小于或等于指定值的最大整數(shù)
pi()
返回常數(shù)3.1415926
power(numeric_expr,power)
返回numeric_expr的值給power的冪
square 返回表示為float的指定值的平方值。
Square(numeric)
例如:select square(price) from titles
Select square(5) 結(jié)果:25.0
注意:次函數(shù)同等于power(muneric_expr,2),但是它返回float類型而不返回int類型。
rand([int_expr])
返回0⑴之間的隨機(jī)浮點(diǎn)數(shù),可指定基值
round(numeric_expr,int_expr)
把數(shù)值表達(dá)式圓整到int_expr指定的精度
sign(int_expr)
返回正+1,零0或負(fù)⑴
sqrt(float_expr)
返回指定值的平方根
SQL SERVER 支持所有標(biāo)準(zhǔn)的3角函數(shù)和其他有用的函數(shù)
日期函數(shù)
getdate()
返回當(dāng)前的系統(tǒng)日期和時(shí)間
current_date() 返回當(dāng)前日期(不包括小時(shí))
current_time() 返回當(dāng)前時(shí)間
datename(datepart,date_expr)
以字符串情勢(shì)返回date_expr指定部份的值,轉(zhuǎn)換成適合的名字
eg:Select datename(month,getdate()) 結(jié)果November
datepart(datepart,date_expr)
作為整數(shù)返回date_expr值的指定部份
datediff(datepart,date_expr1,date_expr2)
返回date_expr2-date_expr1,通過指定的datepart度量
dateadd(datepart,number,date_expr)
返回日期,通過在date_expr上增加指定number的日期部件而產(chǎn)生的
day 返回指定日期的datepart中表示天的整數(shù)
Select day(‘11/02/03’) 結(jié)果 2
month 返回1個(gè)整數(shù),該整數(shù)表示月份。
例如:select month(getdate()) 結(jié)果:12
同理: select day(getdate()) 結(jié)果:15
select year(getdate()) 結(jié)果:2006
tips:datepart
日期部件 縮寫 值范圍
年 yy 1753⑼999
季度 qq 1⑷
月 mm 1⑴2
每一年中的天 dy 1⑶66
天 dd 1⑶1
星期 wk 1⑸4
星期天 dw 1⑺(1=sunday)
小時(shí) hh 0⑵3
分鐘 mi 0⑸9
秒 ss 0⑸9
毫秒 ms 0⑼99
例:
select invoice_no,
datediff(dd,date_shipped,getdate())
from invoices
where balance_due>0
轉(zhuǎn)換函數(shù)
convert
此函數(shù)把值從1種類型改變成另外一種類型
convert(datetype [(length)],expression)
select "Advance="+convert(char(12),advance)
from titles
日期轉(zhuǎn)換
convert(datetype[(length)],expression,format)
format指定將日期轉(zhuǎn)換為何格式,有以下值:
沒有世紀(jì) 有世紀(jì) 轉(zhuǎn)換字符串中日期格式
tips: 0 or 100 mon dd yyy hh:miAM(or PM)
1 101 mm/dd/yy
2 102 yy.mm.dd
3 103 dd/mm/yy
4 104 dd.mm.yy
5 105 dd-mm-yy
6 106 dd mon yy
7 107 mon dd,yy
8 108 hh:mm:ss
9 or 109 mon dd,yyyy hh:mi:ss:mmmAM(or PM)
10 110 mm-dd-yy
11 111 yy/mm/dd
12 112 yymmdd
系統(tǒng)函數(shù)
函數(shù) 定義
訪問和安全性信息
host_id() 客戶進(jìn)程確當(dāng)前主機(jī)進(jìn)程ID號(hào)
host_name() 客戶進(jìn)程確當(dāng)前主計(jì)算機(jī)名
suser_id(["login_name"]) 用戶的SQL Server ID號(hào)
suser_name([server_user_id]) 用戶的SQL Server登錄名
user_id(["name_in_db"]) 用戶在數(shù)據(jù)庫 中的ID號(hào)
user_name([user_id]) 用戶在數(shù)據(jù)庫中的名字
user 用戶在數(shù)據(jù)庫中的名字
show_role() 用戶確當(dāng)前活動(dòng)角色
數(shù)據(jù)庫和對(duì)象信息
db_id(["db_name"]) 數(shù)據(jù)庫ID號(hào)
db_name([db_id]) 數(shù)據(jù)庫名
object_id("objname") 數(shù)據(jù)庫對(duì)象ID號(hào)
object_name(obj_id]) 數(shù)據(jù)庫對(duì)象號(hào)
col_name(obj_id,col_id) 對(duì)象的欄名
col_length("objname","colname") 欄的長(zhǎng)度
index_col("objname",index_id,key#) 已索引的欄名
valid_name(char_expr) 若char_expr不是有效標(biāo)識(shí)符,則返回0
數(shù)據(jù)函數(shù)
datalength(expression) 按字節(jié)返回expression的長(zhǎng)度
tsequal(timestamp1,timestamp2) 比較時(shí)戳值,若時(shí)戳值不匹配,則返回出錯(cuò)消息
isnull()
isnull函數(shù)用指定的值代替查詢欄或合計(jì)中的空值
例:
select avg(isnull(total_order,$0))
from invoices
col_length 返回已定義的列的長(zhǎng)度。如:返回表tablename 的title列的長(zhǎng)度,selectcol_length(‘tablename’,’title’)
注意:對(duì)text和image,numeric列比較特殊。
col_name 返回已指定表
ID 和列 ID 的列的名稱,且最長(zhǎng)可為 255
個(gè)字節(jié)。
語法 col_name(object_id,
column_id [, database_id])
參數(shù) object_id
是1個(gè)數(shù)值表達(dá)式,它是表、視圖或其它數(shù)據(jù)庫對(duì)象的對(duì)象 ID。它們 存儲(chǔ)在
sysobjects 的 id 列中
column_id是1個(gè)數(shù)值表達(dá)式,它表示1列的列
ID。它們存儲(chǔ)在 syscolumns 的colid 列中。
database_id是1個(gè)數(shù)值表達(dá)式,它表示數(shù)據(jù)庫的
ID。它們存儲(chǔ)在 sysdatabase 的db_id
列中。
curunreservedpas 返回指定磁盤區(qū)段中的可用頁數(shù)
Curunreservedpgs(dbid,istart,unreservedpgs)
dbid是數(shù)據(jù)庫的ID,它們存儲(chǔ)在sysdatabases的dbid字段。
Istart是要返回的頁所在磁盤區(qū)段中的1頁。
Unreservedpgs是在dbtable當(dāng)前對(duì)所要求的數(shù)據(jù)庫不可用時(shí)返回的缺省值。
eg: 返回數(shù)據(jù)庫名稱、裝備名和每一個(gè)裝備區(qū)段中的未保存頁數(shù)。
select db_name(dbid),d.name,curunreservedpgs(dbid,1,unreservedpgs)
from sysusages u ,sysdevices d where d.low<=u.size+vstart
and d.high>=u.size+vstart - 1
and d.status &2 = 2
db_id 返回數(shù)據(jù)庫的ID號(hào)
Select db_id(‘pub’) 返回pub數(shù)據(jù)庫的id
Select db_id() 當(dāng)前數(shù)據(jù)庫的ID
identity_burn_max 跟蹤給定表的identity burn最大值,此函數(shù)只返還只而不進(jìn)行更新。
例如:select identity_burn_max(‘tablename’)
注意:返回的是自增列的最大ID,如果這個(gè)表沒有自增列,則返回為null
object_id 返回指定對(duì)象的對(duì)象ID,object_name是返回對(duì)象ID的對(duì)象名稱。
Obeject_di(object_name)
object_name是數(shù)據(jù)庫對(duì)象(表、視圖、進(jìn)程、觸發(fā)器、缺省值或規(guī)則)的名稱。
例如:select object_id(‘TBL_ZONE’) 結(jié)果:437573566
一樣 selectobject_name(437573566) 結(jié)果:TBL_ZONE
注意:對(duì)象ID存儲(chǔ)在sysobjects的ID列中。
index_col 返回指定表或視圖中帶索引的列的名稱。
index_clolrder 返回列的順序
is_sec_service_on 安全服務(wù)啟用時(shí)返回1;否則返回0
is_sec_service_on(sevurity_service_nm)
其中:sevurity_service_nm是安全服務(wù)的名稱。
查找安全服務(wù)的有效名稱,用select * from syssecmechs
例如:select is_sec_service_on(“unifiedlogin”)
mut_excl_roles 返回有關(guān)兩個(gè)角色之間互斥性的信息。
mut_excl_roles是1個(gè)系統(tǒng)函數(shù)。如果系統(tǒng)安全員將role1定義為與role2互斥的角色,或直接有role2所包括的角色,則mut_excl_roles返回1,如果不是則返回0。
newid 根據(jù)提供的參數(shù)生成兩種不同格式的、人工可讀的全局唯1ID
Newid([optionflag])
例如:select newid()
select newid(0)
select newid(0x0)
select newid(1)
可以通過newid()來參數(shù)隨機(jī)器數(shù)
select id from tablename order by newid()
這時(shí)候你會(huì)發(fā)現(xiàn),每次履行,排序都不1樣。
next_identity 檢查下1個(gè)insert可用的下1個(gè)標(biāo)識(shí)值。(就是下1個(gè)自增的ID)
Next_identity(tablename)
例如:select next_identity(‘tbl_zone’) 結(jié)果:返回tbl_zone表中下1個(gè)要自增的id
注意:如果這個(gè)表不是自增,則返回null
proc_role 返回關(guān)因而否已授與用戶指定角色的信息。(通俗點(diǎn)就是你登陸后看看你是不是有某些角色,有則返回1,否則返回0)
例如:檢查用戶是不是已被授與系統(tǒng)安全員角色:
Select proc_role(‘sso_role’)
檢查用戶是不是已被授與系統(tǒng)安全員角色:
Select proc_role(‘oper_role’)
role_contain 當(dāng)role2包括role1是返回1
role_contain(‘role1’,’role2’)
role_id 返回已指定名稱的角色的系統(tǒng)角色I(xiàn)D
role_name 返回已指定系統(tǒng)角色I(xiàn)D的角色名稱
role_id(‘role_name’)
role_ name’(role_id)
例如:返還sa_role的系統(tǒng)角色I(xiàn)D
Select role_id(‘sa_role’) 結(jié)果:0
Select role_name(0) 結(jié)果:sa_role
show_role 顯示登陸確當(dāng)前啟用的角色。
例如:select show_role()
show_sec_services 列出可供會(huì)話使用的安全服務(wù)。
例如:select show_sec_services()
注意:如果沒有,則為null
suser_id 從syslogins表中返回服務(wù)器用戶的ID號(hào)
suser_name 從syslogins表中返回服務(wù)器用戶的名稱
Suser_id([server_user_name])
例如:select suser_id()
或 selectsuser_id(‘lei’)
select suser_name()
或 select suser_name(4)
syb_quit 終止連接.
例如:終止在其中履行該函數(shù)且返回毛病消息的連接.
Select syb_quit()
tempdb_id 報(bào)告給定會(huì)話分配到的臨時(shí)數(shù)據(jù)庫.
used_pgs 返回表或索引所用的頁數(shù).
user 返回當(dāng)前用戶的名稱.
user_id 返回數(shù)據(jù)庫中指定用戶或當(dāng)前用戶的ID號(hào)
user_name 返回數(shù)據(jù)庫中指定用戶或當(dāng)前用戶的名稱.
例如:select user_id()
或select user_id(‘lei’)
或select user_name()
或select user_name(4)
valid_name 如果指定字符串不是有效標(biāo)識(shí)符,則返回0,否則返回非0數(shù)字
vaild_user 看是不是是1個(gè)數(shù)據(jù)庫中的有功效戶或別名.
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)