Solved

Save data from form as datetime

Posted on 2011-09-12
3
288 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

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now