Solved

Need to find dash in SQL string

Posted on 2015-02-10
7
99 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 48

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 48

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 48

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

838 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