?
Solved

SQL 2008 InStr

Posted on 2013-01-04
4
Medium Priority
?
1,276 Views
Last Modified: 2013-01-04
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 ?
0
Comment
Question by:thayduck
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
sognoct earned 200 total points
ID: 38744136
use charindex instead of instr

usage :
CHARINDEX('-',StateCode)
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 38744280
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

0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 200 total points
ID: 38744287
replace the @t with StateCode

where LEFT(StateCode, CHARINDEX('-', StateCode, 1)-1) = StateProvinceCode 
and RIGHT(StateCode, CHARINDEX('-', REVERSE(StateCode), 1)-1) = StateProvinceCountryCode

Open in new window

0
 

Author Closing Comment

by:thayduck
ID: 38744398
Thanks..
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question