Link to home
Start Free TrialLog in
Avatar of Whah
Whah

asked on

How can I copy a SQL column to another column and ignore nulls?

Hi,

I need to copy all of the contents from one column to another. I'm running into an issue with the one that I'm using because there are nulls in the column that I am copying the data from.

Thanks,

Whah
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

UPDATE your_table
SET some_column = some_other_column

>I'm running into an issue with the one that I'm using because there are nulls in the column
So ... what's the issue?  If some_column is set to NOT NULL, then you can use ISNULL() to convert them to some other value

UPDATE your_table
SET some_column = ISNULL(some_other_column, '')
Avatar of Whah
Whah

ASKER

Hi Jim,

The new column is set to null, but when I run the same command that you mention I get:

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.

Thanks,

Brian
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Whah

ASKER

Hi Jim,

I was able to accomplish what I needed done by using

UPDATE my_table
SET date = CAST([field2] AS DATE)
WHERE ISDATE([field2]) = 1

Thanks for your help.

Whah
Thanks for the grade.  Good luck with your project.  -Jim