5

I have a requirement to insert the literal value 'NULL' into a nvarchar column as a default value. But when I insert records the default is going as db NULL instead of the literal 'NULL'. Can someone tell me where I am going wrong?

3
  • 2
    Incidentally, what is the purpose of defaulting a column to 'NULL' instead of NULL? This sounds like a questionable decision to me. Commented Nov 3, 2011 at 20:03
  • @djacobson - The requirement for us to send 'NULL' is because the data we send it out is consumed by another application which needs to see the text NULL rather than an empty tag. Commented Nov 5, 2011 at 22:02
  • Ah well, what can you do? Fair enough. :S Commented Nov 5, 2011 at 23:04

3 Answers 3

5

So you want the actual string of "NULL" to be inserted? Try something like this:

create table NullTable
(
    nvarchar(100) not null default 'NULL',
    .... -- your other fields
)

EDIT: Here is a full solution

create table InsertNull
(
    Nullfield nvarchar(100) not null default 'NULL',
    someint int not null
)
go

insert into insertnull(someint)
select 20

select *
from InsertNull
Sign up to request clarification or add additional context in comments.

3 Comments

shark- I do have a table with the same setup. In the same example say our table contains two fields field1 with default of 'NULL', field2 INT. if I run the query say - insert into nulltable (field1) select 20 then field1 is still showing as NULL DB value
@VKarthik check out my edit. I just ran this and it is showing the string 'NULL' on the result query.
@VKarthik are you looking to have the single quotes stored in the column too? Because what Shark has will have the default of a string NULL not DB NULL.
3

Note the quotes:

INSERT INTO yourtable (varcharfield) VALUES ('NULL'); // string null
INSERT INTO yourtable (varcharfield) VALUES (NULL); // actual null

1 Comment

I think the OP wants the default value of the field to be the string 'NULL', not just inserting the value on an INSERT statement.
1

If set the default in code instead of the designer is should work.

ALTER TABLE [myTable] ADD  DEFAULT ('NULL') FOR [TextColumn]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.