[Reprinted] SQL server T-SQL two methods to distinguish string case

Source: Internet
Author: User

Original article: http://blog.csdn.net/ylqmf/article/details/4972034

Method 1

Write a function

 

-- String comparison functions that are case sensitive to strings
Create function [dbo]. [StrComp] (@ Str1 NVARCHAR (50), @ Str2 NVARCHAR (50 ))
Returns integer--1 is equal, and its return value is not equal.
AS
BEGIN
Declare @ hashcode1 varbinary (32)
Declare @ hashcode2 varbinary (32)
Declare @ result int

Select @ hashcode1 = HashBytes ('md5', @ Str1)
Select @ hashcode2 = HashBytes ('md5', @ Str2)

If @ hashcode1 = @ hashcode2
Begin
Set @ result = 1;
End
Else
Begin
Set @ result = 0;
End
Return @ result;
End

 

Use

Declare @ Str1 nvarchar (50)
Declare @ Str2 nvarchar (50)

Set @ Str1 = 'abc'
Set @ Str2 = 'abc'

Select [dbo]. [StrComp] (@ Str1, @ Str2)

 

Result

====================

0

 

 

 

Method 2

 

Use the collate clause to define the conversion rules of strings as Chinese_PRC_CS_AS_WS

 

Use

Declare @ Str1 nvarchar (50)
Declare @ Str2 nvarchar (50)

Set @ Str1 = 'abc'
Set @ Str2 = 'abc'

If @ Str1 collate Chinese_PRC_CS_AS_WS = @ Str2
Begin
Select 1
End
Else
Begin
Select 0
End

 

Result

==============

0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.