Solved

Date Format on Insert Stored Procedure

Posted on 2004-08-08
16
30,311 Views
Last Modified: 2012-08-13
Hi,

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.

slts.

plm
0
Comment
Question by:pascalmartin
  • 5
  • 5
  • 2
  • +3
16 Comments
 
LVL 3

Expert Comment

by:harry_hendrata
ID: 11746787
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;


0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 11746800
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
formats....?

0
 
LVL 17

Accepted Solution

by:
BillAn1 earned 250 total points
ID: 11746861
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)
as

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

That way you can explicitly cast from dd/mm/yy format of string to date.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 9

Expert Comment

by:crescendo
ID: 11746897
Just change the line to

    objParam.Value = CDate(ETD.Text)

0
 
LVL 17

Expert Comment

by:BillAn1
ID: 11746971
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.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 11746999
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..........


0
 
LVL 9

Expert Comment

by:crescendo
ID: 11747024
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.

0
 
LVL 17

Expert Comment

by:BillAn1
ID: 11747028
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?
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11747677
BillAn1:

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?

LowFatSpread:

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.
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 11747717
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.
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 11747746
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.
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11747867
The CDate is server-side. Thanks.
0
 

Author Comment

by:pascalmartin
ID: 11749251
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?

0
 
LVL 1

Expert Comment

by:worldserga
ID: 11750661
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.
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11750677
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.
0
 
LVL 3

Expert Comment

by:harry_hendrata
ID: 11752771
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)



0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question