sybase 標準函式

2021-09-01 20:35:27 字數 4467 閱讀 4176

長度和語法分析

datalength(char_expr)

在char_expr中返回字元的長度值,忽略尾空

substring(expression,start,length)

返回部分字串

right(char_expr,int_expr)

返回char_expr右邊的int_expr字元

基本字串運算

upper(char_expr)

把char_expr轉換成大寫形式

lower(char_expr)

把char_expr轉換成小寫形式

stuff(expr1,start,length,expr2)

用expr2代替epxr1中start起始長為length的字串

reverse(char_expr)

反寫char_expr中的文字

ltrim(char_expr)

刪除頭空

rtrim(char_expr)

刪除尾空

格式轉換

ascii(char_expr)

返回char_expr中第乙個字元的ascii值

char(int_expr)

把ascii碼轉換為字元, 和ascii相對

str(float_expr[,length[,decimal]])

進行數值型到字元型轉換

soundex(char_expr)

返回char_expr的soundex值

difference(char_expr1,char_expr2)

返回表示式soundex值之差

串內搜尋

charindex(char_expr,expression)

返回指定char_expr的開始位置,否則為0

patindex("%pattern%",expression)

返回指定樣式的開始位置,否則為0

datalength用於確定可變字串的長度

soundex用於確定字串是否發音相似

difference返回0-4之間的值,0表示最不相似,4表示最相似

萬用字元

% 匹配任何數量的字元或無字元

_ 匹配任何單個字元(空間佔位符)

規定有效範圍,或某個"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子句

用某個轉義字元可在搜尋字串時將萬用字元作為文本來包含。

ansi-89 sql標準定義了escape子句指定某個轉義字元

預設情況下,來轉義某個萬用字元,例:

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

數學函式

abs(numeric_expr)

返回指定值的絕對值

ceiling(numeric_expr)

返回大於或等於指定值的最小整數

floor(numeric_expr)

返回小於或等於指定值的最大整數

pi()

返回常數3.1415926

power(numeric_expr,power)

返回numeric_expr的值給power的冪

rand([int_expr])

返回0-1之間的隨機浮點數,可指定基值

round(numeric_expr,int_expr)

把數值表示式圓整到int_expr指定的精度

sign(int_expr)

返回正+1,零0或負-1

sqrt(float_expr)

返回指定值的平方根

sql server支援所有標準的三角函式和其他有用的函式

日期函式

getdate()

返回當前的系統日期和時間

datename(datepart,date_expr)

以字串形式返回date_expr指定部分的值,轉換成合適的名字

datepart(datepart,date_expr)

作為整數返回date_expr值的指定部分 和datename對應

datediff(datepart,date_expr1,date_expr2)

返回date_expr2-date_expr1,通過指定的datepart度量

dateadd(datepart,number,date_expr)

返回日期,通過在date_expr上增加指定number的日期部件而產生的

datepart

日期部件 縮寫 值範圍

年 yy 1753-9999

季度 qq 1-4

月 mm 1-12

每年中第幾天 dy 1-366

天 dd 1-31

一年中第幾星期 wk 1-54

一周中第幾天 dw 1-7(1=sunday)

小時 hh 0-23

分鐘 mi 0-59

秒 ss 0-59

毫秒 ms 0-999

例: select invoice_no,

datediff(dd,date_shipped,getdate())

from invoices

where balance_due>0

轉換函式convert

此函式把值從一種型別改變成另一種型別

convert(datetype [(length)],expression)

select "advance="+convert(char(12),advance)

from titles

日期轉換

convert(datetype[(length)],expression,format)

format指定將日期轉換為什麼格式,有以下值:

沒有世紀 有世紀 轉換字串中日期格式

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

系統函式

函式 定義

訪問和安全性資訊

host_id() 客戶程序的當前主機程序id號

host_name() 客戶程序的當前主計算機名

suser_id(["login_name"]) 使用者的sql server id號

suser_name([server_user_id]) 使用者的sql server登入名

user_id(["name_in_db"]) 使用者在資料庫中的id號

user_name([user_id]) 使用者在資料庫中的名字

user 使用者在資料庫中的名字

show_role() 使用者的當前活動角色

資料庫和物件資訊

db_id(["db_name"]) 資料庫id號

db_name([db_id]) 資料庫名

object_id("objname") 資料庫物件id號

object_name(obj_id]) 資料庫物件號

col_name(obj_id,col_id) 物件的欄名

col_length("objname","colname") 欄的長度

index_col("objname",index_id,key#) 已索引的欄名

valid_name(char_expr) 若char_expr不是有效識別符號,則返回0

資料函式

tsequal(timestamp1,timestamp2) 比較時戳值,若時戳值不匹配,則返回出錯訊息

isnull()

isnull函式用指定的值代替查詢欄或合計中的空值

sybase 日期函式

getdate 得到當前時間 可以設定得到各種時間格式 datepart 日期部分,日期 取指定時間的某乙個部分 年月天時分秒 datediff 日期部分,日期 1,日期 2 計算指定的日期 1和日期 2的時間差多少 dateadd 日期部分 數值表示式,日期 計算指定時間 再加上表示式指定的時間長...

sybase 函式使用

一.字元操作 1.替換指定字元 str replace string1,需要替換字元,替換成字元 substring string1,二.數值操作 三.日期操作 getdate 得到當前時間,可以設定得到各種時間格式.datepart 日期部分,日期 取指定時間的某乙個部分,年月天時分秒.dated...

sybase 常用函式

a.substring b.len 查詢 串內搜尋 charindex char expr,expression 返回指定char expr的開始位置,否則為0 select charindex c job id from job z where job id 1 c.字串連線 select sub...