Rohin
Rohin

Reputation: 526

Inserting Multiple values into a single null columns using sql

I've to Update a table which has two columns and 300 rows

In which One column is filled already and the second column is Null.

How to Update the Null column at once using a query.

What I've tried is

UPDATE tablename
SET Column_2 = (
        CASE column_2
            WHEN 'NULL'
                THEN 'Value1'
            WHEN 'NULL'
                THEN 'Value2'...
            WHEN 'NULL'
                THEN 'Value300'
            ELSE column_2
            END
        )

When i run this query, editor said (300 rows affected)

But when i look into the table, it's not updated

Thanks

Upvotes: 0

Views: 63

Answers (2)

John Herman
John Herman

Reputation: 47

Try something like this. Also SQL doesn't recognize 'NULL' as a string when you are referencing it. A true NULL value is called NULL without the single quotes.

UPDATE tablename
SET colum_2 = COALESCE('Value1','Value2','Value300',S_Type)

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270653

I think you can do what you want by enumerating the rows and then using the enumeration:

with toupdate as (
      select t.*, row_number() over (order by column_1) as seqnum
      from tablename t
     )
update tablename
    set colum_2 = (case when seqnum = 1 then 'Value1'
                        when seqnum = 2 then 'Value2' 
                                           .
                                           .
                                           .
                        when seqnum = 300 then 'Value300'
                        else s_type 
                    end);

Upvotes: 3

Related Questions