Home All Groups Group Topic Archive Search About

Selecting dates in SQL Server

Author
28 May 2009 11:08 PM
Kevinp
I have a table with a date field and one of the
rows has a date value of '2009-03-31
16:39:16.000'.

I'm trying to use a SELECT statement like this:
SELECT * FROM MyTable WHERE fldDate >= '01/01/09'
AND fldDate <= '03/31/09'

The results will not include the above stated
record. Is there some way to change the SQL
statement that will include it?

Author
28 May 2009 11:31 PM
Armin Zingler
Kevinp wrote:
> I have a table with a date field and one of the
> rows has a date value of '2009-03-31
> 16:39:16.000'.
>
> I'm trying to use a SELECT statement like this:
> SELECT * FROM MyTable WHERE fldDate >= '01/01/09'
> AND fldDate <= '03/31/09'
>
> The results will not include the above stated
> record. Is there some way to change the SQL
> statement that will include it?

'03/31/09' is equal to '03/31/09 00:00'

'2009-03-31 16:39:16.000'  is not before and not equal to 03/31/09 00:00

If you want to include the 31th of March 2009, use

    < '04/01/2009'

(which is also true if you use SQLParameters)


Armin
Author
29 May 2009 2:29 AM
Kevinp
On Fri, 29 May 2009 01:31:56 +0200, "Armin
Zingler" <az.nospam@freenet.de> wrote:

Show quoteHide quote
>Kevinp wrote:
>> I have a table with a date field and one of the
>> rows has a date value of '2009-03-31
>> 16:39:16.000'.
>>
>> I'm trying to use a SELECT statement like this:
>> SELECT * FROM MyTable WHERE fldDate >= '01/01/09'
>> AND fldDate <= '03/31/09'
>>
>> The results will not include the above stated
>> record. Is there some way to change the SQL
>> statement that will include it?
>
>'03/31/09' is equal to '03/31/09 00:00'
>
>'2009-03-31 16:39:16.000'  is not before and not equal to 03/31/09 00:00
>
>If you want to include the 31th of March 2009, use
>
>    < '04/01/2009'
>
>(which is also true if you use SQLParameters)
>
>
>Armin

Yes, I realize that. Entering '04/01/09' is not
acceptable to my users who only want up to
'03/31/09'.
Author
29 May 2009 2:56 AM
Armin Zingler
Kevinp wrote:
Show quoteHide quote
> On Fri, 29 May 2009 01:31:56 +0200, "Armin
> Zingler" <az.nospam@freenet.de> wrote:
>
>> Kevinp wrote:
>>> I have a table with a date field and one of the
>>> rows has a date value of '2009-03-31
>>> 16:39:16.000'.
>>>
>>> I'm trying to use a SELECT statement like this:
>>> SELECT * FROM MyTable WHERE fldDate >= '01/01/09'
>>> AND fldDate <= '03/31/09'
>>>
>>> The results will not include the above stated
>>> record. Is there some way to change the SQL
>>> statement that will include it?
>>
>> '03/31/09' is equal to '03/31/09 00:00'
>>
>> '2009-03-31 16:39:16.000'  is not before and not equal to 03/31/09
>> 00:00
>>
>> If you want to include the 31th of March 2009, use
>>
>>    < '04/01/2009'
>>
>> (which is also true if you use SQLParameters)
>>
>>
>> Armin
>
> Yes, I realize that. Entering '04/01/09' is not
> acceptable to my users who only want up to
> '03/31/09'.


Add one day by code.


Armin
Author
29 May 2009 11:01 AM
Kevinp
On Fri, 29 May 2009 04:56:16 +0200, "Armin
Zingler" <az.nospam@freenet.de> wrote:

Show quoteHide quote
>Kevinp wrote:
>> On Fri, 29 May 2009 01:31:56 +0200, "Armin
>> Zingler" <az.nospam@freenet.de> wrote:
>>
>>> Kevinp wrote:
>>>> I have a table with a date field and one of the
>>>> rows has a date value of '2009-03-31
>>>> 16:39:16.000'.
>>>>
>>>> I'm trying to use a SELECT statement like this:
>>>> SELECT * FROM MyTable WHERE fldDate >= '01/01/09'
>>>> AND fldDate <= '03/31/09'
>>>>
>>>> The results will not include the above stated
>>>> record. Is there some way to change the SQL
>>>> statement that will include it?
>>>
>>> '03/31/09' is equal to '03/31/09 00:00'
>>>
>>> '2009-03-31 16:39:16.000'  is not before and not equal to 03/31/09
>>> 00:00
>>>
>>> If you want to include the 31th of March 2009, use
>>>
>>>    < '04/01/2009'
>>>
>>> (which is also true if you use SQLParameters)
>>>
>>>
>>> Armin
>>
>> Yes, I realize that. Entering '04/01/09' is not
>> acceptable to my users who only want up to
>> '03/31/09'.
>
>
>Add one day by code.
>
>
>Armin

Thanks for the suggestion, but I 'fixed' it. I was
getting the date from a DateTimePicker control and
only saving the date. Of course it adds the
current time to the date. I changed my program so
it wouldn't. Then I wrote a little loop to fix all
the current records.

I'm too anal to add a day by code :)