Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 227 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL Case Sensitive String Compare

#1
How do you compare strings so that the comparison is true only if the cases of each of the strings are equal as well. For example:

Select * from a_table where attribute = 'k'

...will return a row with an attribute of 'K'. I do not want this behaviour.
Reply

#2
You can define `attribute` as `BINARY` or use `INSTR` or `STRCMP` to perform your search.
Reply

#3
Select * from a_table where attribute = 'k' COLLATE Latin1_General_CS_AS

Did the trick.
Reply

#4
You can also convert that attribute as **case sensitive** using this syntax :
<!-- language: lang-sql -->

ALTER TABLE Table1
ALTER COLUMN Column1 VARCHAR(200)
COLLATE SQL_Latin1_General_CP1_CS_AS

Now your search will be **case sensitive**.

If you want to make that column **case insensitive** again, then use
<!-- language: lang-sql -->


ALTER TABLE Table1
ALTER COLUMN Column1 VARCHAR(200)
COLLATE SQL_Latin1_General_CP1_CI_AS

Reply

#5
You Can easily Convert columns to VARBINARY(Max Length), The length must be the maximum you expect to avoid defective comparison, It's enough to set length as the column length. Trim column help you to compare the real value except space has a meaning and valued in your table columns, This is a simple sample and as you can see I Trim the columns value and then convert and compare.:

CONVERT(VARBINARY(250),LTRIM(RTRIM(Column1))) = CONVERT(VARBINARY(250),LTRIM(RTRIM(Column2)))

Hope this help.
Reply

#6
Just as another alternative you could use HASHBYTES, something like this:

SELECT *
FROM a_table
WHERE HASHBYTES('sha1', attribute) = HASHBYTES('sha1', 'k')
Reply

#7
simplifying the general answer

> SQL Case Sensitive String Compare

These examples may be helpful:

Declare @S1 varchar(20) = 'SQL'
Declare @S2 varchar(20) = 'sql'


if @S1 = @S2 print 'equal!' else print 'NOT equal!' -- equal (default non-case sensitivity for SQL

if cast(@S1 as binary) = cast(Upper(@S2) as binary) print 'equal!' else print 'NOT equal!' -- equal

if cast(@S1 as binary) = cast(@S2 as binary) print 'equal!' else print 'NOT equal!' -- not equal

if @S1 COLLATE Latin1_General_CS_AS = Upper(@S2) COLLATE Latin1_General_CS_AS print 'equal!' else print 'NOT equal!' -- equal

if @S1 COLLATE Latin1_General_CS_AS = @S2 COLLATE Latin1_General_CS_AS print 'equal!' else print 'NOT equal!' -- not equal


The convert is probably more efficient than something like runtime calculation of hashbytes, and I'd expect the collate may be even faster.

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through