Groups | Blog | Home
all groups > sql server (alternate) > july 2004 >

sql server (alternate) : How to insert NULL char values in SQLSERVER with a SQL sentence?


miguelcampoy NO[at]SPAM hotmail.com
7/29/2004 4:15:15 AM
Hi everyone!
I am working with Delphi v7 and MS SQLServer.
I am trying to insert data in a table with a SQL sentence. Some of the
fields of my table are type char or varchar, and they can have null
values.
¿What do i have to write in the SQL sentence to insert a null value in
those fields?
I tried with '', an empty String, but it doesnt work, the tables
stores an empty String (logical :-)).
In the SQLServer GUI you have to press CTRL + 0 to insert a NULL
value, but how can i tell this to the SQLServer through a SQL
Sentence?

DHatheway
7/29/2004 8:03:21 AM
Given:

create table foo
(col1 char(1) not null,
col2 char(1) null)

you can insert nulls to col2 by either explicitly specifying a null for the
content:

insert foo (col1, col2) values ('a',null)

or by skipping it in the column list and SQL Server will automatically
insert null:

insert foo (col1) values ('b')

Check it:

select * from foo

outputs:

col1 col2
---- ----
a NULL
b NULL


[quoted text, click to view]

Hugo Kornelis
7/29/2004 1:21:30 PM
[quoted text, click to view]

Hi schumacker,

INSERT INTO MyTable (Col1, Col2, Col3)
VALUES (1, NULL, 3)

or

INSERT INTO MyTable (Col1, Col2, Col3)
SELECT 1, NULL, 3

Best, Hugo
--

AddThis Social Bookmark Button