SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


problem using ISDATE in a Function


problem using ISDATE in a Function

Author
Message
wolfkillj
wolfkillj
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1462 Visits: 2582
wolfkillj (9/30/2011)
I certainly sympathize with you for having to work with suboptimal requirements with no ability to change them!

Do you really need the hyphen separators when you convert @inDDMMYYYYHHMMSS to @TextDate? If you leave them out, @TextDate will be in ISO 8601 basic format (YYYYDDMM, e.g., '20110930'). ISDATE() works correctly on character strings in ISO 8601 basic format (with or without time data) regardless of the DATEFORMAT setting.



Actually, you could just go with full ISO 8601 format, thusly: '2011-09-01T13:37:44'

From BOL:

The advantage in using the ISO 8601 format is that it is an international standard. Date and time values that are specified by using this format are unambiguous. This format is not affected by the SET DATEFORMAT, SET LANGUAGE, of login default language settings.


http://msdn.microsoft.com/en-us/library/ms180878.aspx#ISO8601Format

Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Michael Valentine Jones
Michael Valentine Jones
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5758 Visits: 11771
Here is a fairly simple check that will work with any setting of DATEFIRST:
select
a.dt,
IsValidDateTime =
case
-- Check that date string is in correct format, mm/dd/yyyy hh:mm:ss
when dt not like '[0-1][0-9]/[0-3][0-9]/[1-9][0-9][0-9][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]'
then 0
-- Reformat and test date in universal format of yyyymmdd hh:mm:ss
when isdate(substring(a.dt,7,4)+substring(a.dt,1,2)+substring(a.dt,4,2)+substring(a.dt,11,9)) <> 1
then 0
else 1
end
from
( -- Test Data
select dt = '08/31/2001 14:34:56' union all
select dt = '08/31/2001 34:34:56' union all
select dt = '08/31/2001 14:64:56' union all
select dt = '08/31/2001 14:34:66' union all
select dt = '08/31/2001 25:34:56' union all
select dt = '08/31/2001 14:34:56' union all
select dt = '13/01/2001 14:34:56' union all
select dt = '0a/31/2001 14:34:56' union all
select dt = '08/32/2001 14:34:56' union all
select dt = '21/31/2001 14:34:56'
) a



Results:
dt                  IsValidDateTime 
------------------- ---------------
08/31/2001 14:34:56 1
08/31/2001 34:34:56 0
08/31/2001 14:64:56 0
08/31/2001 14:34:66 0
08/31/2001 25:34:56 0
08/31/2001 14:34:56 1
13/01/2001 14:34:56 0
0a/31/2001 14:34:56 0
08/32/2001 14:34:56 0
21/31/2001 14:34:56 0

(10 row(s) affected)

Cadavre
Cadavre
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3902 Visits: 8472
Michael Valentine Jones (9/30/2011)
Here is a fairly simple check that will work with any setting of DATEFIRST:
select
a.dt,
IsValidDateTime =
case
-- Check that date string is in correct format, mm/dd/yyyy hh:mm:ss
when dt not like '[0-1][0-9]/[0-3][0-9]/[1-9][0-9][0-9][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]'
then 0
-- Reformat and test date in universal format of yyyymmdd hh:mm:ss
when isdate(substring(a.dt,7,4)+substring(a.dt,1,2)+substring(a.dt,4,2)+substring(a.dt,11,9)) <> 1
then 0
else 1
end
from
( -- Test Data
select dt = '08/31/2001 14:34:56' union all
select dt = '08/31/2001 34:34:56' union all
select dt = '08/31/2001 14:64:56' union all
select dt = '08/31/2001 14:34:66' union all
select dt = '08/31/2001 25:34:56' union all
select dt = '08/31/2001 14:34:56' union all
select dt = '13/01/2001 14:34:56' union all
select dt = '0a/31/2001 14:34:56' union all
select dt = '08/32/2001 14:34:56' union all
select dt = '21/31/2001 14:34:56'
) a



Results:
dt                  IsValidDateTime 
------------------- ---------------
08/31/2001 14:34:56 1
08/31/2001 34:34:56 0
08/31/2001 14:64:56 0
08/31/2001 14:34:66 0
08/31/2001 25:34:56 0
08/31/2001 14:34:56 1
13/01/2001 14:34:56 0
0a/31/2001 14:34:56 0
08/32/2001 14:34:56 0
21/31/2001 14:34:56 0

(10 row(s) affected)







That would allow 30th February as a valid date. . . In fact, it'd allow 39th February ;-)


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Michael Valentine Jones
Michael Valentine Jones
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5758 Visits: 11771
Cadavre (9/30/2011)
Michael Valentine Jones (9/30/2011)
Here is a fairly simple check that will work with any setting of DATEFIRST:
select
a.dt,
IsValidDateTime =
case
-- Check that date string is in correct format, mm/dd/yyyy hh:mm:ss
when dt not like '[0-1][0-9]/[0-3][0-9]/[1-9][0-9][0-9][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]'
then 0
-- Reformat and test date in universal format of yyyymmdd hh:mm:ss
when isdate(substring(a.dt,7,4)+substring(a.dt,1,2)+substring(a.dt,4,2)+substring(a.dt,11,9)) <> 1
then 0
else 1
end
from
( -- Test Data
select dt = '08/31/2001 14:34:56' union all
select dt = '08/31/2001 34:34:56' union all
select dt = '08/31/2001 14:64:56' union all
select dt = '08/31/2001 14:34:66' union all
select dt = '08/31/2001 25:34:56' union all
select dt = '08/31/2001 14:34:56' union all
select dt = '13/01/2001 14:34:56' union all
select dt = '0a/31/2001 14:34:56' union all
select dt = '08/32/2001 14:34:56' union all
select dt = '21/31/2001 14:34:56'
) a



Results:
dt                  IsValidDateTime 
------------------- ---------------
08/31/2001 14:34:56 1
08/31/2001 34:34:56 0
08/31/2001 14:64:56 0
08/31/2001 14:34:66 0
08/31/2001 25:34:56 0
08/31/2001 14:34:56 1
13/01/2001 14:34:56 0
0a/31/2001 14:34:56 0
08/32/2001 14:34:56 0
21/31/2001 14:34:56 0

(10 row(s) affected)







That would allow 30th February as a valid date. . . In fact, it'd allow 39th February ;-)


No it wouldn't. You could have tested that before you posted. ;-)
Cadavre
Cadavre
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3902 Visits: 8472
Michael Valentine Jones (9/30/2011)
No it wouldn't. You could have tested that before you posted. ;-)


Yep, or I should've just read the query properly! Didn't see the part of the case statement where you used the ISDATE function on the formatted date. Sorry :-)


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search