Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I skip a column entry on INSERT when a value is blank in SQL?

Tags:

sql

sql-server

I'm inserting lots of rows into a database and some of the columns are blank for some of the rows.

How can I insert without assigning a dummy value to these blank fields?

       1 INSERT Leads VALUES('name', 'cityName', 5, 'anotherValue')
       2 INSERT Leads VALUES('name', 'cityName',  , 'anotherValue')
       3 INSERT Leads VALUES('name', 'cityName', 2, 'anotherValue')
       4 INSERT Leads VALUES('name', 'cityName', 9, 'anotherValue')

My problem lies in row 2 where there is a blank value in between city name and another value. Ideally, I'd like the value to remain null.

Any help is appreciated.

Thanks!

like image 527
Barrett Kuethen Avatar asked Oct 07 '11 18:10

Barrett Kuethen


1 Answers

You should always explicitly specify which columns you're inserting to - then you can leave out those you don't want:

INSERT INTO dbo.Leads(Col1, Col2, Col4) VALUES('name', 'cityName', 'anotherValue')

(leaving out Col3 here in this example)

like image 73
marc_s Avatar answered Dec 14 '22 19:12

marc_s