Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I need to insert date into my datecolumn regdate which is of datetime data type.But before inserting i need to check whether the date iam inserting is in dd mm yyyy format or not.If it is in that format,then i need to insert or else not.

I have tried like this with Isdate function,like

SET LANGUAGE us_english
SET DATEFORMAT dmy

isdate('12/10/2013') /////returns 1
isdate('12/13/2013') ///////returns 0
and its working fine.

but if i use like this

SET LANGUAGE us_english;
SET DATEFORMAT dmy;
select isdate('2013/12/12') ////returns 1 instead of 0


Thanks a lot for any help.
Posted
Comments
Mike Meinz 19-Nov-13 15:32pm    
DateTime data type stores date as an integer value. It does not matter what date format you pass in your INSERT SQL statement although ISO format YYYY-MM-DD is preferred.

Validation of a date should be done in the application source code (e.g. C# or VB .NET). I suggest that you use CDATE() within a Try..Catch block to determine if a date is a valid date.

IsDate Function will return true or false depending on whether the value you are passing is a proper date or not.

You can compare the date for dd/MM/yyyy with the help of Convert function in SQL -

SQL
SELECT CONVERT(VARCHAR,GETDATE(),103)
 
Share this answer
 
Comments
njdcjk 19-Nov-13 7:49am    
If i use like this
select convert(varchar,'2003/2/13',103)

the output is 2003/2/13
Madhu Nair 20-Nov-13 23:44pm    
Try it this way -

SELECT CONVERT(VARCHAR,CAST('2013/12/31' AS DATETIME),103)
If you want to check if date is in proper format, please use RegEx[^]. Here is a collection of examples: Regular Expression Matching a Valid Date[^].

MS SQL Server stores dates depending on many settings[^]. Do not try to store date in custom format. Date is date, no matter of displaying format. Do you get it?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900