- LEN():回傳字串字元數,但尾端空白不算。
- DATALENGTH():回傳字串位元組數目。
DECLARE @Temp TABLE(Data char(10), Data1 char(10) , Data2 varchar(10) , Data3 nchar(10) , Data4 nvarchar(10)) INSERT INTO @Temp SELECT '123','123','123','123','123' UNION ALL SELECT 'ABC','ABC','ABC','ABC','ABC' UNION ALL SELECT '1A1','1A1','1A1','1A1','1A1' UNION ALL SELECT '123測試ABC','123測試ABC','123測試ABC','123測試ABC','123測試ABC' -- 中文字 UNION ALL SELECT '789 ','789 ','789 ','789 ','789 ' -- 尾端故意留一個空格 SELECT Data , LEN(Data1) AS [charL], DATALENGTH(Data1) AS [charDL], LEN(Data2) AS [varcharL], DATALENGTH(Data2) AS [varcharDL], LEN(Data3) AS [ncharL], DATALENGTH(Data3) AS [ncharDL], LEN(Data4) AS [nvarcharL], DATALENGTH(Data4) AS [nvarcharDL] FROM @Temp
- 以 nvarchar 為例子來判斷字串中是否有中文字
SELECT Data4 FROM @Temp WHERE LEN(CAST(Data4 AS varchar(8000))) = DATALENGTH(CAST(RTRIM(Data4) AS VARCHAR(8000)))
此作法盲點在於,不是只有中文有此特性,日文和韓文也是,字串中假如同時混雜中文、日文和韓文時,這個作法就無法篩選結果。
沒有留言:
張貼留言