October 18, 2006 at 6:51 am
All,
I am trying to split a string down into 2 seperate strings using TSQL.
i.e.
name
Acrobat 7.0 Professional
DemoShield 7.5
name version
Acrobat 7.0 Professional
DemoShield 7.5
I basically want to split the first occurence of a number after a space in a string into 2 seperate strings.
Any help would be appreciated
Regards
Carl
October 18, 2006 at 7:11 am
RTRIM(LEFT([column],PATINDEX('%[0-9]%',[column])-1)) AS [name]
SUBSTRING([column],PATINDEX('%[0-9]%',[column]),255) AS [version]
Is it possible for the string not to have a number?
Far away is close at hand in the images of elsewhere.
Anon.
October 18, 2006 at 7:14 am
or use
RTRIM(LEFT([column],PATINDEX('% [0-9]%',[column])-1)) AS [name]
SUBSTRING([column],PATINDEX('% [0-9]%',[column]),255) AS [version]
if you want to include a space before the number in case the name portion contains numbers
Far away is close at hand in the images of elsewhere.
Anon.
October 18, 2006 at 7:25 am
I'm sorry David but I just don't see any difference between the 2 statements. Care to highlight it for me?
October 18, 2006 at 7:43 am
I had the same problem, but after a while I found the difference
RTRIM(LEFT([column],PATINDEX('%[0-9]%',[column])-1)) AS [name]
RTRIM(LEFT([column],PATINDEX('% [0-9]%',[column])-1)) AS [name]
Do you see the difference now that both statements are immediately adjacent, Remi? There is one extra space after the % sign.
Reminds me of "Where is Wally??" cartoons
October 18, 2006 at 7:51 am
Thanx Vlad.
Now here's the interesting part. How is david gonna highlight that space in red without using an underscore ?
October 18, 2006 at 7:53 am
RTRIM(LEFT([column],PATINDEX('%[0-9]%',[column])-1)) AS [name]
RTRIM(LEFT([column],PATINDEX('% [0-9]%',[column])-1)) AS [name]
October 18, 2006 at 7:53 am
Ooops! Sorry about that
Far away is close at hand in the images of elsewhere.
Anon.
October 18, 2006 at 8:02 am
![]() | ...highlight that space in red ... |
didn't think of that
Far away is close at hand in the images of elsewhere.
Anon.
October 18, 2006 at 8:04 am
Ok that was too easy for you guys .
October 18, 2006 at 8:58 am
Guys,
I get the following error when running the following command:
RTRIM(LEFT([name],PATINDEX('% [0-9]%',[name])-1)) AS [name],
SUBSTRING([name],PATINDEX('% [0-9]%',[name]),255) AS [version],
Invalid length parameter passed to the substring function.
Regards
Carl
October 18, 2006 at 9:15 am
It works for me, so it will probably be something in your data structure.
What is the length and datatype of the column you want to parse?
declare @name varchar (255)
set @name = 'SM Widows 3.1'
select RTRIM(LEFT(@name,PATINDEX('% [0-9]%',@name)-1)) AS [name]
select SUBSTRING(@name,PATINDEX('% [0-9]%',@name),255) AS [version]
name
---------------------------------------------------------------------------
SM Widows
(1 row(s) affected)
version
---------------------------------------------------------------------------
3.1
(1 row(s) affected)
If I change @name to varchar(10), I get precisely the same error message you did - so it will probably be something along this line.
October 18, 2006 at 9:19 am
The name column is VARCHAR(255).
October 18, 2006 at 9:24 am
You will get the error if the column does not contain a number preceeded by a space
if so try this
LEFT([name],PATINDEX('% [0-9]%',[name]+' 1')-1) AS [name],
SUBSTRING([name],PATINDEX('% [0-9]%',[name]+' 1')+1,255) AS [version]
Note the spaces (I did not colour them red)
Far away is close at hand in the images of elsewhere.
Anon.
October 18, 2006 at 9:29 am
I don't mind for a single script. But it was hard to figure out the difference when they were not side by side .
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy