如何在MS Sql server中搜索区分大小写的字符串?

时间:2013-05-27 05:20:48

标签: sql-server

如何在ms SQL server中搜索区分大小写的数据,如 user_name 密码。 在MySQl中,它由 BINARY()函数完成。

2 个答案:

答案 0 :(得分:1)

使用区分大小写的collat​​e创建列,并尝试以下操作:

<强>查询:

DECLARE @temp TABLE
(
      Name VARCHAR(50) COLLATE Latin1_General_CS_AS
)

INSERT INTO @temp (Name)
VALUES 
    ('Ankit Kumar'),
    ('DevArt'),
    ('Devart')

SELECT * 
FROM @temp
WHERE Name LIKE '%Art'

<强>输出:

DevArt

或尝试使用此类似代码 -

DECLARE @temp TABLE (Name NVARCHAR(50))

INSERT INTO @temp (Name)
VALUES 
    ('Ankit Kumar'),
    ('DevArt'),
    ('Devart')

SELECT * 
FROM @temp
WHERE Name LIKE '%Art' COLLATE Latin1_General_CS_AS

答案 1 :(得分:1)

可以使用Casting to Binary

来完成此操作
SELECT * FROM UsersTable
WHERE   
    CAST(Username as varbinary(50)) = CAST(@Username as varbinary(50))
    AND CAST(Password as varbinary(50)) = CAST(@Password as varbinary(50))
    AND Username = @Username 
    AND Password = @Password