Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Find integer at end of a string. There *must* be a better solution ?


Find integer at end of a string. There *must* be a better solution ?

Author
Message
Erland Sommarskog
Erland Sommarskog
SSC Eights!
SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)

Group: General Forum Members
Points: 937 Visits: 866
Thorkil Johansen (7/27/2013)To sharpen a little: "The integer to the right of the last hyphen in the string"


In such case...

with tmp as (
select x = 'some text-1'
union select x = 'some text-123'
union select x = 'some text-123.4'
union select x = 'some text 123'
union select x = 'some text-'
union select x = 'some text'
union select x = 'some text-xyz'
union select x = 'some text-2.2-44'
)
select * ,
coalesce( TRY_CONVERT(int,
nullif(SUBSTRING(x, len(x) - nullif(CHARINDEX('-', reverse(x)), 0) + 2, LEN(x)),
'')),
-1)
from tmp



Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Thorkil Johansen
Thorkil Johansen
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 50
Hi

Allow me to wake this thread up again...

When using this:

select ISNULL(TRY_CONVERT(INT,SUBSTRING('Ejerskifte-45\Behandlet-45',
NULLIF(PATINDEX('%-[0-9]%','Ejerskifte-45\Behandlet-45'),0)
+ 1,LEN('Ejerskifte-45\Behandlet-45')) ),-1)


I get -1 I would like it to be 45
TomThomson
TomThomson
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10714 Visits: 12017
Thorkil Johansen (8/29/2013)

select ISNULL(TRY_CONVERT(INT,SUBSTRING('Ejerskifte-45\Behandlet-45',
NULLIF(PATINDEX('%-[0-9]%','Ejerskifte-45\Behandlet-45'),0)
+ 1,LEN('Ejerskifte-45\Behandlet-45')) ),-1)


I get -1 I would like it to be 45

The problem is that trying to convert the 26 character string '45\Behandlet-45' to INT delivers NULL. You need to hand the the right string to TRY_CONVERT. It isn't clear which of the two occurrences of '45'in the original string you want (or even whether it matters which you get - if the numeric is repeated twice in each sting you want to do this with, as it is in this example, it doesn't matter which string you get); if it's the first one, you can correct the length using another call of PATINDEX to find the first not --9 character after the start you've selected, and then doing some arithmetic; if you want something that's at the end of the string, there are suggestions earlier in this topic for finding the right start position and the length you use in the call to substring can be the length of the whole original string since you are going to the end.

Tom

Erland Sommarskog
Erland Sommarskog
SSC Eights!
SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)

Group: General Forum Members
Points: 937 Visits: 866
The solution I posted works for this string as well:

with tmp as (
select x = 'some text-1'
union select x = 'some text-123'
union select x = 'some text-123.4'
union select x = 'some text 123'
union select x = 'some text-'
union select x = 'some text'
union select x = 'some text-xyz'
union select x = 'some text-2.2-44'
union select x = 'Ejerskifte-45\Behandlet-45'
)
select * ,
coalesce( TRY_CONVERT(int,
nullif(SUBSTRING(x, len(x) - nullif(CHARINDEX('-', reverse(x)), 0) + 2, LEN(x)),
'')),
-1)
from tmp



Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Thorkil Johansen
Thorkil Johansen
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 50
Thanx for a nice answer...

And thanx for a nice blog as well :-)

Hejsa TreKroner... :-)

/T
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search