How to insert NULL into the DATETIME coulmn instead 1900-01-01 00:00:00.000 in SQL Server

show empty string when date field is 1/1/1900 in sql
sql server isnull returning 1900-01-01 when field is null
sql server default date 1/1/1900
how to insert blank date in sql
how to update null value in datetime field in sql server
sql server default null date
sql null date
cast null to date sql server

I am new to SQL Server here I am trying to insert date into the column which has DATETIME as datatype .

When I pass a datetime to the stored procedure from API it insert the values which I have passed. But If I pass an empty string " ", it inserts a default value of 1900-01-01 00:00:00.000. But I want to insert NULL if the data is " ".

I googled a lot but I can't find a solution for this .

@DATEVAL1 DATETIME = '',
@DATEVAL1 DATETIME = ''

INSERT INTO TABLE (mDate1, mDate2)
VALUES (@DATEVAL1, @DATEVAL2);

This is the code I have in my stored procedure.

When you set a datetime variable to an empty string an implicit conversion happens. And an empty string converts implicitly to 1900-01-01. Either just declare the variable and don't assign them a value or explicitly set them to NULL.

@DATEVAL1 DATETIME,
@DATEVAL2 DATETIME = NULL

Or you could use NULLIF. Since you are receiving these values from an API you would most likely have to use NULLIF. Otherwise a change to the API call would be required to send in a NULL instead of an empty string.

Values( NULLIF(@DATEVAL1, '')

SQL Server “DATE” and “DATETIME” data types store date and time values in a This will insert a NULL value for the column, since while creating the table I SELECT DateEdited = CASE WHEN DateEdited = '1900-01-01 00:00:00.000'  I am new to SQL Server here I am trying to insert date into the column which has DATETIME as datatype . When I pass a datetime to the stored procedure from API it insert the values which I have passed. But If I pass an empty string " ", it inserts a default value of 1900-01-01 00:00:00.000. But I want to insert NULL if the data is " ".

Dates and strings are not the same thing. For some reason, SQL Server converts an empty string into the date value '1900-01-01 00:00:00.000' rather than generating an error.

My primary advice is to not use empty strings for dates. Instead, just use NULL:

DECLARE @DATEVAL1 DATETIME = NULL;
DECLARE @DATEVAL1 DATETIME = NULL;

INSERT INTO TABLE (mDate1, mDate2)
    VALUES (@DATEVAL1, @DATEVAL2);

If for some reason you have to use empty strings, then you can use a trigger to convert the 1900 value to NULL or use NULLIF() in the INSERT:

INSERT INTO TABLE (mDate1, mDate2)
    VALUES (NULLIF(@DATEVAL1, ''), NULLIF(@DATEVAL2, ''));

You can't get an empty string because you're returning the DATE value type from ISNULL . Instead, use: An empty string, on the other hand, evaluates to 0 , which in SQL Server is a value for DOB when it is populated and remove the null value when there is no Otherwise, I was getting 1900-01-01 00:00:00.000 . Insert "1900-01-01 00:00:00.000 instead of the empty string to datetime parameter or datetime column NULL value in a DATETIME field in SQL server 2008 via vb

I will go on a different direction as the other two answers.

My guess is you have an APP with some dropdown combos or some text fields. Your APP call your API and then you send those values to the stored procedure. Sometimes the app send empty string and when you send those to the stored procedure are converted to 1900-01-01.

So if you have the following stored procedure:

CREATE PROCEDURE dbo.InsertDate 
    @DATEVAL1 DATETIME, @DATEVAL1 DATETIME
AS
    INSERT INTO TABLE (mDate1, mDate2) 
    VALUES (@DATEVAL1, @DATEVAL2);
GO

In this case if you send '' to the stored procedure you will get 1900-01-01 at the moment the parameters are created because an implicit conversion occurs so NULLIF() won't work here.

You can add some IF () logic to the stored procedure to convert any 1900-01-01 to NULL before run the INSERT

Or you can add the logic to your API converting any '' to NULL before calling the stored procedure.

Allow null values in the table and do not pass any parameter while Go through Enter Null Values for DateTime Column of SQL Server[^]. 1900-01-01 00:00:00.000 " How to insert a NULL value in a DATETIME field in SQL server 2008 via vb.net code How to Store Null Values in sql server 2008 in

zero (0) is SQL's starting date of 1900-01-01, so i suspect that either your a CREATE TABLE statement and INSERT INTO statement into that Something in the query or the display is, or the column is a varchar and = '' (rather than null), Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci) Inserting a null value to the DateTime Field in SQL Server is one of the most common issues giving various errors. Even if one enters null values the value in the database is some default value as 1/1/1900 12:00:00 AM.

I want to insert blank value in my table for datatime column. drop table wdi. output: 1900-01-01 00:00:00.000. NULL When you attempt to insert a '' into a datetime column, SQL Server must CAST the blank value to  Note that the database table I'm inserting into uses an nvarchar column for the string field and a date column for the date field. The first one just uses a separate "Update Data" activity that will update all date values that equal '1900-01-01' with NULL. The second workflow uses a "Custom Query" activity instead of an "Insert Data" activity.

declare @tbl table (date datetime) insert into @tbl select null union all Per somewhat funny rules in SQL Server, blank translates to 1900-01-01. No, you have changed datatype , with datetime you can have NULL not ''  It would be helpfull then to let us know how you update the db. Even if you are not using this particular control, more likely this is the same root cause (ie. you set the date to an empty string rather than to NULL), SQL Server converts empty string to 1/1/1900 rather than to NULL.

Comments
  • Make the field nullable
  • Possible duplicate of Insert null/empty value in sql datetime column by default
  • You cant control @DATEVAL if came from an API call. So I suggest you go with NULLIF
  • @JuanCarlosOropeza excellent point. I somehow glossed over that these were the values being received. Absolutely need to use NULLIF to make this work correctly.
  • The problem is if this is an API function receiving DATETIME others can call it using '' and the OP will get 1900-01-01 anyway, isnt?
  • @JuanCarlosOropeza . . . If that is addressed to me, I don't understand the question. NULLIF() solves the problem regardless of whether the code is in a stored procedure or not. SQL Server causes the problem by allowing an empty string to be converted to an arbitrary date.
  • IFNULL() doesn't work anywhere, at least in SQL Server as it is not a built in function name. You are probably confusing NULLIF() and ISNULL(); the former substitutes a null value if the first argument equals the second argument, the latter substitutes the second argument for the first argument when the first argument is NULL. But as you say, the conversion will have already happened when a blank string is passes into a parameter of a specified datatype in a stored procedure. Best thing would be to have the app test and perform business logic against the user input.
  • @ChadEstes You are right I mean NULLIF() as the other answers suggest. And yes validate the input before send to the stored procedure