天天看點

SQL Server基本函數詳細介紹

1、字元串函數

  長度與分析用

  datalength(Char_expr) 傳回字元串包含字元數,但不包含後面的空格

  substring(expression,start,length) 不多說了,取子串

  right(char_expr,int_expr) 傳回字元串右邊int_expr個字元

  字元操作類

  upper(char_expr) 轉為大寫

  lower(char_expr) 轉為小寫

  space(int_expr) 生成int_expr個空格

  replicate(char_expr,int_expr)複制字元串int_expr次

  reverse(char_expr) 反轉字元串

  stuff(char_expr1,start,length,char_expr2) 将字元串char_expr1中的從

  start開始的length個字元用char_expr2代替

  ltrim(char_expr) rtrim(char_expr) 取掉空格

  ascii(char) char(ascii) 兩函數對應,取ascii碼,根據ascii嗎取字元

  字元串查找

  charindex(char_expr,expression) 傳回char_expr的起始位置

  patindex("%pattern%",expression) 傳回指定模式的起始位置,否則為0

2.數學函數

  abs(numeric_expr) 求絕對值

  ceiling(numeric_expr) 取大于等于指定值的最小整數

  exp(float_expr) 取指數

  floor(numeric_expr) 小于等于指定值得最大整數

  pi() 3.1415926.........

  power(numeric_expr,power) 傳回power次方

  rand([int_expr]) 随機數産生器

  round(numeric_expr,int_expr) 安int_expr規定的精度四舍五入

  sign(int_expr) 根據正數,0,負數,,傳回+1,0,-1

  sqrt(float_expr) 平方根

3.日期函數

  getdate() 傳回日期

  datename(datepart,date_expr) 傳回名稱如 June

  datepart(datepart,date_expr) 取日期一部份

  datediff(datepart,date_expr1.dateexpr2) 日期差

  dateadd(datepart,number,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 周幾

  hh 0-23 小時

  mi 0-59 分鐘

  ss 0-59 秒

  ms 0-999 毫秒

  日期轉換

  convert()

4.系統函數

  suser_name() 使用者登入名

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

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

  show_role() 對目前使用者起作用的規則

  db_name() 資料庫名

  object_name(obj_id) 資料庫對象名

  col_name(obj_id,col_id) 列名

  col_length(objname,colname) 列長度

  valid_name(char_expr) 是否是有效辨別符