Insert NULL or ' ' in SmallDateTimefield

Hursh 191 Reputation points
2022-07-21T12:30:23.12+00:00

When I insert NULL or empty string in a nullable SmallDateTime field; it inserts 1900-01-01 00:00:00
How do I set it to either empty or null?

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Naomi Nosonovsky 8,881 Reputation points
    2022-07-21T12:33:00.64+00:00

    How exactly you're inserting null? NULL should work fine, e.g. insert into myTable (MyDateTimeColumn) values (NULL)

    Empty '' string converts to '1900-01-01' by default.


1 additional answer

Sort by: Most helpful
  1. Olaf Helper 47,581 Reputation points
    2022-07-21T12:36:16.5+00:00

    Insert NULL works as expected and an empty string gets contered to 1900-01-01, it's the smallest possible date value for smalldatetime

    declare @test table(myDate smalldatetime NULL);  
      
    insert into @test (myDate) values (NULL);  
    insert into @test (myDate) values ('');  
      
    select *  
    from @test  
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.