0

I have a device that generates logs into a database.

I need to query the database

  1. Query1 = give me all the records and columns from TODAY from table01

  2. Query2 = give me all the records and columns from YESTERDAY from table01

The problem I am having is that the date field is not exactly in a nice mm-dd-yyyy format.

Fields are:

datetime, device, controlnumber, result

the datetime field outputs data that looks like this yyyy-mm-ddTHH:MM:SS (not sure why there is a T except maybe its just plainly saying this is the time):

2018-07-16T13:34:00
2018-07-16T18:15:00
2018-07-16T18:33:00
2018-07-16T18:50:00
2018-07-16T19:02:00

Can anyone help me through a quick (i know this is not right):

select * from table01
where datetime = Today()
and datetime = Yesterday()
Order by controlnumber
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • "The problem I amm having is that the date field is not exactly in a nice mm-dd-yyyy format."...I would hope the date field is a `datetime` column, and then the format is completely irrelevant?? If the field is in fact stored as text (yuk) then the format you're seeing is actually sensible because it's not ambiguous - some dates in mm-dd-yyyy could be interpreted as dd-mm-yyyy and the computer can't tell them apart easily. mm-dd-yyyy and dd-mm-yyyy are nice formats for the humans who are used to them, but they're horrible for computers to work with. Don't store dates as text!! – ADyson Aug 01 '18 at 23:26
  • 1
    The format you're seeing is ISO format by the way, internationally recognised and accepted precisely because it's not possible to mis-interpret it, unlike many other formats. To say it's "not proper" as per your title is completely wrong. What's not proper, as I said, using a text column to store dates in the first place. – ADyson Aug 01 '18 at 23:28
  • 1
    Also, which DBMS are you using? There are different function names used to get the current date, and to add and subtract values from a date (e.g. to work out yesterday based on today), in each engine. – ADyson Aug 01 '18 at 23:30
  • 1
    and your sample query makes no sense - the date field in any given row cannot be "yesterday" _and_ "today" at the same time! Remember that a SQL WHERE clause is evaluated separately for each row. You could use an OR instead of AND to get a list of all rows where the date is _either_ today _or_ yesterday. Or you can write two separate queries, as per your stated requirement in the question. – ADyson Aug 01 '18 at 23:32

2 Answers2

1

In standard SQL you would do this for today:

select t.*
from table01 t
where t.datetime >= current_date and
      t.datetime < current_date + interval '1 day'
Order by controlnumber;

Yesterday is similar:

select t.*
from table01 t
where t.datetime >= current_date - interval '1 day' and
      t.datetime < current_date 
Order by controlnumber;

Just because this is standard SQL doesn't mean that all databases support it. However, all databases do support date arithmetic, although the syntax might vary.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

I think the easiest way to do it is using DATEDIFF() function as follow:

SELECT * FROM Ttable
WHERE DATEDIFF(day,Ttable.DatetimeColumn ,GETDATE()) = 0 -- For today

SELECT * FROM Ttable
WHERE DATEDIFF(day,Ttable.DatetimeColumn ,GETDATE()) = 1 -- For yesterday
Ehsan
  • 767
  • 7
  • 18
  • im getting an error on this.. it doesnt seem to like the "getdate()" – user3006617 Aug 02 '18 at 01:45
  • See if you can find your answer in this link :https://social.msdn.microsoft.com/Forums/sqlserver/en-US/117da43d-0e1f-4038-85af-63df124dd5d3/sql-query-for-yesterdays-data?forum=transactsql – Ehsan Aug 02 '18 at 01:59
  • or this link: https://stackoverflow.com/questions/1503298/sql-statement-to-select-all-rows-from-previous-day – Ehsan Aug 02 '18 at 02:00
  • @user3006617 again, which DBMS are you using? MySQL? Oracle? Sql Server? Something else? You'll have to use the date functions whcih are correct for your database engine. I mentioned this in the comments on the main question, which you've so far apparently ignored. – ADyson Aug 02 '18 at 05:33