Surya sasidhar
Surya sasidhar

Reputation: 30303

Insert default value in datetime column

In SQL Server, for the data type datetime, can I insert like "00-00-0000" from the front end? It is giving an error. I want to insert "00-00-0000" if the user did not enter anything.

Upvotes: 0

Views: 1479

Answers (1)

anon
anon

Reputation:

00-00-0000 is not a date. If the user didn't enter anything, and that's ok, enter NULL. If you really want to show it as 00-00-0000 on the client side, you can do so using COALESCE, e.g.

DECLARE @d TABLE(d DATETIME);

INSERT @d SELECT '2011-01-01'
    UNION ALL SELECT NULL;

SELECT COALESCE(
  CONVERT(CHAR(10), d, 120), '0000-00-00')
  FROM @d;

Upvotes: 2

Related Questions