How to structure my query in sqlite? - sqlite

I would like to retrieve today's data. At the moment I have something like SELECT * FROM myTable WHERE timeStamp>DATETIME('NOW','-1 DAY') but this gives me results from now to 24hrs back, not just today (i.e. no matter how many hours have passed since 00:00). Using the same logic I want to retrieve data for just yesterday, and for the this week.
[EDIT]
By the term this week i mean.. if today is Thursday, i want to show results from monday or Sunday (it doesnt matter) upto now.

Instead of timeStamp > DATETIME('now') use timeStamp >= DATE('now').
For since yesterday you can use DATE('now', '-1 day').
As for this week - it depends if you mean 7 days ago:
DATE('now', '-7 days')
Or if you mean since the beginning of the first day of this week:
DATE('now', 'weekday 0', '-7 days')

Related

Is there a function to select a group of hours from different days?

I'm looking for a way to select in Amazon Athena a selection of hours for different days.
I have a table with visitors to a specific location for every half hour, I now want to know the visitors during opening hours for a store, for the period of a month.
I now used this but doing it day by day is quite a job.
Was trying to split datetime with Datepart but didn't get it working properly.
SELECT visitors, datetime
FROM corrected_scanners_per_half_hour
WHERE datetime
BETWEEN CAST('2020-05-25 08:30:00' AS timestamp)
AND CAST('2020-05-25 17:30:00' AS timestamp) ;
Here you go
select visitors, date(datetime)
from corrected_scanners_per_half_hour
where date_format(datetime, '%H:%i') between '08:30' and '17:30'

How to get previous day via SQLite in Swift

I have a sqlite table with a number of dates. I would like to execute a query to select those records where the Date is the previous date. I have tried the following but it doesn't work:
SELECT SUM(Amount) AS TOTAL FROM (SpentAmount) Where Date ='\(Date().yesterday)'"
and output is : '2018-07-02 05:43:16 +0000'
But I want only date and this format: '02-07-2018'.
And I m trying This : SELECT SUM(Amount) AS TOTAL FROM SpentAmount WHERE Date = DATE('now', '-1 days');
but this Query give me no result
This is my database Database
but when i execute the query then give me this result
If you want to target yesterday (i.e. the date before today), then use DATE('now', '-1 days'):
SELECT SUM(Amount) AS TOTAL
FROM SpentAmount
WHERE Date = STRFTIME('%d.%m.%Y', DATE('now', '-1 days'));
As a general comment, your current date format is very non ideal. It will be very hard to sort your table by date. Instead, you should always store your date information in ISO format with SQLite, i.e. use 2018-07-02, not 02-07-2018.

Retrieve date from Evernote date_created timestamp field

Does anyone know on what basetime Evernote calculates datetime?
I need to directly deal with the notes table in the Evernote SQLite DB and the documentation refers people to the SQLite manual https://www.sqlite.org/lang_datefunc.html
This stored time 736012.8334375 should yield 2016/02/18 21:00
I've tried multiple variants such as
select datetime(((((736012.8334375)*1000/60)/60)/24), 'unixepoch'), datetime(((736012.8334375)), 'unixepoch'), datetime(736012.8334375, 'unixepoch'), strftime('%s', 'now'), strftime('7736012.8334375', 'unixepoch'), datetime((736012.8334375 *1000), 'unixepoch')
giving
"1970-01-01 23:39:46","1970-01-09 12:26:52","1970-01-09 12:26:52","1464341058",,"1993-04-28 16:00:33"
This Excel formula
=((((736012.8334375)*1000/60)/60)/24)+DATE(1970,1,1)+(1/24)
gets closer with
4/28/93 5:00 PM
but still a bit out.
What am I doing wrong?
Here's the formula I arrived at for determining the real date from Evernote's dates:
unixTime = (EvernoteTime * 86400) - 62135683200
I've tested this with a few time zones by exporting data from the Evernote app to html and hand-verifying the times match.
I'm not sure where the 62135683200 comes from. It's not quite the difference between unix epoch and year zero, but after arriving at the correct value I stopped trying to figure it out.

how to get previous day datetime at specific hour in oracle sql

How to get datetime in oracle of previous day at specific hour?
Say for example today is June 1st and i want to get the datetime of previous day 6pm?
i need to go back one day at specific hour.
There is a lot of ways to do that, I give you few, if I understand your question correctly:
SELECT TO_DATE(TO_CHAR(TRUNC(SYSDATE - 1), 'YYYY.MM.DD')||' '||'06:00:00','YYYY.MM.DD HH24:MI:SS'),
TRUNC(SYSDATE - 1) + 0.25
FROM dual
In first way you get, 05/31, and 06:00:00 is time which you want to use.
In second example you will get last day and add 6/24=0.25 (6 hours of 24)

SQLite Query Sorting

I'm a bit of a nub when it comes to SQL queries and I couldn't find any help so I figured I'd ask.
I'm currently working on an event tracker/calendar style thing, with two types of events. One is a standard starts at X and ends at Y, while the other is "all day" (ie, starts at 12:01 AM, ends at 11:59 PM). My problem is the database query to sort it properly. What I'm trying to do is get the return such that the all-day events are at the very end of that day's list.
For example, if I have 4 events, one at 1 PM, one at 2 PM, one all day, and one tomorrow at 11 AM, it would look like:
1:00 PM Event
2:00 PM Event
All Day Event
Tomorrow 11:00 AM Event
I've got UNIX timestamps (in seconds for whatever reason) for start and end dates, and my current attempt is
SELECT * FROM table ORDER BY all_day_flag, startTime;
This won't work, because it would always put the all-day events at the end, so any tips on where to refine it would be much appreciated.
You need to extract the date and time separately from your Unix timestamp, and then use the date as the first sort option, followed by all-day flag and then the time.
Try this:
SELECT date(startTime, 'unixepoch') AS startDate,
time(startTime, 'unixepoch') AS startHour,
time(endTime, 'unixepoch') AS endHour,
all_day_flag
FROM table
ORDER BY
startDate, all_day_flag, startHour;

Resources