Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SELECT INTO a null literal of a specific type

How do I SELECT INTO a NULL value of a particular type?

For example, backing up data selectively and incorporating a DateFixed column that will start NULL and later be populated via GetUtcDate():

SELECT
    Thing.Id,
    Thing.SomeValue AS "BadValue",
    NULL AS "DateFixed"
INTO ThingValueFixBackup
FROM Thing
WHERE SomeValue IS WRONG SOMEHOW;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

GETUTCDATE returns a datetime

So you can use

CAST(NULL AS DATETIME) AS DateFixed

to get a column of that type created with your SELECT ... INTO.

But you should consider using datetime2/SYSUTCDATETIME instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading