Solved

Save data from form as datetime

Posted on 2011-09-12
3
289 Views
Last Modified: 2012-05-12
Hello Experts,
I've got a form which has a DOB field (textbox) ...my potential problem is that when a user does not supply the correct format i'm having a problem saving the data. For example...
If the user supplies '1/1/91' i'm getting an out of range error since the stored proc which does the saving of data

CONVERT(DATETIME, @DOB, 121) as DOB

Is there any way of saving the data without experiencing the errors.
0
Comment
Question by:jsuanque
3 Comments
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 62 total points
ID: 36526513

Put validation code in your stored procedure. if the date has a two digit year value, you decide how to correct it or reject it

There are several functions you can use

ISDATE() to verify the string is a proper date

The you could something of the sort

IF YEAR(@DOB) < 100
  IF YEAR(@DOB) + 2000 > YEAR(GETDATE())
    set it to 19XX
  else
    set it to 20xx
 
0
 

Author Comment

by:jsuanque
ID: 36526751
Thanks ewangoya,
Anyway, i'll explore the idea.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 63 total points
ID: 36526786
You need to validate the date in the presentation layer (i.e. in your "form which has a DOB field").  By the time it gets to the Stored Procedure it is too late.

Incidentally, you are using the wrong style for '1/1/91'.  121 is ODBC canonical (with milliseconds) yyyy-mm-dd hh:mi:ss.mmm(24h)

What you need for '1/1/91' is depending on your date format:
MDY: CONVERT(DATETIME, @DOB, 1) as DOB
DMY: CONVERT(DATETIME, @DOB, 3) as DOB




 
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.

828 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