Gossamer Forum
Home : General : Databases and SQL :

Help with Substring/Trim to split data into two new columns

Quote Reply
Help with Substring/Trim to split data into two new columns
This is killing me... I'm fairly familiar with TSQL but I'm by no means a DBA or even close...



My issue is that I have a column with blood pressure values in it, the values are separated by a '/' to the left are 2 or 3 numbers and the same to the right.



I need to basically just strip what's before the '/' and put those #'s in their own column, and what's after the '/' and put those #'s in their own column.


Column 'BP' has values xxx/xxx or xx/xxx or xx/xx or xxx/xx and is nvarchar.


Unsure

Thanks for your help!

Last edited by:

netghost: Apr 11, 2003, 6:50 AM
Quote Reply
Re: [netghost] Help with Substring/Trim to split data into two new columns In reply to
You don't mention which database you are using, its probably not MySQL, but in MySQL this would be accomplished by:

UPDATE tablename SET
leftcolumn = LEFT(BP, LOCATE('/', BP) - 1),
rightcolumn = RIGHT(BP, LOCATE('/', BP) + 1));

Have a look at similiar string functions in your db.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Help with Substring/Trim to split data into two new columns In reply to
Alex,



Thanks!

I am using SQL 2000... So I believe I can accomplish this by using:

UPDATE tablename SET

leftcolumn = LEFT(BP, CHARINDEX('/', BP) - 1),

rightcolumn = RIGHT(BP, CHARINDEX('/', BP) + 1));

Does this look right to you?



Thank you so much.
Quote Reply
Re: [netghost] Help with Substring/Trim to split data into two new columns In reply to
Alex,



Thanks! Fyi in case anyone ever needs this in the future.. here is the code that I used that worked w/ MS SQL 2000:



UPDATE tablename SET

leftcolumn = SUBSTRING(originalcolumn, 1, CHARINDEX('/', originalcolumn) - 1),

rightcolumn = SUBSTRING(originalcolumn, CHARINDEX('/', originalcolumn) + 1, LEN(originalcolumn))



This took a blood pressure of 120/80 in originalcolumn and separated values into left and right columns per diastolic and systolic levels.



Thanks again for your help!