Solved

Extracting an ID from a built up string?

Posted on 2008-10-02
2
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 74

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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