6

I'm trying to perform some upgrade maintenance on our database. I need to move 3 columns of data from all rows of one table and insert that data as new rows in a new table.

INSERT INTO [dbo].[SnmpSettings]([NetworkDiscoveryId], [RoCommunities], [RwCommunities])
    SELECT id, Ro_Community, RW_Communities
    FROM [dbo].[Network_Discovery] 

The above code would work fine, but Ro_Community and RW_Communities allow NULL where as RoCommunities and RwCommunities do not allow NULL. How should I convert NULLs to the empty string and then insert into my new table?

EDIT:

INSERT INTO [dbo].[SnmpSettings]([NetworkDiscoveryId], [RoCommunities], [RwCommunities])
    SELECT id, Ro_Community, RW_Communities
    ISNULL(Ro_Community,'')
    FROM [dbo].[Network_Discovery] 


Msg 102, Level 15, State 1, Line 27
Incorrect syntax near 'Ro_Community'.
3
  • 3
    If it is Sql Server, use isnull(Ro_Community, '') Commented Jun 1, 2012 at 23:50
  • I apologize for how naive I am in this bit of work. I've edited in my attempt to follow your instruction, but it does not compile. Commented Jun 1, 2012 at 23:53
  • SELECT id, isnull(Ro_Community, ''), isnull(RW_Communities, '') Commented Jun 1, 2012 at 23:54

1 Answer 1

17
SELECT ISNULL(Ro_Community, '')

or

SELECT COALESCE(Ro_Community, '')

Note: COALESCE is supported since SQL Server 2005. It is part of the ANSI-92 SQL standard, so many suggest it's preferred over ISNULL. However there are also reports that it is a bit slower.

Sign up to request clarification or add additional context in comments.

2 Comments

ISNULL was way faster for me.
@TotZam That's what I said, COALESCE is said to be slower

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.