March 3, 2015 at 7:14 am
Hi Team,
I have a description field in a table which also stores unit of measure in the same column but with some space between them, I need to split these into two different columns.
For Eg:
Description
APPEARANCE UNIT
BDV KV
DENSITY KG/L
Can any one suggest with the query to separate these.
Thanks in advance..
March 17, 2015 at 4:18 am
Thanks andy...
We have like after 40 characters the unit of measure is place in the description field. We have more than one space between the description and unit of measure.
March 17, 2015 at 4:36 am
You could replace the multiple spaces with a single space first:
WHILE PATINDEX('% %',@str)>0-- replace multiple spaces with single spaces
BEGIN
SET @STR=REPLACE(@str,' ',' ')
END
So the whole thing would read:
DECLARE @STR CHAR(40)= 'something unit'
WHILE PATINDEX('% %',@str)>0
BEGIN
SET @STR=REPLACE(@str,' ',' ')
END
SELECTSUBSTRING(@Str,1,CHARINDEX(' ',@Str)-1) AS [LeftPart],
SUBSTRING(@Str,(CHARINDEX(' ',@Str)+1),LEN(@Str)) AS [RightPart]
March 17, 2015 at 5:34 am
March 17, 2015 at 6:42 am
sqlquery29 (3/17/2015)
Thanks andy...We have like after 40 characters the unit of measure is place in the description field. We have more than one space between the description and unit of measure.
Does the UOM always begin at exactly the same place?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 17, 2015 at 7:32 am
yes..Chris..
March 17, 2015 at 7:43 am
Then try SUBSTRING(string, startpos, 8000) or calculate the correct length for the third parameter.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 17, 2015 at 7:46 am
.. so why won't a sumple SUBSTRING(@str,from,length) do the job?
(like wot Chris said! ๐ )
March 17, 2015 at 11:21 am
sqlquery29 (3/17/2015)
Thanks andy...We have like after 40 characters the unit of measure is place in the description field. We have more than one space between the description and unit of measure.
Please post a more complete sample of data.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply