SQLite Query Sorting - sqlite

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;

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'

Searching between 2 different time periods

I would like to be able to have a stored procedure that will allow the user to search between 2 different time frames based off of a parameter that will say whether they are searching for results on a full 24 hour period or based off the business hours. Would a case statement in the where clause be the best choice? This will be a stored procedure that will be used in an SSRS report and the user will choose between "Calendar Day" and "Business Day" in a drop-down menu to specify the type of day defined by the parameter in the stored procedure. Thank you for any help.
I don't know if you'd need to use a CASE statement in your WHERE clause. You could probably just get away with ANDs and ORs.
WHERE
(Parameter = 'Calendar Day')
or
(Parameter = 'Business Day' and DATEPART(HOUR, DateField) between 9 and 17)
The where clause won't compare the hour when its a calendar day but when it's a business day, the time needs to be between 9 am and 5 pm.

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)

where clause with time columns on sql server

In the project there a functionality that lets you book the place during the day, they have to enter the day, the start time and final time from 7:00 am to 12:00 AM (midnight) or 1:00 AM, for example if someone enter date=21/oct/2011 start time=8:00 pm and end time 12:00 am (he book the place from 8:00 pm until 12:00 am) the webform send to the store procedure 20:00 and 00:00 to check the table to see if is available, if someone already book in the same day until midnight it is store like this
startime=23:00 endtime=00:00
so when i check the new client it has to return that there a reservation already in the range of time,
my query is not efficient but it working from the 7:00 to 23:00 range, it fails when from the webfrom enters a endtime 12 am (00:00 on sql) because the starttime es greater than the end time
this is my query
select COUNT(*)
from table1
where id_place=#id_place
and date=#date
and (
(#start_time=res_start_time and #end_time=res_end_time)
or (#start_time > res_start_time and #start_time < res_end_time)
or (#end_time > res_start_time and res_end_time < res_end_time)
or (res_start_time > #start_time and res_start_time < #end_time)
or (res_end_time > #start_time and res_end_time < #end_time)
or (res_start_time < #start_time and res_end_time >#end_time)
)
-- #start_time = start time of the reservations (from webform)
-- #end_time = end time of the reservations (from webform)
-- res_start_time= represents the start time column
-- res_ebd_time= represents the end time column
i need help on two things, how i solved the issue when i have to check times like 12:00 am or 1:00 am that are already in the table like the example at the begins of the question and to check my query because is think there has to be a better solution to implement this kind of functionality
You obviously have a date somewhere in the table, as well, or you would not be able to book resources. This leads to a couple of possibilities.
Query both time and date (can get complex, but you can make this a udf for reuse)
Use DateTime instead of date and time columns
if in SQL Server, consider creating a CLR function to handle this, as the CLR (.NET code) will more efficiently determine "is in time range"
I am sure there are other possibilities. The key point here is it sounds like your algorithm is failing largely due to events that spill over to another day. If that is correct, bring the day into the equation is your best bet. This can end up as a rather complex SQL statement, a change of data types (datetime instead of date and time) or creating a .NET function (CLR) to help more efficiently determine "is in range".

Resources