SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to compare two string variables


how to compare two string variables

Author
Message
iceheartjade
iceheartjade
SSC-Enthusiastic
SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)

Group: General Forum Members
Points: 101 Visits: 58
Hi,

I need to find out if the first string is in second string. How can I compare two string variables as following:

declare @s1 as varchar
declare @s2 as varchar

set @s1 = '1232'
set @s2 = 'tete,1232'

select patindex('%1232%','tete,1232') will return a number

Can I do something like select patindex (@s1, @s2)?
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)

Group: General Forum Members
Points: 203470 Visits: 41949
Yes you can... like this...

DECLARE @S1 VARCHAR(100)
DECLARE @S2 VARCHAR(100)

SET @S1 = '5'
SET @S2 = '1,2,3,4,55,6'

SELECT PATINDEX('%'+@S1+'%',@S2)



But, you notice that in this case, the answer is incorrect. There is no "5" in @S2... the number is incorrectly being returned because of the "55" the "5" picks up on.

If you're not going to split the data, then you must wrap the delimiters in the search on both @S1 and @S2... like this...

DECLARE @S1 VARCHAR(100)
DECLARE @S2 VARCHAR(100)

SET @S1 = '5'
SET @S2 = '1,2,3,4,55,6'

SELECT PATINDEX('%,'+@S1+',%' , ','+@S2+',')



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
SqlUser-529296
SqlUser-529296
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1164 Visits: 754
try this one also ....

DECLARE @s1 varchar(16)
DECLARE @s2 varchar(16)

SET @s1 = '1232'
SET @s2 = 'tete,1232'

SELECT @s1,@s2

SELECT SUBSTRING(@s2,CHARINDEX(@s1,@s2,1),LEN(@s1))

---
iceheartjade
iceheartjade
SSC-Enthusiastic
SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)

Group: General Forum Members
Points: 101 Visits: 58
Thanks !
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)

Group: General Forum Members
Points: 203470 Visits: 41949
sqluser (2/20/2008)
try this one also ....

DECLARE @s1 varchar(16)
DECLARE @s2 varchar(16)

SET @s1 = '1232'
SET @s2 = 'tete,1232'

SELECT @s1,@s2

SELECT SUBSTRING(@s2,CHARINDEX(@s1,@s2,1),LEN(@s1))

---

Careful!!!! You must still include the delimiter wrapping or you will get false returns.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search