使用到的函數
- SUBSTRING(變數, 起始(1), 長度)
- LEFT(變數, 從左取幾個長度)
- ESCAPE CHAR
- REGULAR EXPRESS
- LEN(計算長度)
- REPLACE(變數, 舊字串, 新字串)
- --字串練習
- SELECT '***' + SUBSTRING(VAL, 1, 4) "1取四"
- ,'***' + SUBSTRING(VAL, 5, 8000) "5之後"
- ,LEFT(VAL, 3) "左取3"
- ,CHARINDEX('N', A.VAL) AS "查詢N位置"
- FROM
- (
- SELECT 'abandon' AS VAL
- ) A
- --考慮ESCAPE CHAR
- SELECT *
- FROM
- (
- SELECT '5% DISCOUNT' AS VAL
- )A
- WHERE VAL LIKE '5[%]%'
- --正規表達式 REGULAR EXPRESS
- SELECT VAL
- FROM
- (
- SELECT '123' VAL
- UNION ALL
- SELECT '456'
- UNION ALL
- SELECT 'ABC'
- UNION ALL
- SELECT 'xyz'
- UNION ALL
- SELECT '@789'
- UNION ALL
- SELECT '789@'
- ) A
- WHERE 1=1
- --AND VAL LIKE '[0-9]%' --測試1.數字開頭
- --AND VAL LIKE '[^0-9]%' --測試2.非數字開頭
- AND VAL LIKE '[A-Z]%' --測試3.英文開頭
- --左邊補0 或著 靠左對齊
- SELECT VAL "對照組"
- ,RIGHT( REPLICATE('0',5) + CONVERT(VARCHAR, A.VAL),5) "前面補0"
- ,LEFT(VAL + SPACE(5), 5) "向左對齊"
- FROM
- (
- SELECT '1' VAL
- UNION ALL
- SELECT '12'
- UNION ALL
- SELECT '123'
- )A
- --計算某字串出現的次數
- SELECT (LEN(VAL) - LEN(REPLACE(VAL, 'OX', ''))) / LEN('OX') "OX出現次數"
- FROM
- (
- SELECT 'OXXOXOOOXXX' AS "VAL"
- )A
沒有留言:
張貼留言