views:

39

answers:

3

i have a varchar reporttime that has the date stored. dont ask me why it is a varchar

i am doing this to get data between two dates:

select rowid 
 from batchinfo 
where CONVERT(DATE, reporttime, 103) between '2010-07-01' and '2010-07-31'

and it is not working

what am i doing wrong?

btw this is what the data looks like:

rowid   datapath                                                        analysistime       reporttime
4695    F:\MassHunter\DATA\6897_Pan_1\QuantResults\6897_Pan_1.batch.bin  1/2/2010 8:13 AM   1/2/2010 8:25 AM
4696    F:\MassHunter\DATA\6897_Pan_2\QuantResults\6897_Pan2.batch.bin   1/2/2010 8:21 AM   1/2/2010 8:33 AM
4697    F:\MassHunter\DATA\6903_Pan_1\QuantResults\6903_P1.batch.bin     1/2/2010 9:41 AM   1/2/2010 9:46 AM
4698    F:\MassHunter\DATA\6903_Pan_2\QuantResults\6903_Pan2.batch.bin   1/2/2010 9:50 AM   1/2/2010 9:57 AM
4699    F:\MassHunter\DATA\6915_Pan_1\QuantResults\6915_pan1.batch.bin   1/2/2010 10:09 AM  1/2/2010 10:33 AM
+2  A: 

First off, I assume your sample data is not supposed to qualify, true?

Second, you may still be doing a alphanumeric comparison, not a datetime comparison. Try this to force the datetime comparison.

select rowid 
from batchinfo 
where CONVERT(DATE, reporttime, 103)
    between cast('2010-07-01' as datetime) and CAST('2010-07-31' as datetime)

The CAST operator is similar to the CONVERT, which can also be used.

bobs
Msg 241, Level 16, State 1, Line 1Conversion failed when converting date and/or time from character string.
I__
That error is from this CONVERT(DATE, reporttime, 103) - Do you have a bad value not showing in your post example? Bobs solution works for me.
ajdams
Yeah, I saw that too. In your solution, try changing each date string to sue the CONVERT, `CONVERT(DATE, '2010-07-01', 103)`. Or, you can try changing your first `CONVERT`, to CONVERT(DATE, ReportTime, 101)`. The US format works for me, so it may be related to region.
bobs
i cannot do that, as you can see reporttime is in this format: 1/2/2010 8:25 AM
I__
I saw the same issue with the CONVERT(DATE, reporttime, 103). The orginal solution works when I change 103 to 101.
bobs
+1  A: 

CONVERT style 103 is dd/mm/yyyy (European format) so you've got your month and day reversed. Try using 101 instead.

Joe Stefanelli
Msg 241, Level 16, State 1, Line 1Conversion failed when converting date and/or time from character string.
I__
problem i think is that i ahve seconds in there, i think i need to take everything before the first space
I__
This works when I tried it: `select convert(date, '1/2/2010 8:13:02 AM', 101)`
Joe Stefanelli
for me as well, but when i do select convert(date,reporttime, 101) from batchinfo, it gives me the level 16 error
I__
You've got some bad data in that `varchar` column then. Another example of why it's so important to use appropriate data types.
Joe Stefanelli
how do i find the bad data?
I__
A: 

To find your bad data try this:

SELECT *
FROM batchinfo 
WHERE IsDate(reporttime) = 0
Emtucifor