split sql column into two with delimiter

I have a column in a table (named 'docksTemp) that has dimensional data in it, like this 12x18

I need it seperated into two columns and drop the 'x'

initial column name is Dimensions, I want that parsed out to DimensionWidth and DimensionLength

I use these two commands:

UPDATE docksTemp
SET DimensionWidth = LEFT(Dimensions,CHARINDEX('x',Dimensions)-1)

UPDATE docksTemp
SET DimensionLength = RIGHT(Dimensions,LEN(Dimensions)-CHARINDEX('x',Dimensions))

The length portion of it works, it seperates the data no problem.  the first part with the width doesn't work I get a

ERROR: Invalid length parameter passed to the LEFT or SUBSTRING
function. Error Code: 537

If I use the same command but add a where clause and just do one statement at a time that create the correct results.  So what I think is its having problems when there might be a field with no data in it?  I can I get it deal with that?

thanks
Baub
Baub EisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Sounds like you need a CASE block to test for non-nulls and 'x' values.  Copy-paste the below into your SSMS, execute it to verify it works as you need, then modify to fit your situation
-- This returns values 12 and 18
Declare @x varchar(10) = '12x18'

-- These will return no value, and will not error
-- Declare @x varchar(10) = '12', 'banana', NULL

SELECT 
   CASE WHEN LEN(ISNULL(@x, '')) > 0 AND CHARINDEX('x', @x) > 1 THEN LEFT(@x,CHARINDEX('x',@x)-1)
   ELSE '' END as the_height

SELECT 
   CASE WHEN LEN(ISNULL(@x, '')) > 0 AND CHARINDEX('x', @x) > 1 THEN RIGHT(@x,LEN(@x)-CHARINDEX('x',@x))
   ELSE '' END as the_width

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Baub EisAuthor Commented:
That did the trick thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.