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
Microsoft SQL Server 2008

Avatar of undefined
Last Comment
Jim Horn

8/22/2022 - Mon
Jim Horn

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, '')
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
Jim Horn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Jim Horn

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