October 26, 2007 at 11:38 am
How can I check if a date is a valid date format, the field has date in '11/16/2004' format.
overall i need to check if the date is in the right format
October 26, 2007 at 11:45 am
....
declare @myanswer bit
select @myanswer=isdate('11/24/1998')
....
isdate returns 1 if valid 0 otherwise (valid also means in a range SQL can store).
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
October 26, 2007 at 11:48 am
its not the exact date i need to check rather i want to check the format
October 26, 2007 at 11:51 am
What do you mean. IsDate on a value will return 1 is the value (plus formatting) is acceptable as a date and 0 if not. But if you are talking about data in a datetime field the answer is the format is valid but depends on whom you are trying to display.
October 26, 2007 at 11:56 am
IsDate checks that the format of whatever you have in quotes would convert to a date. So - the format has to be right, AND it would have to evaluate to a "real date".
Pass it a string parameter.
If you're storing the data IN a datetime field - it HAS to be valid.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
October 26, 2007 at 11:58 am
SELECT importdate FROM
WHERE
CAST(substring(importdate,8,4) AS integer) < 1754
OR
CAST(substring(importdate,8,4) AS integer) > 9998
if u r checking whether ur date column is between the datetime range
October 26, 2007 at 12:16 pm
Mike is wanting to check the formatting of the date, not necessarily whether its a date or not.
Mike Levan (10/26/2007)
its not the exact date i need to check rather i want to check the format
You'll need to use string functions to check for the specific format if the field is in fact a char/varchar field.
CREATE TABLE #temp (dateString VARCHAR(50))
INSERT #temp
SELECT '4/1/2007'
UNION
SELECT 'Feb 1, 2006'
UNION
SELECT '05/08/2012'
UNION
SELECT '05/21/12'
SELECT
datestring
,ISDATE(datestring) as IsDate
,CASE WHEN SUBSTRING(datestring,2,1) = '/' THEN 'yes' ELSE 'no' END as isValidFormat
FROM
#temp
DROP TABLE #temp
Gives you
datestringIsDateisValidFormat
05/08/20121no
05/21/121no
4/1/20071yes
Feb 1, 20061no
______________________________________________________________________
Personal Motto: Why push the envelope when you can just open it?
If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.
Jason L. SelburgOctober 26, 2007 at 1:05 pm
Mike Levan (10/26/2007)
How can I check if a date is a valid date format, the field has date in '11/16/2004' format.overall i need to check if the date is in the right format
You might have other issues as well. Do all of your users enter dates the same? there is really only one way to interpret '11/16/2004', but if it is '6/8/2004' it could be June 8, or it could be Aug 6. When users are allowed to enter dates in char field you will get inconsistency. You should try to verify which it is.
Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
October 26, 2007 at 1:28 pm
the real problem is that you are storing dates in character data types. else all values are already in the same format. should store dates in smalldatetime or datetime fields.
since u are storing your dates in character fields, Jason is right, you'll need to use string manipulation as he demonstrated.
October 26, 2007 at 1:49 pm
13/34/2007 is not a valid date, need to check like this.
October 26, 2007 at 2:06 pm
October 26, 2007 at 5:52 pm
Mike Levan (10/26/2007)
13/34/2007 is not a valid date, need to check like this.
Mike,
As we've noted here, there is a difference in checking to see if "13/34/2004" is a valid date or a valid date format. There is a difference. :w00t:
______________________________________________________________________
Personal Motto: Why push the envelope when you can just open it?
If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.
Jason L. SelburgOctober 27, 2007 at 5:55 pm
I agree with Jason... I think ISDATE, by itself, is a very bad way to check for valid dates. For example, the following all return "1" using ISDATE...
[font="Courier New"]SELECT ISDATE('2007')
SELECT ISDATE('JAN 2007')[/font]
At least add in a check for length and a check for slashes in the right spots...
--Jeff Moden
Change is inevitable... Change for the better is not.
October 27, 2007 at 6:59 pm
Jason certainly had the right idea... and this goes just a bit further for forcing a format... but it's not 100% bullet proof...
DECLARE @test-2 TABLE (SomeDate VARCHAR(20))
INSERT INTO @test-2
(SomeDate)
SELECT '2007' UNION ALL
SELECT '01/01/1753' UNION ALL
SELECT '1753' UNION ALL
SELECT '1752' UNION ALL
SELECT 'JAN 2007' UNION ALL
SELECT '11/34/2007' UNION ALL
SELECT '34/11/2007' UNION ALL
SELECT '12/28/2007' UNION ALL
SELECT '28/12/2007' UNION ALL
SELECT '1/28/2007' UNION ALL
SELECT '28/1/2007' UNION ALL
SELECT '05/08/2012' UNION ALL
SELECT '05/21/12' UNION ALL
SELECT '4/1/2007' UNION ALL
SELECT 'Feb 1, 2006' UNION ALL
SELECT 'Feb 1 2006'
SELECT SomeDate,
ISDATE(SomeDate) AS IsDate,
CASE
WHEN ISDATE(SomeDate) = 1
AND SomeDate LIKE '[0-1][0-9]/[0-3][0-9]/[1-2][0-9][0-9][0-9]'
THEN 1
ELSE 0
END AS IsValidDateAndFormat
FROM @test-2
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply