需要查询返回包含列表的不同字符的记录

时间:2013-05-28 20:33:55

标签: sql sql-server sql-server-2012

在Sql Server 2012上,我需要一个查询,它将返回包含任何不同字符的记录。

例如,我的字段名称是“description”。如果“description”字段中查询中的任何字母包含任何不属于以下任何条件的字符,则应列出:

数字(0-9),字母(a-z),其他一些区域字母(öçşğüıÖÇŞİĞÜ),一些标志( - ,。&)和空间()

由于

编辑:我在之前关闭的帖子中也问了同样的问题,我也问了同样的问题并尝试更具描述性。

2 个答案:

答案 0 :(得分:3)

我可能会用计数表解决方案来解决这个问题。你需要一个数字表,无论是TVF还是物理表,但这应该可行

-- http://www.sqlservercentral.com/articles/T-SQL/62867/
--
WITH SRC (row_id, description) AS
(
    SELECT 1, N'normal text'
    UNION ALL SELECT 2, N'bad text ¥1,235'
    UNION ALL SELECT 3, N'normal text öçşğüıÖÇŞİĞÜ'
)
, ALLOWABLE (unicode_character) AS
(
    -- Define your known universe of allowed characters
    -- A-Z
    SELECT CAST(CHAR(GN.number + 64) AS nchar(1)) AS x
    FROM
        dbo.GenerateNumbers(26) AS GN
    UNION ALL
    SELECT CAST((GN.number -1) AS nchar(1)) AS x
    FROM
        dbo.GenerateNumbers(10) AS GN

    -- ...
    UNION ALL SELECT N' '
    UNION ALL SELECT N'ö'
    UNION ALL SELECT N'ç'
    UNION ALL SELECT N'ş'
    UNION ALL SELECT N'ğ'
    UNION ALL SELECT N'ü'
    UNION ALL SELECT N'ı'
    UNION ALL SELECT N'Ö'
    UNION ALL SELECT N'Ç'
    UNION ALL SELECT N'Ş'
    UNION ALL SELECT N'İ'
    UNION ALL SELECT N'Ğ'
    UNION ALL SELECT N'Ü'
)
, EXPLODED AS
(
    SELECT
        S.*
    ,   GN.number
    ,   SUBSTRING(S.description, GN.number, 1) AS individual_character
    FROM
        SRC S
        CROSS APPLY
            -- choose a large enough value      
            dbo.GenerateNumbers(30) AS GN  
)
SELECT
    DISTINCT
    E.row_id
,   E.individual_character      
FROM
    EXPLODED E
    LEFT OUTER JOIN
        ALLOWABLE A
        ON A.unicode_character =  E.individual_character
WHERE
    A.unicode_character IS NULL;

答案 1 :(得分:0)

This answer可能是一个好的开始。它显示了如何使用正则表达式(如语法)来确定某些字符是否在字段中

修改

select * from [TABLE] where [FIELD] LIKE '%[^a-zA-Z0-9öçşğüıÖÇŞİĞÜ-\& \.]%'