SQL where datetime column equals today's date?

前端 未结 5 553
感情败类
感情败类 2021-01-31 07:08

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         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 07:49

    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()))
    

提交回复
热议问题