all groups > sql server new users > february 2007 >
You're in the

sql server new users

group:

Case Sensitive CharIndex without changing collations


Case Sensitive CharIndex without changing collations Nirmal Singh
2/26/2007 12:00:00 AM
sql server new users:
I have the following (sample) code

Set @BaseString='ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890+-'
Set @myLetter='c'
Set @b1=CharIndex(@myLetter, @BaseString)

@b1 is returning the value 3 instead of 29 because it is case-insensitive.
Can I change this behaviour?

Changing collations on the installation is NOT an option.

Nirmal Singh

Re: Case Sensitive CharIndex without changing collations Nirmal Singh
2/26/2007 12:00:00 AM
Sorry, Line 1 below should be

Set
@BaseString='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890+-'



[quoted text, click to view]

Re: Case Sensitive CharIndex without changing collations AlterEgo
2/26/2007 10:36:09 AM
Nirmal,

Converting the varchar to varbinary might work for you:

declare @String1 varchar(8000)
declare @String2 as varchar(8000)
set @String1 = 'c'
set @String2 =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890+-'

select charindex(cast(@String1 as varbinary(8000)), cast(@String2 as
varbinary(8000)))

-- Bill

[quoted text, click to view]

Re: Case Sensitive CharIndex without changing collations Nirmal Singh
2/27/2007 12:00:00 AM
Thanks Bill, that works fine.

Nirmal


[quoted text, click to view]

Re: Case Sensitive CharIndex without changing collations Anthony Thomas
3/1/2007 6:55:34 PM
Another way would be to explicitly cast the collation to a case-sensitive
one.

DECLARE
@BaseString AS VARCHAR(8000)
,@myLetter AS VARCHAR(8000)

SET @BaseString =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz' +
'01234567890+-'

SET @myLetter = 'c'

SELECT
CHARINDEX(@myLetter, @BaseString)
AS DefaultCollation

,CHARINDEX(
@myLetter COLLATE SQL_Latin1_General_CP1_CS_AS
,@BaseString COLLATE SQL_Latin1_General_CP1_CS_AS
)
AS CaseSensitiveCollation


Because of collation precedence, you could get by with only explicitly
casting one of the two operands, but by explicitly casting both, you are
removing a source of ambiguity.

Good luck.

Sincerely,


Anthony Thomas


--

[quoted text, click to view]
@BaseString='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890
+-'
[quoted text, click to view]

AddThis Social Bookmark Button