May 17, 2010 at 2:55 pm
create table TestTable
(id int,
date Varchar(20)
)
insert into TestTable values(2,'08/31/1984')
insert into TestTable values(1,'07/12/2005')
insert into TestTable values(3,'08/31/0002')-- in this row i inserted 0002 intentionally my data had some errors like that.
declare @olams int
declare @dob datetime
set @dob='08/31/1984'
select @olams = (select count(*) from TestTable where (@dob IS NULL) OR date = CONVERT(DATETIME,@dob,101))
Print @olams
why this query is not working .
i know that date coloumn in test table has invalid data(0002) so thats why its giving an error . so how can fix this
Any help is appreciated.
Thanks
Kishore
May 17, 2010 at 3:04 pm
The error you are getting is due to the CONVERT statement. The conversion results in an invalid date, hence the error.
The best way to prevent this is to store the data as a date datatype. You've just uncovered why you should not store dates as character data. To fix your existing data, you'll need to identify invalid dates in your data and manually fix them. How large of a table are you talking about here?
May 17, 2010 at 3:08 pm
Yes i had like 2lakh records. i cant change the datatype of table to datetime. i need to fix it through query only.
Thanks
Kishore
May 17, 2010 at 3:10 pm
And just in case you were wondering why is "08/31/0002" an invalid date time: it's because the earliest date you can store in SQL2K5 is Jan 01, 1753: http://msdn.microsoft.com/en-us/library/ms187819(SQL.90).aspx
May 17, 2010 at 3:12 pm
Yes i know the error is due to invalid date.
is there any way that we fix this using tsql code
Thanks
Kishore
May 17, 2010 at 3:13 pm
I think your statement at the end of your post skipped my eyes. Apologies :blush:
May 17, 2010 at 3:23 pm
I would suggest trying this (on a separate set of data first)
update TestTable set myDate = convert(varchar(20), getdate()-3,101)
where myDate <= '01/01/1753'
May 17, 2010 at 3:23 pm
I would build out a dates table with character dates and then JOIN that back to your table to find date value that are out of range. You can then take those out of range values, decide what they should be, and edit them appropriately.
This solution contains a loop. This could be written completely set based if you have a tally table in your DB.
--==== Build out test table from OP's example:
declare @TestTable table (id int, date Varchar(20))
insert into @TestTable values(2,'08/31/1984')
insert into @TestTable values(1,'07/12/2005')
insert into @TestTable values(3,'08/31/0002')-- in this row i inserted 0002 intentionally my data had some errors like that.
DECLARE @Date datetime
DECLARE @CharDates TABLE (RowID int IDENTITY(1,1), CharDate varchar(10))
SET NOCOUNT ON
--==== Set you MIN date range here
SET @Date = '1980-01-01'
--==== Loop from MIN date to MAX date (Set in WHILE condition below)
WHILE @Date < '2011-01-01'
BEGIN
INSERT INTO @CharDates(CharDate)
SELECT CONVERT(varchar,@Date,101)
SET @Date = DATEADD(dd,1,@Date)
END
--==== Now that a character dates table is built out, JOIN it back to your
--==== table containing character dates and look for values that don't match
SELECT *
FROM@TestTable t
LEFT JOIN @CharDates c ON t.date = c.chardate
WHEREc.chardate IS NULL
May 17, 2010 at 3:36 pm
rjv_rnjn (5/17/2010)
I would suggest trying this (on a separate set of data first)
update TestTable set myDate = convert(varchar(20), getdate()-3,101)
where myDate <= '01/01/1753'
This will not work. Your <= comparison will not catch the invalid dates. The way your code is written, SQL Server will do a character comparison between the Date column and the string value ('01/01/1753'). Besides that, the invalid date in the example may not be the only flavor of invalid date. What if someone entered '15/31/2010'?
The only way to accuratly compare the columns is to convert the date to a real datetime. As we all know, this will fail when an invalid date is encountered. To solve this, we need to identify a which dates are invalid.
May 17, 2010 at 3:38 pm
Thanks John
but your solution is creating new table called @CharDates
say suposse if my base table has 2 lakh records each time the @charDates will also have 2 lakh records .
its an extra burden to maintain a new table.
i can understand the probelm.
Thanks
Kishore.
May 17, 2010 at 3:44 pm
@Chardates is a table variable that will only be used at the time of the query to find the invalid dates. It will not need to be maintained.
Is this a one-time fix?
May 17, 2010 at 3:45 pm
John Rowan (5/17/2010)
rjv_rnjn (5/17/2010)
I would suggest trying this (on a separate set of data first)
update TestTable set myDate = convert(varchar(20), getdate()-3,101)
where myDate <= '01/01/1753'
This will not work. Your <= comparison will not catch the invalid dates. The way your code is written, SQL Server will do a character comparison between the Date column and the string value ('01/01/1753'). Besides that, the invalid date in the example may not be the only flavor of invalid date. What if someone entered '15/31/2010'?
True. I get the point. Thanks.
May 17, 2010 at 5:34 pm
sql.kishore1 (5/17/2010)
create table TestTable(id int,
date Varchar(20)
)
insert into TestTable values(2,'08/31/1984')
insert into TestTable values(1,'07/12/2005')
insert into TestTable values(3,'08/31/0002')-- in this row i inserted 0002 intentionally my data had some errors like that.
declare @olams int
declare @dob datetime
set @dob='08/31/1984'
select @olams = (select count(*) from TestTable where (@dob IS NULL) OR date = CONVERT(DATETIME,@dob,101))
Print @olams
why this query is not working .
i know that date coloumn in test table has invalid data(0002) so thats why its giving an error . so how can fix this
Any help is appreciated.
Thanks
Kishore
If you just want to fix your query (so that it will run without fixing the data), you could use the IsDate function with a CASE statement, something like this:
select @olams = (select count(*) from TestTable
where (@dob IS NULL)
OR CONVERT(DATETIME,@dob,101) = CASE WHEN ISDATE(date)= 1
THEN date
ELSE ''
END)
Now, if the varchar version of the date is not a date, you'll be matching against '', so it won't choke on the comparison.
Rob Schripsema
Propack, Inc.
May 17, 2010 at 7:02 pm
Heh... try the following, Rob...
SELECT ISDATE('2000')
You sure you want that as a valid date?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 17, 2010 at 8:05 pm
Thanks Guys. I got it now.
Viewing 15 posts - 1 through 15 (of 27 total)
You must be logged in to reply to this topic. Login to reply