?
Solved

Need to find dash in SQL string

Posted on 2015-02-10
7
Medium Priority
?
108 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 51

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 51

Accepted Solution

by:
Vitor Montalvão earned 2000 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 51

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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…
Suggested Courses

752 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