Solved

SQL 2008 InStr

Posted on 2013-01-04
4
667 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 50 total points
ID: 38744136
use charindex instead of instr

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

Expert Comment

by:ewangoya
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:ewangoya
ewangoya earned 50 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Contained Database Collations 6 20
using t-sql EXISTS 8 23
Scheduling Jobs for Execution: 4 16
Square brackets 4 0
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now