简体   繁体   中英

SQL Server - get distinct part of string

Let me start off by saying this is for a database view that I do not manage so the data is what it is. It has a column for job title which also includes the job level (eg Software Engineer 2, Designer C, Air Traffic Controller E-1, etc...) I need to get the distinct job titles. All the Software Engineers (level 1 through 6) should return a single value. Job titles can have one or more words in them. Levels can have 1 to 3 characters. I tried this to get the level

SELECT 
    jobtitle, 
    REVERSE(LEFT(REVERSE(jobtitle), CHARINDEX(' ', REVERSE(jobtitle)) - 1)) AS level

to get the level but I couldn't figure out how to strip that off the job title and then get the distinct value for those.

If level is always a word you can use following which strips the last word off so you get only job title:

SELECT SUBSTRING(jobtitle, 1, LEN(jobtitle) - CHARINDEX(' ', REVERSE(jobtitle)))

or if you prefer using LEFT instead of SUBSTRING:

SELECT LEFT(jobtitle,LEN(jobtitle)-CHARINDEX(' ', REVERSE(jobtitle),0)+1)

If your level is always the format "E-1" or just "C" or "A4" you can split on the last space in your full jobtitle.

Try this:

SELECT RIGHT(jobtitle, charindex(' ', reverse(jobtitle) + ' ') - 1)

The RIGHT will get you the level. Or:

SELECT LEFT(jobtitle, len(jobtitle) - charindex(' ', reverse(jobtitle) + ' '))

The LEFT will get you the title.

I came across these when looking for a sql equivalent for C# string.LastIndexOf() method.

If the level can have a space, this becomes quite a bit more complicated.

You will probably just want to pull these 2 values into 2 columns on a temp table, along with any other relevant pieces of info. Then do your aggregations on the temp table. This will give you more freedom for analysis, and probably better performance than trying to separate the pieces and then do a distinct on the substring all in one shot.

select  reverse(stuff(reverse([JOBTITLE]), 1, 3, '')) ,    Substring(reverse(stuff(([JOBTITLE]), 1, 3, '')),0,CHARINDEX(' ', reverse(stuff(([JOBTITLE]), 1, 3, ''))))

Using an apply operator allows re-use of a generated position within a string for subsequent calculations. eg

SQL Fiddle

MS SQL Server 2017 Schema Setup :

CREATE TABLE Table1
    ([jobtitle] varchar(50))
;

INSERT INTO Table1
    ([jobtitle])
VALUES
    (NULL),
    (''),
    ('nospacehere'),
    ('Software Engineer 2'),
    ('Designer C'),
    ('Air Traffic Controller E-1')
;

Query 1 :

select
  jobtitle
, left(jobtitle,a.pos)
, ltrim(substring(jobtitle,a.pos+1,50))
, a.pos
from table1
outer apply (
  select len(jobtitle) - charindex(' ',reverse(jobtitle))
  ) a (pos)

Results :

|                   jobtitle |                        |        |    pos |
|----------------------------|------------------------|--------|--------|
|                     (null) |                 (null) | (null) | (null) |
|                            |                        |        |      0 |
|                nospacehere |            nospacehere |        |     11 |
|        Software Engineer 2 |      Software Engineer |      2 |     17 |
|                 Designer C |               Designer |      C |      8 |
| Air Traffic Controller E-1 | Air Traffic Controller |    E-1 |     22 |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM