Link to home
Start Free TrialLog in
Avatar of thayduck
thayduckFlag for United States of America

asked on

SQL 2008 InStr

I have a table that contains a field called StateCode:
My state codes reflect USA, Mexico, Canada etc.. so state codes could be larger than 2 positions.



AB-AAAAAAAAAA
ABC-BBBBB
ABCD-MMMM
K-KKKKKK

I need to update another table using information from this table.

In a Where statement, I need to be able to compare everything to the left of the - to something and I also need to compare everything to the right of the - to something else.

I tried doing this but get   'InStr' is not a recognized built-in function name.

WHERE Substring(StateCode,1,(InStr(StateCode,"-")-1)) = StateProvinceCode and Substring(StateCode,(InStr(StateCode,"-")+1),50) = StateProvinceCountryCode

Is there another way to accomplish this ?
ASKER CERTIFIED SOLUTION
Avatar of sognoct
sognoct
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use LEFT and RIGHT,
Try this way
where LEFT(@t, CHARINDEX('-', @t, 1)-1) = StateProvinceCode 
and RIGHT(@t, CHARINDEX('-', REVERSE(@t), 1)-1) = StateProvinceCountryCode

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thayduck

ASKER

Thanks..