Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Select by Date from DateTime in SQL Server
Message
 
 
To
10/08/2017 14:13:21
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01653212
Message ID:
01653215
Views:
48
>>I am working with the SQL Server database. The field/column is DateTime. And I want to get records by the following criteria:
>>
>>
>>select * from MyTable where DateTimeCol <= '20170810'  
>>
>>
>>Since the DateTimeCol has both the date and time, the above will select 0 records.
>>
>>How should I change the WHERE to make sure that only the Date of DateTime is considered and therefore the records where the Date is '20170810' are returned?
>
>You would usually use greater or equal from the start of the day and less or equal from the end of the day. I have some easy to convert to VFP methods like this to return the start and end dates of a day:
>
>
>    ' Return a date starting on the day
>    ' expT1 Date
>    Public Function GetDateDayStart(ByVal tdDate As Date) As Date
>        Return New Date(tdDate.Year, tdDate.Month, tdDate.Day, 0, 0, 0)
>    End Function
>
>    ' Return a date ending on the day
>    ' expT1 Date
>    Public Function GetDateDayEnd(ByVal tdDate As Date) As Date
>        Return New Date(tdDate.Year, tdDate.Month, tdDate.Day, 23, 59, 59)
>    End Function
>
>
>However, in GetDatDayEnd(), you would need to consider high precision at the millisecond level in case you would have a record which was created at 23:59:59 123.
>
>Another way is to use greater than or equal to the start of the day and less than of that start date + 1 day.

Thank you. But I think it is too complecated. I just found the following solution:
select * from MyTable where DATEADD(dd, 0, DATEDIFF(dd, 0, DateTimeCol)) <= '20170810'
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform