Re: Default value: ISNULL()



You can create a default constraint to specify a default value for a column.
For example:

CREATE TABLE MyTable
(
Col1 int NOT NULL,
Col2 int NULL
CONSTRAINT DF_MyTable_Col1 DEFAULT 0
)
GO
INSERT INTO MyTable (Col1) VALUES(1)
SELECT * FROM MyTable
GO

However, an explicit NULL will override the default constraint value:

INSERT INTO MyTable (Col1, Col2) VALUES(2, NULL)
SELECT * FROM MyTable
GO

If you need to import data containing a mix of nulls and not nulls, you have
options depending on your data source and import tool. In the case of a
query, you could use ISNULL or COALESCE to specify the desired value when
NULL. With DTS, a column transformation could do the job.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Peter Neumaier" <Peter.Neumaier@xxxxxxxxx> wrote in message
news:1138013760.433105.245790@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hi!
>
> I'm wondering whether it's possible to set up the MS SQL function
> ISNULL() as a default value to avoid NULL entries when importing data
> into a table?!
>
> For example, I want the column1, to have a 0 (zero) as default value,
> when entering/importing data: isnull("column1",0)
>
> I remember that it is possible to set up with a date function like
> now(), having for each record the current time as default value. Is
> that also with isnull() somehow possible?
>
> Thx a lot!
> Peter
>


.



Relevant Pages

  • Re: Difference between semivowel and consonant
    ... would seem to specify only how the tongue is oriented relative to the ... than there is such a constraint on its fricative counterpart SAMPA ... So there is an implicit contraint to the position of the lips? ... there are many examples both of vowels and of consonants undergoing ...
    (sci.lang)
  • Re: OReilly interview with Date
    ... >>OK but if you have to specify the foreign key, ... constraint for every join you might want to do, ... (How many other salespersons have total ...
    (comp.databases.theory)
  • Re: what are keys and surrogates?
    ... never mind manipulation of ASTs. ... avoid the need to introduce lots of meaningless identifiers. ... specify a constraint on a variable, ...
    (comp.databases.theory)
  • Re: simple ALTER TABLE question
    ... and the NOT NULL constraint added at column creation time makes sure ... "Michael C" wrote in message ... Finally, you should specify ...
    (microsoft.public.sqlserver.programming)
  • Re: Changing Default Names
    ... >>must specify the owner name when referencing the object. ... >>SQL Server MVP ... >>>>Is it possible the constraint has a different owner ...
    (microsoft.public.sqlserver.server)