Postgresql sql query convert time in second [duplicate] - sql

This question already has an answer here:
Convert timestamp column values to epoch in PostgreSQL select query
(1 answer)
Closed 3 years ago.
I have MySQL DB and i am using following syntax to convert my datetime type in second
select UNIX_TIMESTAMP(created_at) as time_sec, blah..blah..
But now i have other postgresql DB which has timestamp with time zone type so how do i convert that time in second with my select query?
2018-04-18 18:27:48.96283+00 This is this format of timestamp in table which i want to convert in seconds

you can use
select extract(epoch from created_at) as time_sec from tsepok
db<>fiddle here
more docs here

Related

How to pass a value from the Sqlite DB to julianday() [duplicate]

This question already has answers here:
Difference in seconds between timestamps in Sqlite3
(2 answers)
Closed 2 years ago.
I need to calculate the difference between two dates represented as TEXT. julianday() can do this, but it accepts a literal:
select(julianday('2015-01-01 12:00:00') - julianday('2015-01-01 13:00:00'))
But if I have a table:
CREATE TABLE "time_u" (
"time_in" TEXT,
"time_out" TEXT
);
INSERT INTO time_u (time_in, time_out)
VALUES ('2007-01-01 10:00:50', '2007-01-01 11:00:00')
how can I do something like? :
julianday(time_u.time_out-time_u.time_in)
in other words take values directly from the database and subtract them as a date
You can turn each date to Julian days, then substract:
select
t.*,
julianday(time_out) -julianday(time_in) day_diff
from time_u t
If you want something more accurate, you can turn the datetimes to epoch timestamp. Say you want the difference in minutes, then:
select
t.*,
(strftime('%s', time_out) - strftime('%s', time_in)) / 60.0 day_diff
from time_u t

Is there a way to see the type of a SQL expression? [duplicate]

This question already has answers here:
Show query result column types (PostgreSQL)
(3 answers)
Closed 3 years ago.
Say I select a SQL expression:
my_db=> select cast('2019-12-12' as date) + cast('10:00' as time);
?column?
---------------------
2019-12-12 10:00:00
(1 row)
Is there a way for me to view what type that column is? E.g. is it a timestamp with time zone or a timestamp without time zone;.
So ideally something like:
my_db=> select type(cast('2019-12-12' as date) + cast('10:00' as time));
?column?
-----------------------------
timestamp without time zone
(1 row)
It may be that my example works only in postgresql, but I'd like to know if there is a standard SQL way of doing it, and if not if there's a postgresql way of doing it.
Use pg_typeof(<expression>) in Postgresql.
So for your example:
my_db=> select pg_typeof(cast('2019-12-12' as date) + cast('10:00' as time));
pg_typeof
-----------------------------
timestamp without time zone
(1 row)

Convert date to MM//DD HH/MM [duplicate]

This question already has answers here:
Sql Server string to date conversion
(16 answers)
Closed 5 years ago.
How do you convert date to MM/DD HH/MM
example: 04-05 12:42
Since you are on SQL Server 2012, use FORMAT:
SELECT FORMAT(GETDATE(), 'MM-dd HH:mm')
Try using the MONTH, DAY, HOUR, MINUTE functions in T-SQL and concatenate your results into a string form like:
CONCAT(STRING(HOUR(created_at)), ':', RIGHT(STRING(MINUTE(created_at)), 3)) AS hour_min
Where the created_at is your timestamp column. Hope this helps!

Show date with time when querying oracle DATE data type [duplicate]

This question already has answers here:
Oracle. How to output date and time?
(5 answers)
Closed 6 years ago.
When querying an oracle date, I see only the date - not the time. Example
select D from ALIK_TZ
Result:
D
01-JUN-16
How can I see the time as well?
Use TO_CHAR with time format. Example below.
Consider the date being saved in column named D, than the following query would get the time as well:
select to_char(D, 'DD-MON-YYYY HH24:MI:SS') D_TIME, D from ALIK_TZ;
Result:
D_TIME D
01-JUN-2016 06:14:04 01-JUN-16
Since you have answered it yourself. Just to add the reason as why you are getting the result in the format DD-MON-YY format. Oracle docs says:
For input and output of dates, the standard Oracle date format is
DD-MON-YY
Also note that Oracle uses the TO_CHAR function internally whenever a DATE value is displayed, Oracle will call TO_CHAR automatically using the default DATE format which is DD-MON-YY.
Hence you need to change it using the TO_CHAR function to get the date in the format which you want.

Convert Unix time from IPFix to datetime in SQL [duplicate]

This question already has answers here:
converting Epoch timestamp to sql server(human readable format)
(2 answers)
SQL: Using DATEADD with bigints
(6 answers)
Closed 8 years ago.
I have an IPFix timestamp which appears to be in Unix time. My SQL code is:
update
temp_ipfix
set FlowStartTimeDT = dateadd(S, FlowStartTime, '1970-01-01 00:00:00')
Where FlowStartTime is the column containing the Unix timestamp.
A sample timestamp is 1410435197783
The error I'm getting is:
Arithmetic overflow error for type int, value = 1410435197783.000000.
The statement has been terminated
I tried the suggestions linked to this page, but the results are incorrect. Month and Day are correct, but year is 2034.
select dateadd(s, (flowstoptime/1000), '1990-01-01 00:00:00') from temp_unix