2007年11月29日 星期四

Sybase 資料庫函數

Sybase字元串函數 長度和語法分析

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轉換成小寫形式

space(int_expr)
生成有int_expr個空格的字元串

replicate(char_expr,int_expr)
重複char_expr,int_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碼轉換為字符

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)
返回大于或等于指定值的最小整數

exp(float_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值的指定部分

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
\r\n
例: convert(char(11),dt_registdate ,111) + convert(char(5),dt_registdate ,108)
\r\n
顯示結果: 2006/10/13 20:26

系統函數
函數 定義
訪問和安全性訊息
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
數據函數
datalength(expression) 按位元組返回expression的長度
tsequal(timestamp1,timestamp2) 比較時戳值,若時戳值不匹配,則返回出錯消息

isnull()
isnull函數用指定的值代替查詢欄或合計中的空值
例︰
select avg(isnull(total_order,$0))
from invoices


綜合運用

格式化數字,轉換數字為x長度的字串,前補零
substring('00'||rtrim(convert(varchar(X),expression)),datalength('00'||rtrim(convert(varchar(X),expression)))-(X-1),X)

note:'00'中為X-1個零


流程控制

if...else

if exists (select postalcode from authors
where postalcode = "94705")
print "Berkeley author"



case expression

select title, "Contract Status" =
case
when contract = 1 then "Contract"
when contract = 0 then "No Contract"
end
from titles

沒有留言: