Solved

Need to find dash in SQL string

Posted on 2015-02-10
7
93 Views
Last Modified: 2015-02-12
Hello,
I have a table titled "salary"
In my "salary" table I have a field titled "a_id"

The values in the field "a_id" look something like this:
143939-0100000373-1
143939-0100000373-2
53866-02009-1
53866-02009-2
53866-02009-3

I need to write a query that will select the highest value AFTER THE SECOND DASH.

Thanks..
0
Comment
Question by:swaggrK
  • 3
  • 3
7 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40600948
In your example what should be returned? These records?
143939-0100000373-2
53866-02009-3

Or these records?
2
3

Or even this one?
3
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40600978
This is a good example of packing multiple conceptual values into a single field.  That's probably customer number, order number, and revision or something of the sort.  The 3 should be in separate fields and only concatenated with dashes when needed for display.

I understand you probably do not have the luxury of fixing the system design.  But ... for the record and future reference ... you are experiencing the problems with violation of First Normal Form.
0
 

Author Comment

by:swaggrK
ID: 40600999
Based on the example records I provided, the following results would be returned.

Example values:
143939-0100000373-1
143939-0100000373-2
53866-02009-1
53866-02009-2
53866-02009-3

Would like the results to look like:
143939-0100000373-2
 53866-02009-3
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40601008
Ok. You can use this code:
SELECT LEFT(a_id, LEN(a_id) - CHARINDEX('-',REVERSE(a_id))), MAX(RIGHT(a_id, CHARINDEX('-',REVERSE(a_id))-1))
FROM Salary
GROUP BY LEFT(a_id, LEN(a_id) - CHARINDEX('-',REVERSE(a_id)))

Open in new window

0
 

Author Comment

by:swaggrK
ID: 40601153
@Victor, this looks good but if you could assist with one last piece of the puzzle that would be great.
I am having trouble re-combing the list of returned values.

Since the results are being returned as aliases, I need to...

a) re-combine the resultset so that a split value like "143939-0100000373" and "6" will look like its original value of "143939-0100000373-6"

b) use my new alias (143939-0100000373-6) in my Cross Apply
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40602558
Did you open a new question for this? If so, please post the url.
0
 

Author Comment

by:swaggrK
ID: 40606088
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

708 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

21 Experts available now in Live!

Get 1:1 Help Now