Solved

Save data from form as datetime

Posted on 2011-09-12
3
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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