Solved

Extracting an ID from a built up string?

Posted on 2008-10-02
2
245 Views
Last Modified: 2012-05-05
I am building up a string a storing them in the database:

eg:


72-8442

I was wondering how I would go about extracting '8442' from this string in SQL because I would like to do:

select serialnumber from assets where id = '8442'

Any ideas?

The Format is always:

ID1-ID2
72-8442

Cheers
0
Comment
Question by:directxBOB
2 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 250 total points
ID: 22624072
if id1 is always 2 characters then....

select serialnumber from assets where substr(id,4) = '8442'

if id1 might vary in length then...

select serialnumber from assets where substr(id,instr(id,'-')+1) = '8442'


note, you won't be able to use an index on id when applying these conditions unless you create a function based index
0
 
LVL 4

Assisted Solution

by:GO-87
GO-87 earned 250 total points
ID: 22624128
Or if you don't know what the number will be, and assuming the column you're selecting is called myID:



select substring(myID, charindex('-',myID,1)+1, len(myID) ) 

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL export CSV & schedule It 9 52
mySQL. SQL query. Substitute for Numeric key word. 3 49
How to get the following query to show decimals 3 28
the whoisactive update 12 39
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 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

18 Experts available now in Live!

Get 1:1 Help Now