Solved

Need to find dash in SQL string

Posted on 2015-02-10
7
106 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 50

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50

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 50

Expert Comment

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

Featured Post

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

696 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