如何检测列NVARCHAR是否包含拉丁字符或西里尔字符

时间:2016-11-24 09:37:51

标签: sql sql-server

我有"模板"表:

CREATE TABLE Template (
  ID BIGINT, -- PK
  NAME NVARCHAR(255)
)

NAME 包含俄语或英语文本。如何将此列的值移至 RUSSIAN_NAME ENGLISH_NAME 列取决于 NAME 列值的值。

CREATE TABLE Template (
  ID BIGINT, -- PK
  RUSSIAN_NAME NVARCHAR(255),
  ENGLISH_NAME NVARCHAR(255)
)

1 个答案:

答案 0 :(得分:2)

试试这个:

我不知道俄语文本是什么意思,只是从某处复制

DECLARE @tbl TABLE (name NVARCHAR(255),plainLatin NVARCHAR(255),foreignChars NVARCHAR(100));
INSERT INTO @tbl(name) VALUES
 (N'abcd'),(N'слов в тексте'),(N'one more'),(N'с пробелами и без них');

UPDATE @tbl
SET plainLatin=CASE WHEN PATINDEX('%[^-a-zA-Z0-9 ]%' /*add signs you want to allow*/,name)=0 THEN name END
   ,foreignChars=CASE WHEN PATINDEX('%[^-a-zA-Z0-9 ]%' /*add signs you want to allow*/,name)>0 THEN name END

SELECT * FROM @tbl

结果

+-----------------------+------------+-----------------------+
| name                  | plainLatin | foreignChars          |
+-----------------------+------------+-----------------------+
| abcd                  | abcd       | NULL                  |
+-----------------------+------------+-----------------------+
| слов в тексте         | NULL       | слов в тексте         |
+-----------------------+------------+-----------------------+
| one more              | one more   | NULL                  |
+-----------------------+------------+-----------------------+
| с пробелами и без них | NULL       | с пробелами и без них |
+-----------------------+------------+-----------------------+