SQL Server
SQL Server 常用函數 SQLServer charindex ( string_expression1 , string_expression2 [ , start_location ]  ) 搜尋 "string_expression2" 以找出第一個出現的 "string_expression1",然後傳回整數。"Start_location" 是在 "string_expression2" 中開始搜尋 "string_expression1" 的字元位置。如果沒有指定 "start_location"、或它是負數,或它是零,則會從 "string_expression2" 的開頭開始搜尋。 concat ( string_expression1 , string_expression2 ) 傳回連結或結合 "string_expression1" 與 "string_expression2" 後得到的字串。 範例:concat ( [銷售額 (查詢)].[銷售人員].[名字], [銷售額 (查詢)].[銷售人員].[姓氏] ) 結果:傳回名字與姓氏;例如 Bob Smith。 {current_user} { current_user } 傳回現行使用者的名稱。 datalength ( string_expression ) 傳回 "string_expression" 中的位元組數。 dateadd ( { datepart } , integer_expression , date_expression ) 傳回將 "integer_expression" 單位數(以 "datepart"(日、月、年)指出)加到 "date_expression" 後產生的日期。 datediff ( {datepart} , date_expression1 , date_expression2 ) 傳回 "date_expression1" 與 "date_expression2" 之間的 "datepart"(日、月、年)單位數。 範例:datediff ( {yy} , 1984-01-01 , 1997-01-01 ) 結果:13 datename ( ' { ' datepart ' } ' , date_expression ) 從 "date_expression" "datepart",它可以是 ASCII 字串形式的 datetime、smalldatetime、date 或 time 值。請注意,"datepart" 必須是代表 datepart 的關鍵字或 Microsoft® SQL Server 能辨識的 datepart 縮寫,並且必須包括在大括弧內。 範例:datename ( {mm} , 2000-01-01 ) 結果:1 月。 datepart ( ' { ' datepart ' } ' , date_expression ) 以整數傳回 "date_expression" 的一部分(例如,月)。"date_expression" 可以是 datetime、smalldatetime、date 或 time 值。請注意,"datepart" 必須是代表 datepart 的關鍵字或 Microsoft® SQL Server 能辨識的 datepart 縮寫,並且必須包括在大括弧內。 範例:datepart ( {wk} , 2000-01-01 ) 結果:1(該年度的第一週) day ( date_expression ) 傳回 "date_expression" 日的部分。與 extract (day from date_expression) 相同。 difference ( string_expression1 , string_expression2 ) 傳回一個整數值,代表由 "string_expression1" 和 "string_expression2" 的資料來源特定的 soundex 函數傳回的值之間的差異。會傳回範圍從 0 到 4 的值,其中 4 代表最符合。請注意,4 不表示這兩個字串完全相同。 範例:difference ([銷售目標 (查詢)].[銷售人員].[名字],[銷售額 (查詢)].[零售商].[聯絡人名字]) 結果:0 範例:difference ([銷售目標 (查詢)].[銷售人員].[名字],[銷售目標 (查詢)].[銷售人員].[名字]) 結果:4 getdate () 傳回日期時間值,代表執行資料庫軟體的電腦上的現行日期和時間。 isnull ( expression , expression ) 傳回第一個非空值的引數(如果兩個引數均為空值,則會傳回空值)。建議使用 SQL 標準 COALESCE 功能來取代。 範例:isnull ( [銷售額 (查詢)].[銷售數據].[銷售數量] , 0 ) 結果:若不是空值,傳回銷售數量,否則傳回 0。 left ( string_expression , integer_expression ) 傳回 "string_expression" 最左側的 "integer_expression" 個字元。 範例:left ( [銷售額 (查詢)].[銷售人員].[姓氏] , 3 ) 結果:傳回每個姓氏的前三個字元。 ltrim ( string_expression ) 傳回已移除前導空格的 "string_expression"。 month ( date_expression ) 傳回 "date_expression" 月的部分。與擷取 date_expression 中的月相同。 patindex ( string_expression1 , string_expression2 ) 傳回一個整數,此數字代表 "string_expression1" 在 "string_expression2" 中第一次出現的起始位置。如果找不到 "string_expression1" 會傳回 0。"string_expression1" 前後必須有 % 萬用字元,但搜尋前面或後面的幾個字元時除外。 範例:patindex ( '%po%', 'Report' ) 結果:3 replace ( string_expression1 , string_expression2 , string_expression3 ) 以 "string_expression3" 取代在 "string_expression1" 中出現的所有 "string_expression2"。 replicate ( string_expression , integer_expression ) 傳回將 "string_expression" 重複 "integer_expression" 次後組成的字串。 reverse ( string_expression ) 以相反順序傳回 "string_expression"。 rtrim ( string_expression ) 傳回已移除尾端空格的 "string_expression"。 範例:rtrim ( [銷售額 (查詢)].[銷售人員].[姓氏] ) 結果:傳回已移除名字結尾處的所有空格的姓氏。 sign ( numeric_expression ) 傳回 "numeric_expression" 的正負號指示符:如果 "numeric_expression" 是正數會傳回 +1,如果是零會傳回 0,如果是負數則會傳回 -1。 space ( integer_expression ) 傳回由 "integer_expression" 個空格組成的字串。 str ( numeric_expression [ , integer_expression1 [ , integer_expression2 ] ] ) 傳回 "numeric_expression" 的字串表示法,其中 "integer_expression1" 是傳回的字串長度,而 "integer_expression2" 是小數位數。 stuff ( string_expression1 , integer_expression1 , integer_expression2 , string_expression2 ) 傳回一個字串,這是在 "string_expression1" 的第 "integer_expression1" 個字元處開始刪除 "integer_expression2" 個字元,然後在該處開始將 "string_expression2" 插入到 "string_expression1" 之後產生的字串。字串中的第一個字元是在位置 1。 year ( date_expression ) 傳回 "date_expression" 年的部分。與擷取 date_expression 中的年相同。 ascii ( string_expression ) 傳回一個數字,此數字代表 "string_expression" 最左側字元的 ascii 字碼值。 範例:ascii ( 'A' ) 結果:65 char ( integer_expression ) 傳回一個字元,此字元具有 "integer_expression" 所指定的 ASCII 字碼值。"Integer_expression" 應該在 0 到 255 之間。 範例:char ( 65 ) 結果:A right ( string_expression , integer_expression ) 傳回 "string_expression" 最右側的 "integer_expression" 個字元。 round ( numeric_expression , integer_expression ) 傳回四捨五入到小數點右邊第 "integer_expression" 位數的近似值後得到的 "numeric_expression"。在替資料套用格式之前,請先將它四捨五入。 範例:round (125, -1) 結果:130 soundex ( string_expression ) 傳回代表 "string_expression" 中的單字發音的 4 字元的字串。
SQL Server 強制轉型函數 cast_float ( expression ) 傳回將 "expression" 強制轉型為浮點數之後的值。 cast_real ( expression ) 傳回將 "expression" 強制轉型為實數之後的值。 cast_smallint ( expression ) 傳回將 "expression" 強制轉型為小整數之後的值。 cast_integer ( expression ) 傳回將 "expression" 強制轉型為整數之後的值。 範例:integer ( 84.95 ) 結果:85 cast_timestamp ( expression ) 傳回將 "expression" 強制轉型為日期時間之後的值。 cast_char ( expression ) 傳回將 "expression" 強制轉型為字元之後的值。傳回的字元數限制為 30 個。 cast_varchar ( expression ) 傳回強制轉型為變數字元之後的 "expression" 值。
SQL Server 數學函數 log ( numeric_expression ) 傳回 "numeric_expression" 的自然對數。 log10 ( numeric_expression ) 傳回 "numeric_expression" 以 10 為基數的對數。 pi () 以浮點數值傳回 pi 的常數值。 rand ( integer_expression ) 使用 "integer_expression" 作為種子值來產生亂數。
SQL Server 三角函數 acos ( numeric_expression ) 傳回 "numeric_expression" 的反餘弦值,以弧度表示。反餘弦值是其餘弦值為 "numeric_expression" 的角度。 asin ( numeric_expression ) 傳回 "numeric_expression" 的反正弦值,以弧度表示。反正弦值是其正弦值為 "numeric_expression" 的角度。 atan ( numeric_expression ) 傳回 "numeric_expression" 的反正切值,以弧度表示。反正切值是其正切值為 "numeric_expression" 的角度。 atn2 ( numeric_expression1, numeric_expression2 ) 傳回分別由 "numeric_expression1" 和 "numeric_expression2" 指定的 x 座標和 y 座標的反正切值,以弧度表示。反正切值是其正切值為 "numeric_expression1" 的角度。 cos ( numeric_expression ) 傳回 "numeric_expression" 的餘弦值,其中 "numeric_expression" 是以弧度表示的角度。 cot ( numeric_expression ) 傳回 "numeric_expression" 的餘切值,其中 "numeric_expression" 是以弧度表示的角度。 degrees ( numeric_expression ) 傳回將 "numeric_expression" 弧度轉換為度數之後的值。 radians ( numeric_expression ) 傳回從 "numeric_expression" 度數轉換成的弧度數。 sin ( numeric_expression ) 傳回 "numeric_expression" 的正弦值,其中 "numeric_expression" 是以弧度表示的角度。 tan ( numeric_expression ) 傳回 "numeric_expression" 的正切值,其中 "numeric_expression" 是以弧度表示的角度。