Sampath Sampath - 5 months ago 22
SQL Question

Cannot insert the value NULL into column CreationTime

Runtime Exception :


Cannot insert the value NULL into column 'CreationTime', table
'MyTables'; column does not allow nulls. INSERT
fails.


Code:

INSERT INTO [MyTables] (LegacyId, CreationTime)
SELECT DISTINCT
a.[IPLID], a.[inputdate]
FROM
[Legacy].[dbo].[MyTables2] AS a


Can you tell me how to insert custom date like
01/01/2000
when
a.[inputdate]
is
Null
?

Answer

just wrap in an ISNULL:

INSERT INTO [MyTables] (LegacyId,CreationTime)
SELECT DISTINCT a.[IPLID],ISNULL(a.[inputdate], '01/01/2000')
FROM [Legacy].[dbo].[MyTables2] as a