soni2926 (soni2926 (AT) yahoo (DOT) com) writes:
Quote:
now using those dates i tried the following:
SELECT distinct CONVERT(datetime, a.startdate, 105), startdate
FROM Client a left join SHistory b on a.ClientId = b.ClientId
WHERE DATEDIFF(month, CONVERT(datetime, a.startdate_yymmdd, 105),
@LastestBiWeeklyPeriod) < 6
but i keep getting this:
Conversion failed when converting datetime from character string.
I don't get why, as running the query without the WHERE DATEDIFF...
everything runs fine, there is no error about the same date conversion
happening in the SELECT clause. I tried checking the dates the data is
working with and they all seem fine, I didn't know any incorrect date.
the startdate is coming in like this:
20080908
20091019
20100125
20100405
20080728 |
You asking for startdate_yymmdd to be interpreted according to format 105,
which I don't know how it looks like on the top of my head. But that much I
know that YYYYMMDD is format 112, so 105 is likely to be incorrect.
Furthermore, YYYYMMDD is a safe format that always works, so you don't
need to specify any format code with it. Or convert either for that matter.
Overall, don't store dates in string format, but use the datetime data type
instead. This saves you from problems like this. Furthermore, it will be
possible for SQL Server to use indexes. In this case you could save the
show with writing:
a.startdate_yymmdd >
convert(char(8), dateadd(MONTH, -6, @LastestBiWeeklyPeriod), 112)
Although this is not exactly equal to what you had - then again, it
may be closer to what you want!
--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx