Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string and return greatest in mssql

I need to find a way to get the data with the highest versionNumber.

Here is my database design:

VERSIONNUMBER - varchar(15)
DOWNLOADPATH - varchar(100)

Lets say I have records like:

VERSIONNUMBER -------- DOWNLOADPATH
1.1.2                  a.com
1.1.3                  b.com
2.1.4                  c.com
2.1.5                  d.com
2.2.1                  e.com

I need to get the record with the versionnumber 2.2.1. Need some help with the sql though :)

Thank you for any help

like image 914
Pabuc Avatar asked Jul 28 '11 07:07

Pabuc


People also ask

How do I split a string into two parts in SQL?

You can use the STRING_SPLIT () function in the SQL server to split a delimited string. With the help of the SPLIT_PART () function, you can split any string in SQL. This PostgreSQL split string function takes a string to split and a delimiter to use.

How to split a string in PostgreSQL?

You can use the STRING_SPLIT () function in the SQL server to split a delimited string. With the help of the SPLIT_PART () function, you can split any string in SQL. This PostgreSQL split string function takes a string to split and a delimiter to use. Also, it returns the part of the string as specified.

How do you split a sentence into words in SQL Server?

sentence. The STRING_SPLIT (string, separator) function in SQL Server splits the string in the first argument by the separator in the second argument. To split a sentence into words, specify the sentence as the first argument of the STRING_SPLIT () function and ' ' as the second argument.

How to split a string into words using string_split () function?

The STRING_SPLIT (string, separator) function in SQL Server splits the string in the first argument by the separator in the second argument. To split a sentence into words, specify the sentence as the first argument of the STRING_SPLIT () function and ' ' as the second argument. STRING_SPLIT () results in a column named value.


3 Answers

Alternatively, instead of multiplying each digit group, you can use ranking:

with a as
(
    select * from (values
        ('2.1.4 '), ('2.1.12'), ('2.1.5'), ('2.2.1') 
    ) as b(c)     
),
x as
(
select c, 
    Ranking = RANK() over(order by convert(int,PARSENAME(c,3)), convert(int,PARSENAME(c,2)), convert(int,PARSENAME(c,1))) 
from a
)
select * from x 
    order by ranking

Yields:

c   Ranking
2.1.4   1
2.1.5   2
2.1.12  3
2.2.1   4

Final query:

with a as
(
    select * from (values
        ('2.1.4 '), ('2.1.12'), ('2.1.5'), ('2.2.1') 
    ) as b(c)     
),
x as
(
select c, 
    Ranking = RANK() over(order by convert(int,PARSENAME(c,3)), convert(int,PARSENAME(c,2)), convert(int,PARSENAME(c,1))) 
from a
)
select * 
from x  
where Ranking = (select MAX(ranking) from x)

Output:

c   Ranking
2.2.1   4

Another simple approach, sort descending then just get the first row:

with a as
(
    select * from (values
        ('2.1.4 '), ('2.1.12'), ('2.1.5'), ('2.2.1') ) as b(c)     
),
x as
(
select c, 
    Ranking = RANK() 
        over(order by 
            convert(int,PARSENAME(c,3)) desc, 
            convert(int,PARSENAME(c,2)) desc, 
            convert(int,PARSENAME(c,1)) desc) 
from a
)
select * 
from x  
where Ranking = 1
like image 59
Michael Buen Avatar answered Oct 27 '22 14:10

Michael Buen


select top 1 DOWNLOADPATH
from YourTable
order by cast(parsename(VERSIONNUMBER, 3) as int) desc,
         cast(parsename(VERSIONNUMBER, 2) as int) desc,
         cast(parsename(VERSIONNUMBER, 1) as int) desc
like image 26
Mikael Eriksson Avatar answered Oct 27 '22 13:10

Mikael Eriksson


If you are on SQL Server 2008 you can lever the HIERARCHYID datatype.

SELECT VersionNumber, DownloadPath
FROM (VALUES
    ('1.1.2','a.com'),
    ('1.1.3','b.com'),
    ('2.1.4','c.com'),
    ('2.1.5','d.com'),
    ('2.2.1','e.com')        
     ) AS T(VersionNumber, DownloadPath)
ORDER  BY CAST('/' + VersionNumber + '/' AS HIERARCHYID) DESC
like image 32
Martin Smith Avatar answered Oct 27 '22 13:10

Martin Smith