74

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

Beginner
  • 27,041
  • 62
  • 151
  • 234
  • 1
    possible duplicates: [Get row where datetime column = today](http://stackoverflow.com/questions/2583228), [Comparing results with today's date?](http://stackoverflow.com/questions/10395459) – mellamokb Aug 14 '12 at 15:06
  • 1
    Date functions are often database specific. Which database are you using? – Gordon Linoff Aug 14 '12 at 15:06
  • SQL is the Structured Query Language - it's **not** a database product. We really need to know what **database product** and which version you're using.... – marc_s Aug 14 '12 at 15:06
  • 1
    @marc_s and the data type of `Submission_date`. **Edit:** it's in the title but we should confirm it is not of type `Date` :) – D'Arcy Rittich Aug 14 '12 at 15:08
  • 1
    no i get date is not recognised function – Beginner Aug 14 '12 at 15:09
  • 1
    @JIM: there is no `DATE()` built-in function in SQL Server ... he needs to do a `CAST(... AS DATE)` – marc_s Aug 14 '12 at 15:15

5 Answers5

78

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)
davek
  • 21,791
  • 7
  • 74
  • 94
63

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()))
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
8

There might be another way, but this should work:

SELECT [Title], [Firstname], [Surname], [Company_name], [Interest] 
FROM [dbo].[EXTRANET] 
WHERE day(Submission_date)=day(now) and 
     month(Submission_date)=month(now)
     and year(Submission_date)=year(now)
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Allie
  • 824
  • 1
  • 9
  • 14
  • sorry, copy/paste error (dificult when all on one line)... change EXTRANET for EXTRANET_users then it should work – Allie Aug 14 '12 at 15:22
  • @marc_s: year etc. were available in SQL Server 2000: http://msdn.microsoft.com/en-us/library/aa258863(v=sql.80).aspx – davek Aug 14 '12 at 15:28
  • @davek: right you are! Hmm.... odd... in the [2012 docs for `YEAR()`](http://msdn.microsoft.com/en-us/library/ms186313.aspx) there's a "other versions" dropdown - SQL Server 2000 isn't listed there - odd ..... – marc_s Aug 14 '12 at 15:30
2

Easy way out is to use a condition like this ( use desired date > GETDATE()-1)

your sql statement "date specific" > GETDATE()-1

Neyomal
  • 1,459
  • 1
  • 12
  • 14
  • 1
    This doesn't answer the question, as this will only work if there are no dates later than the current day, however this is useful to know and has helped me just now. – Matthew Hudson Nov 07 '18 at 16:38
2

To get all the records where record created date is today's date Use the code after WHERE clause

WHERE  CAST(Submission_date AS DATE) = CAST( curdate() AS DATE)