0

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.

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Aaron thanks for reply, is it not possible from frent end, i am using asp.net.
I'm not sure what you mean by "not possible" - asp.net knows whether the user entered a date or not, doesn't it? And so can pass the keyword NULL instead of a formatted string?
Aaron actually, when user not enter date option then null will taken, but it is giving error, saying format is not correct, becuase i am converting the value which is in textbox into datetime.
You need to pass NULL with no quotes. You are never going to be able to store non-date strings like '00-00-0000' or 'NULL' or 'be-fo-fart' in a datetime column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.