Solved

Need to find dash in SQL string

Posted on 2015-02-10
7
100 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
[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
  • 3
  • 3
7 Comments
 
LVL 49

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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 49

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 49

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

733 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