Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if datetime is in current date

I am using SQLServer 2008. I have a table X with field Y that is a datetime format. In the WHERE statement of my query I only want to keep the rows where the date of field Y equals the current date.

I have searched the internet but couldnt find an example that works for SQLServer/

Thank you for your help!

like image 241
Anonymoose Avatar asked Aug 31 '12 09:08

Anonymoose


People also ask

How do I get the current datetime in Python?

To get both current date and time datetime. now() function of DateTime module is used. This function returns the current local date and time.

How do you check if date is less than or equals to today's date?

Compare(dTCurrent, inputDate); The int 'result' would indicate if dTCurrent is less than inputDate (less than 0), same as (0) or greater than (greater than 0). Save this answer.

What is Strftime in Python?

The strftime() function is used to convert date and time objects to their string representation. It takes one or more input of formatted code and returns the string representation. Syntax : strftime(format) Returns : It returns the string representation of the date or time object.


2 Answers

Try this:

WHERE CONVERT(DATE, Y) = CONVERT(DATE, getdate())
like image 102
Joe G Joseph Avatar answered Oct 02 '22 18:10

Joe G Joseph


You need to convert the date in specific format. Because when you will store the date in sql server in datetime data type then sql server automatically set the date with the default time. Now when you make query with getdate() then it will take the current date with the time, so this will not match with the date which you stored with default time.

So you can do this below 2 ways and get the actual result.

1) Convert using Date DataType which is already done above.

2) Convert with the varchar data type with specific format.

select * from X where Convert(varchar(10),Y,120) = CONVERT(varchar(10),GETDATE(),120)
like image 41
Mayur Desai Avatar answered Oct 02 '22 20:10

Mayur Desai