Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL where datetime column equals today's date?

How can I get the records from a db where created date is today's date?

SELECT [Title], [Firstname], [Surname], [Company_name], [Interest]  FROM [dbo].[EXTRANET_users]  WHERE DATE(Submission_date) = DATE(NOW()) 

This doesn't work im using sql server 2000 and submission date is a date time field

like image 862
Beginner Avatar asked Aug 14 '12 15:08

Beginner


People also ask

How do I get today's date in SQL query?

To get the current date and time in SQL Server, use the GETDATE() function. This function returns a datetime data type; in other words, it contains both the date and the time, e.g. 2019-08-20 10:22:34 .

Is there a today function in SQL?

SQL Server provides several different functions that return the current date time including: GETDATE(), SYSDATETIME(), and CURRENT_TIMESTAMP.


2 Answers

Looks like you're using SQL Server, in which case GETDATE() or current_timestamp may help you. But you will have to ensure that the format of the date with which you are comparing the system dates matches (timezone, granularity etc.)

e.g.

where convert(varchar(10), submission_date, 102)      = convert(varchar(10), getdate(), 102) 
like image 105
davek Avatar answered Oct 02 '22 19:10

davek


Can you try this?

SELECT [Title], [Firstname], [Surname], [Company_name], [Interest]  FROM [dbo].[EXTRANET_users]  WHERE CAST(Submission_date AS DATE) = CAST(GETDATE() AS DATE) 

T-SQL doesn't really have the "implied" casting like C# does - you need to explicitly use CAST (or CONVERT).

Also, use GETDATE() or CURRENT_TIMESTAMP to get the "now" date and time.

Update: since you're working against SQL Server 2000 - none of those approaches so far work. Try this instead:

SELECT [Title], [Firstname], [Surname], [Company_name], [Interest]  FROM [dbo].[EXTRANET_users]  WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, submission_date)) = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) 
like image 25
marc_s Avatar answered Oct 02 '22 18:10

marc_s