Parsing date-time into readable formats - sql

I have the following code that WORKS but I am unable to recreate because I do not understand WHY it works. If you plug it in w3schools it compiles successfully.
I do not understand how "1501532100" is parsed into a working date function. individually, I can see how dateadd() and format works, but why does it work the way it does and how can I reverse engineer the rest of the integers into proper dates?
SELECT FORMAT((dateadd(s, 1501532100, '1969-12-31 20:00')), 'MM.dd.yyy');
RETURNS: 07.31.2017

dateadd accepts 3 arguments: interval, number and date. When interval is s, it means that number will be treated as seconds, so it will add that many seconds to the date specified and return the result, which will then be displayed in the MM.dd.yyy format.
You can think of the first argument of dateadd as a measurement unit of the second one.

From: https://www.w3schools.com/sql/func_sqlserver_dateadd.asp
DATEADD(interval, number, date)
interval here is s - seconds,
number is 1501532100
date being 1969-12-31 20:00
all that does is just adds 1501532100 seconds to 1969-12-31 20:00

Related

In oracle, how do I create a check constraint that checks if the time is between 10:00:00 and 21:00:00?

In an Oracle database, I have a registration date column. Using SQL, I want to add a check constraint that checks the time of the date is between 10:00:00 and 21:00:00.
How can I do this?
Ideally, the constraint should use date-time (and interval) expressions and functions as much as possible. Alas, in Oracle date arithmetic works mostly with numbers (representing "days"), so you do need to convert somewhere.
I would do it like this - easiest (in my opinion) for future developers to read and understand:
check (numtodsinterval(reg_date - trunc(reg_date), 'day')
between interval '10' hour and interval '21' hour)
trunc(reg_date) returns the date, with the time-of-day truncated to midnight. Then reg_date - trunc(reg_date) returns the time-of-day stored in reg_date - but the result is a number (expressed in days). I convert this to an interval with the numtodsinterval, specifying that the number represents "days". The rest should be obvious.
By the way, if you must check that the time-of-day is, for example, between 10:20 and 21:20, you can use the same approach - use interval 10:20 hour to minute, for example, in the between condition. The advantage (in readability) of writing the condition in this way becomes even clearer in those cases.
You can use:
check (to_char(datecol, 'HH24:MI') between '10:00' and '21:00'))
Note: This includes '21:00'. Use >= and < if you don't want it included.
You can use the check constraint as follows:
(Your_date - trunc(your_date))*24 between 10 and 21

Converting only time to unixtimestamp in Hive

I have a column eventtime that only stores the time of day as string. Eg:
0445AM - means 04:45 AM. I am using the below query to convert to UNIX timestamp.
select unix_timestamp(eventtime,'hhmmaa'),eventtime from data_raw limit 10;
This seems to work fine for test data. I always thought unixtimestamp is a combination of date and time while here I only have the time. My question is what date does it consider while executing the above function? The timestamps seem to be quite small.
Unix timestamp is the bigint number of seconds from Unix epoch (1970-01-01 00:00:00 UTC). The unix time stamp is a way to track time as a running total of seconds.
select unix_timestamp('0445AM','hhmmaa') as unixtimestamp
Returns
17100
And this is exactly 4hrs, 45min converted to seconds.
select 4*60*60 + 45*60
returns 17100
And to convert it back use from_unixtime function
select from_unixtime (17100,'hhmmaa')
returns:
0445AM
If you convert using format including date, you will see it assumes the date is 1970-01-01
select from_unixtime (17100,'yyyy-MM-dd hhmmaa')
returns:
1970-01-01 0445AM
See Hive functions dosc here.
Also there is very useful site about Unix timestamp

SQL : How sysdate minus a value works?

I found below code in my existing project.
select * from mytable where SomeColumn_date >= trunc(sysdate)-.25/24;
Sample value for SomeColumn_date is 22-JUN-17 05:46:55
How does SomeColumn_date >= trunc(sysdate)-.25/24 work on Date data type?
Different database engines allow different operations to be applied to date data types. For the most part, an operation of <Date Value> +/- 1 will add or subtract one day to that date value. This is syntactically equivalent to the dateadd function when using days.
In your example here, the -.25/24 resolves to the number equivalent of -15 minutes, which is then subtracted from your date value.
It is essentially a much less readable version of datedd(min,-15,<Date Value>).
From the documentation of TRUNC (I'm guessing you are using Oracle):
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. [...] If you omit fmt, then date is truncated to the nearest day.
The result of trunc(sysdate) would be the present date without the time component. Now .25/24 (actually meaning 0.25/24) is substracted from that. If you substract a date using - the operand is always in days. 0.25/24 would be a form to express a quarter of an hour.
So trunc(sysdate)-.25/24 would result in yesterday 23:45.
Ok so 2 things are happening here:
trunk(date,fmt)
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. If you omit fmt, then date is truncated to the nearest day.
So if you have suppose 22-JUN-17 05:46:55 you get 22-JUN-17. Since you don't have the fmt
DATETIME - .25/24 implies .25 hours before your current Date time.
But since you have only DATE all it does is .25 hours before todays 12:00 AM i.e yesterdays 11:45PM
SomeColumn_date >= trunc(sysdate)-.25/24
So suppose if its 22-JUN-2017 right now the date is compared to 21-JUN-2017 11:45 PM
NOTE: - is for before current time, + is for after the current time

PGSQL convert time to second

why i get error with this code
SELECT EXTRACT(SECOND FROM TIME total_time) from tr_empl_quiz;
and i got error to with this code
SELECT EXTRACT(EPOCH FROM TIMESTAMP total_time) from tr_empl_quiz;
this is my table content tr_empl_quiz and type data is time without time zone
total_time
============
00:01:00
00:02:00
When you use the extract() function you are getting the value of the date/time part. In your examples, the seconds are zero, so that is what you get.
Postgres does support what you want, using the perhaps unintuitive name epoch. Epoch returns the number of seconds. For an date or datetime value, this is the number since 1970-01-01 (the beginning of Unix time). For a time or interval it is the total number of seconds during the period. So:
select extract(epoch from time '00:02:00')
returns 120.
Surprisingly, the documentation for epoch doesn't mention that it works on the time data type. The functionality is entirely consistent with what the function does. Either the documentation (which is generally quite excellent) overlooks time; or time is treated as an interval.
For a column in a table, you would just do:
SELECT EXTRACT(EPOCH FROM total_time)
FROM tr_empl_quiz;
or:
SELECT EXTRACT(EPOCH FROM CAST(total_time as time))
FROM tr_empl_quiz;
Depending on what you want.

BQ Date_ADD bug

I notice that if i minus 5 hours with date_add function - it's not take the date one day back
if it's should like this example
however if i wrapped it with date function so than it's showing the correct date.
select
DATE_ADD(timestamp('2014-10-26 04:00:00'), -5, "HOUR") as est_timstamp,
date(DATE_ADD(timestamp('2014-10-26 04:00:00'), -5, "HOUR")) as est_date
I will be glade to hear how can use Date_Add and still remain with timestamp type
and the day is moving back.
OK I think I know what is happening.
The query is returning a UTC timestamp. However, the BigQuery Web UI is rendering that timestamp in your timezone. One way to get around this would be to cast the timestamp to a string.