Date Format on Insert Stored Procedure


My web form contains a textbox where users need to input date (dd/mm/yy), I am using a parameterized stored procedure to update my SQL DB table, but I am facing a problem with the date format in the code below:

objParam = myCommand.Parameters.Add("@ETD", SqlDbType.DateTime)
objParam.Direction = ParameterDirection.Input
objParam.Value = ETD.Text

My browser return the following error:
Cast from string "28/10/04" to type 'Date' is not valid.

How can I ensure that the DB will accept the "28/10/04" date format when the stored procedure fires?

I need some help here.


Who is Participating?
BillAn1Connect With a Mentor Commented:
You might want to change your stored proc to have an input of type char(), and then convert to type datetime in the stored proc
in the stored proc have

create Procedure MyProc
@InputDate char(8)

declare @MyDate datetime
set @MyDate = convert(datetime,@InputDate,3)

That way you can explicitly cast from dd/mm/yy format of string to date.
Hi there..
might want to convert the text to date before you enter it to the parameter:

if it's C#, you could use:
DateTime dateInput = DateTime.ParseExact( ETD.Text,
                                          "dd/MM/yyyy",null );
objParam.Value = dateInput;

if you pass the date value as YYYYMMDD  you shouldn't have any problems....

do you force the user to always enter as dd/mm/yy or do you allow different locales to use there own

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Just change the line to

    objParam.Value = CDate(ETD.Text)

Be careful with something like CDate - it can depend on the locale of the machine, and can easily give strange results. (Particlarly this time of the month when dat formats of yy/mm/dd can be misinterpreted as yy/dd/mm quite easily! Also, users can change their PCs from one locale to another, and this could throw things off for you, so it works for some users, and not others.

The benefit of using convert() in the database is that the conversion is fixed, and cannot be effected

Otherwise, you could use CDate, but then you need to make sure that users input the date in the 'default' format for their particular locale, and not force them to use yy/mm/dd which can be a mess, as often users don't even know what their locale is.
agree with billan1
dates can cause lots of problems especially with  web interfaces

seriously consider passing the date as character to the stored procedure
and actually passing it this format   '08 AUG 2004'  
then there is absolutly no confusion, 3 sets of 2 digits is just asking for trouble..........

It's the locale of the server that matters, or rather the locale of ASPNET. You can't legislate for people putting dates in the wrong format at this time of the month, they just have to know what format to use.

It comes back to the server as text, so it is up to the server to decide how to interpret it. CONVERT doesn't help if you don't know the starting point format, and if you know that, you don't need CONVERT.

If the server locale is set correctly and the users know the expected format, CDate works every time.

Agree with Lowfatspread,
the most robust is to have the month particularly as string.
What ever you do, don't test anything today, is 04/08/08 yy/mm/dd or yy/dd/mm?

Why do you keep on about YY/MM/DD format when he said in his question that he is using DD/MM/YY? Have I missed something?


But how can you convert to  '08 AUG 2004'  unless you know what format his textbox is in? And if you know the format, there's no need to convert further, CDate works.
Appologies, I simply typed them in the wrong order, yes, the question is about dd/mm/yy

CDate will work, but it depends on the user;s comupter. If the user' computer is set up to e.g. locale UK, then if the user inputs 01/08/04 this will be interpreted as 1st Aug 2004. If however the computer happens to be set up with locale US, then CDate will interpret the same text entered as 8th January 2004, because the US default is mm/dd/yy.
If you use CONVERT(datetime,3) then you will force the database to interpret 01/08/04 as 1st Aug, regardless of the user's locale.
But, again, the most robust thing would be for the user to actually type in '08 Aug 2004', then there can be absolutely no ambiguity anywhere.
One more thing - I was assuming that the CDate  is client side?
If not, if it is server-side, then once you make sure your server has the right locale, you can rely on CDate OK.
The CDate is server-side. Thanks.
pascalmartinAuthor Commented:
Thanks for all valuable comments, actually the ideal senario would be to allow the user to enter the date in any format, but the SQL table should always return the date as dd/mm/yy.

The other solution would be also to force the user to type "01-AUG-04" to avoid any confusion between day and month, how the stored procedure shall be amended to accept this insert format?

My friend make your life easy. Create a calendar that will allow users to choose the date instead of allowing them to key in it. You will prevent any wrong data entry. About the date format you can configure it as how it should be represented (dd/mm/yy, yyyy/mm/dd and so on). So after that just pass this parameter to the stored procedure. That's all what you should do. And if you are using ASP.Net for development there is nothing much to do.
If the stored procedure accepts a datetime data type, then just pass it '01 AUG 04'  (no hyphens) and it will recognise it.

You won't be able to allow 'any format' date entry because there's no way easy way of telling what the user is doing unless you store a profile for each user. But that's a lot of extra work and probably not worth the effort.

I suggest you put a CompareValidator on the page, set Operator to CheckDataType, and the Type to Date, and use it to check that the date input is valid before it gets sent to your page.

Finally, if you go down the '01 AUG 04' route, you'll lose the validation, so you'll have to validate server-side.

Given that you already have a working app, and the original question was "How can I ensure that the DB will accept the "28/10/04" date format when the stored procedure fires?" I say stick with what you are doing and use CDate as suggested previously. The error message is simply because you are supplying a string when a date is expected. This question has become a discussion about date programming and has moved away from the original question.
To sum up..

1. force the input to somthing that you could control, then do CDATE or Convert or Convert before pass it to the database

2. If so, the user is ok for you to input in DD-MON-YYYY, you could just  do CAST('1-AUG-2004' AS DATETIME) or CONVERT(DateTime, '01-AUG-2004',106)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.