Click here to Skip to main content
15,908,015 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a variable @Var1 varchar(100) and then a second variable @var2 varchar(10); How can I use either PatIndex or CharIndex to check if @Var1 starts with @Var2 or @Var1 ends with @Var2?

Your answer is well appreciated.
Posted
Updated 18-Jan-13 11:03am
v2

1 solution

You can use this approach for example:
SQL
declare @val1 varchar(100)
set @val1='abcd'

declare @val2 varchar(10)
set @val2='ab'

if @val1 LIKE @val2 + '%' print 'STARTSWITH'
if @val1 LIKE '%' + @val2 print 'ENDSWITH'
 
Share this answer
 
Comments
fjdiewornncalwe 18-Jan-13 17:14pm    
+5. Does the job.
Bassofa 18-Jan-13 17:25pm    
Interestingly, I have the same logic exactly already, but it's not working. The thing is , I am pulling both variables from two diffent tables; it may not be my logic but the data types.
Bassofa 18-Jan-13 17:50pm    
Thank you; like I commented above; this logic is ok; my problem was related to something different.
Zoltán Zörgő 19-Jan-13 5:45am    
Than post your exact situation here. This is the answer to what you requested. If it is not working, than your question is not covering your needs.
Bassofa 19-Jan-13 17:57pm    
The problem was because that part of the code was not running at all because a condition above this logic was not being met. I fixed that and it worked as I expected. Thanks man.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900