March 4, 2016 at 9:43 am
I am trying to get a substring of characters but need to go from right
Example 9.0.0832.1032 need to get the 32 (one after 08).
10.01.0832.1064 need to get the 32 (one after 08).
Please advice
March 4, 2016 at 10:00 am
sharonsql2013 (3/4/2016)
I am trying to get a substring of characters but need to go from rightExample 9.0.0832.1032 need to get the 32 (one after 08).
10.01.0832.1064 need to get the 32 (one after 08).
Please advice
Will these strings always have the same format? Meaning four group of numbers divided by points?
What defines 08? What happens if there's no 08?
March 4, 2016 at 10:00 am
REVERSE the string, use SUBSTRING, and then REVERSE it again.
John
March 4, 2016 at 10:01 am
There's a nifty builtin function called PARSENAME that splits a string like an Application Versions, IP addresses or Servername.Databasename.SchemaName.TableName into it's parts.
you can use that to isolate the second section from the right, and then substring the section like this:
With MyCTE(val)
AS
(
SELECT '9.0.0832.1032' UNION ALL --need to get the 32 (one after 08).
SELECT '10.01.0832.1064'
)
SELECT val,parsename(val,2) As Sectn,RIGHT(parsename(val,2),2) TargetString FROM MyCTE
/*
val Sectn TargetString
--------------- -------- ------------
9.0.0832.1032 0832 32
10.01.0832.1064 0832 32
*/
Lowell
March 4, 2016 at 11:50 am
John Mitchell-245523 (3/4/2016)
REVERSE the string, use SUBSTRING, and then REVERSE it again.John
Got code?
--Jeff Moden
Change is inevitable... Change for the better is not.
July 20, 2016 at 10:16 am
Jeff Moden (3/4/2016)
John Mitchell-245523 (3/4/2016)
REVERSE the string, use SUBSTRING, and then REVERSE it again.John
Got code?
(Didn't see this response when it was posted.)
No, I haven't. I've suggested to the original poster how she might do it. I'm sure if she were struggling with any aspect of it she'd have posted back for more help. Lowell's solution looks better than mine, in any case.
John
July 20, 2016 at 10:28 am
volodymyrlev228 (7/20/2016)
What about reverse the string, and then, reverse the string again? It finally helped me to
--URL REMOVED.
This sure looks a LOT like spam. Care to explain why that link is in your answer??
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 20, 2016 at 11:43 am
John Mitchell-245523 (7/20/2016)
Jeff Moden (3/4/2016)
John Mitchell-245523 (3/4/2016)
REVERSE the string, use SUBSTRING, and then REVERSE it again.John
Got code?
(Didn't see this response when it was posted.)
No, I haven't. I've suggested to the original poster how she might do it. I'm sure if she were struggling with any aspect of it she'd have posted back for more help. Lowell's solution looks better than mine, in any case.
John
Understood. The reason why I was asking is because REVERSE is kind of expensive as functions go. There's normally a way to do what is asked with a single reverse and wanted to see what you were doing so I could make an alternate suggestion. Thanks, John.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 20, 2016 at 1:04 pm
Jeff Moden (3/4/2016)
John Mitchell-245523 (3/4/2016)
REVERSE the string, use SUBSTRING, and then REVERSE it again.John
Got code?
Something like this?
With MyCTE(val)
AS
(
SELECT '9.0.0832.1032' UNION ALL --need to get the 32 (one after 08).
SELECT '10.01.0832.1064'
)
SELECT SUBSTRING(REVERSE(SUBSTRING(REVERSE(val),1,len(val)-(CHARINDEX('.08',(val),1)))),3,2) FROM MyCTE
July 20, 2016 at 8:48 pm
Smendle (7/20/2016)
Jeff Moden (3/4/2016)
John Mitchell-245523 (3/4/2016)
REVERSE the string, use SUBSTRING, and then REVERSE it again.John
Got code?
Something like this?
With MyCTE(val)
AS
(
SELECT '9.0.0832.1032' UNION ALL --need to get the 32 (one after 08).
SELECT '10.01.0832.1064'
)
SELECT SUBSTRING(REVERSE(SUBSTRING(REVERSE(val),1,len(val)-(CHARINDEX('.08',(val),1)))),3,2) FROM MyCTE
Kind of but I'm pretty sure the presence of the .08 isn't reliable.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 21, 2016 at 2:07 am
Jeff Moden (7/20/2016)
John Mitchell-245523 (7/20/2016)
Jeff Moden (3/4/2016)
John Mitchell-245523 (3/4/2016)
REVERSE the string, use SUBSTRING, and then REVERSE it again.John
Got code?
(Didn't see this response when it was posted.)
No, I haven't. I've suggested to the original poster how she might do it. I'm sure if she were struggling with any aspect of it she'd have posted back for more help. Lowell's solution looks better than mine, in any case.
John
Understood. The reason why I was asking is because REVERSE is kind of expensive as functions go. There's normally a way to do what is asked with a single reverse and wanted to see what you were doing so I could make an alternate suggestion. Thanks, John.
Ah yes, you can use CHARINDEX and just one REVERSE. Much better.
John
July 21, 2016 at 12:45 pm
John Mitchell-245523 (7/21/2016)
Ah yes, you can use CHARINDEX and just one REVERSE. Much better.
'Zactly! 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
August 3, 2016 at 6:26 am
Jeff Moden (7/20/2016)
Smendle (7/20/2016)
Jeff Moden (3/4/2016)
John Mitchell-245523 (3/4/2016)
REVERSE the string, use SUBSTRING, and then REVERSE it again.John
Got code?
Something like this?
With MyCTE(val)
AS
(
SELECT '9.0.0832.1032' UNION ALL --need to get the 32 (one after 08).
SELECT '10.01.0832.1064'
)
SELECT SUBSTRING(REVERSE(SUBSTRING(REVERSE(val),1,len(val)-(CHARINDEX('.08',(val),1)))),3,2) FROM MyCTE
Kind of but I'm pretty sure the presence of the .08 isn't reliable.
I know it was a littoral solution but I think the point was made...
This should get you the same result but be after the second '.' if it will only ever be at min 4 characters from the left.
SELECT SUBSTRING(REVERSE(SUBSTRING(REVERSE(val),1,len(val)-(CHARINDEX('.',(val),4)))),3,2) FROM MyCTE
August 3, 2016 at 8:37 am
Smendle (8/3/2016)
Jeff Moden (7/20/2016)
Smendle (7/20/2016)
Jeff Moden (3/4/2016)
John Mitchell-245523 (3/4/2016)
REVERSE the string, use SUBSTRING, and then REVERSE it again.John
Got code?
Something like this?
With MyCTE(val)
AS
(
SELECT '9.0.0832.1032' UNION ALL --need to get the 32 (one after 08).
SELECT '10.01.0832.1064'
)
SELECT SUBSTRING(REVERSE(SUBSTRING(REVERSE(val),1,len(val)-(CHARINDEX('.08',(val),1)))),3,2) FROM MyCTE
Kind of but I'm pretty sure the presence of the .08 isn't reliable.
I know it was a littoral solution but I think the point was made...
This should get you the same result but be after the second '.' if it will only ever be at min 4 characters from the left.
SELECT SUBSTRING(REVERSE(SUBSTRING(REVERSE(val),1,len(val)-(CHARINDEX('.',(val),4)))),3,2) FROM MyCTE
You don't need the overhead of two REVERSEs though. In fact, if the presence of 4 elements separate by 3 periods is consistent, you don't need REVERSE or substring at all. You can use PARSENAME to quickly to the job as Lowell did.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 5, 2016 at 9:08 pm
Jeff Moden (8/3/2016)
Smendle (8/3/2016)
Jeff Moden (7/20/2016)
Smendle (7/20/2016)
Jeff Moden (3/4/2016)
John Mitchell-245523 (3/4/2016)
REVERSE the string, use SUBSTRING, and then REVERSE it again.John
Got code?
Something like this?
With MyCTE(val)
AS
(
SELECT '9.0.0832.1032' UNION ALL --need to get the 32 (one after 08).
SELECT '10.01.0832.1064'
)
SELECT SUBSTRING(REVERSE(SUBSTRING(REVERSE(val),1,len(val)-(CHARINDEX('.08',(val),1)))),3,2) FROM MyCTE
Kind of but I'm pretty sure the presence of the .08 isn't reliable.
I know it was a littoral solution but I think the point was made...
This should get you the same result but be after the second '.' if it will only ever be at min 4 characters from the left.
SELECT SUBSTRING(REVERSE(SUBSTRING(REVERSE(val),1,len(val)-(CHARINDEX('.',(val),4)))),3,2) FROM MyCTE
You don't need the overhead of two REVERSEs though. In fact, if the presence of 4 elements separate by 3 periods is consistent, you don't need REVERSE or substring at all. You can use PARSENAME to quickly to the job as Lowell did.
I was specifically answering this question from you.... It was NOT meant to be a solution per se...
John Mitchell-245523 (3/4/2016)
REVERSE the string, use SUBSTRING, and then REVERSE it again.
John
Got code?
--Jeff Moden
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply