November 21, 2012 at 9:38 am
Hello Everyone
I hope that everyone is having a great day.
I am working with some horrible data, and I need a little help with one piece of data. The string is this, all of it the same, except for the verbiage of course.
9279-1^Respiratory Rate^LOINC
I need to get that separated, but I do not care about the last section of :
LOINC
The separator is the caret "^", I simply need the data like this:
9279-1
and
Respiratory Rate
I am inserting this data into two separate columns.
I greatly appreciate any and all advice and help with this.
Andrew SQLDBA
November 21, 2012 at 10:15 am
I have tried that, I was looking for something to simply be used to update a couple columns, but keep the original.
Thanks
Andrew SQLDBA
November 21, 2012 at 10:30 am
Are you sure? This might give you a better idea.;-)
SELECT string,
MAX( CASE WHEN itemNumber = 1 THEN item END) AS firstpart,
MAX( CASE WHEN itemNumber = 2 THEN item END) AS secondpart
FROM ( SELECT '9279-1^Respiratory Rate^LOINC' string) a
CROSS APPLY dbo.DelimitedSplit8K( a.string, '^') split
GROUP BY string
November 21, 2012 at 10:34 am
Agreed the 8K splitter refrenced by Luis Cazares is great, however for something as simple as your needs, you might want to try the following along with the 8K splitter to determine which performs the best in your situation.
DECLARE @U VARCHAR(100)
SET @U = '9279-1^Respiratory Rate^LOINC'
DECLARE @a INT
DECLARE @b-2 INT
-- To demonstrate what is taking place with neither @a and @b-2 used in the actual solution
SET @a = CHARINDEX('^',@U,1)
SET @b-2 = CHARINDEX('^',@U,@A+1)
-- The following 2 selects are just to demonstrate what is happening
-- SELECT SUBSTRING(@U,1,@A-1) AS 'Answer1',SUBSTRING(@U,@A+1,@B-@A-1)
-- SELECT SUBSTRING(@U,1,CHARINDEX('^',@U,1)-1) AS 'Answer1',SUBSTRING(@U,@A+1,@B-@A-1)
-- After all the demo code the finall answer is:
SELECT SUBSTRING(@U,1,CHARINDEX('^',@U,1)-1) AS 'Answer1',SUBSTRING(@U,@A+1,CHARINDEX('^',@U,@A+1)-@A-1)
Results;
Answer1(No column name)
9279-1Respiratory Rate
November 21, 2012 at 10:34 am
-- place to test the insert
DECLARE @targetTable TABLE (val1 varchar(30), val2 varchar(30));
-- the value fed to your query
DECLARE @splitMe varchar(40) = '9279-1^Respiratory Rate^LOINC';
;WITH x(a,b) AS
(SELECTLEFT(@splitMe, CHARINDEX('^',@splitMe)-1),
RIGHT(@splitMe, LEN(@splitMe)-CHARINDEX('^',@splitMe))
)
INSERT INTO @targetTable
SELECT a, LEFT(b, CHARINDEX('^',b)-1) FROM x;
SELECT * FROM @targetTable
-- Itzik Ben-Gan 2001
November 21, 2012 at 10:56 am
bitbucket-25253 (11/21/2012)
Agreed the 8K splitter refrenced by Luis Cazares is great, however for something as simple as your needs, you might want to try the following along with the 8K splitter to determine which performs the best in your situation.
Agreed, it's important to test what's best for each situation.
November 21, 2012 at 5:56 pm
Thanks Everyone
I had to take a little from each query to get what I needed, in a timely manner. I had over 500 million rows to update. But it was very fast.
Andrew SQLDBA
November 21, 2012 at 7:54 pm
AndrewSQLDBA (11/21/2012)
Thanks EveryoneI had to take a little from each query to get what I needed, in a timely manner. I had over 500 million rows to update. But it was very fast.
Andrew SQLDBA
So what did you finally end up with? Can you post the code please?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply