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


Date Validations?


Date Validations?

Author
Message
Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)

Group: General Forum Members
Points: 819 Visits: 1721



--this date MAY or MAY NOT be valid depending on the DATEFORMAT setting
SELECT ISDATE('29-12-2013')
--this is never valid (out-of-range)
SELECT ISDATE('32-12-2013')
--this should always be valid in all formats
SELECT ISDATE('2013-01-08 15:44:12.208')
--this is a valid UMC date but ISDATE = 0
SELECT ISDATE('2013-01-08 15:44:12.2081606 +05:30')


DECLARE @testdate VARCHAR(20)
SET @testdate = '29-12-2013' -- this is valid in DMY format
SET @testdate = '32-12-2013' -- this is never valid (out-of-range)
SET @testdate = '2013-01-08 15:44:12.208' -- this is valid
SET @testdate = '2013-01-08 15:44:12.2081606 +05:30' -- this is valid UMC date

--for these testdates using the script below,
--#1 is ambiguous, #2 is correct, #3 & #4 are INCORRECT
SELECT
@testdate
,IsDateValid = CASE WHEN @testdate IS NULL THEN 0
WHEN @testdate NOT LIKE '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]' THEN 0
WHEN ISDATE(REPLACE(@testdate,'-','')) <> 1 THEN 0
ELSE 1
END

--this procedure returns the proper results
EXEC dbo.IsValidDate '29-12-2013','DMY'
EXEC dbo.IsValidDate '32-12-2013'
EXEC dbo.IsValidDate '2013-01-08 15:44:12.208'
EXEC dbo.IsValidDate '2013-01-08 15:44:12.2081606 +05:30'




Michael Valentine Jones
Michael Valentine Jones
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5692 Visits: 11771
Steven Willis (7/22/2013)



--this date MAY or MAY NOT be valid depending on the DATEFORMAT setting
SELECT ISDATE('29-12-2013')
--this is never valid (out-of-range)
SELECT ISDATE('32-12-2013')
--this should always be valid in all formats
SELECT ISDATE('2013-01-08 15:44:12.208')
--this is a valid UMC date but ISDATE = 0
SELECT ISDATE('2013-01-08 15:44:12.2081606 +05:30')


DECLARE @testdate VARCHAR(20)
SET @testdate = '29-12-2013' -- this is valid in DMY format
SET @testdate = '32-12-2013' -- this is never valid (out-of-range)
SET @testdate = '2013-01-08 15:44:12.208' -- this is valid
SET @testdate = '2013-01-08 15:44:12.2081606 +05:30' -- this is valid UMC date

--for these testdates using the script below,
--#1 is ambiguous, #2 is correct, #3 & #4 are INCORRECT
SELECT
@testdate
,IsDateValid = CASE WHEN @testdate IS NULL THEN 0
WHEN @testdate NOT LIKE '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]' THEN 0
WHEN ISDATE(REPLACE(@testdate,'-','')) <> 1 THEN 0
ELSE 1
END

--this procedure returns the proper results
EXEC dbo.IsValidDate '29-12-2013','DMY'
EXEC dbo.IsValidDate '32-12-2013'
EXEC dbo.IsValidDate '2013-01-08 15:44:12.208'
EXEC dbo.IsValidDate '2013-01-08 15:44:12.2081606 +05:30'





The OP stated that the total date should be in format YYYY-MM-DD, so the test I gave them looks for exactly that, and is insensitive to the setting of DATEFORMAT.

From OP:
"Year should be in yyyy format ,month should be between 1 and 1 to 12 and date should be between 1 to 31 and total date should be YYYY-MM-DD Formate?"
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