Extracting Time from Timestamp in SQL - sql

I am using Redshift and am looking to extract the time from the timestamp.
Here is the timestamp: 2017-10-31 23:30:00
and I would just like to get the time as 23:30:00
Is that possible?

In Redshift you can simply cast the value to a time:
the_timestamp_column::time
alternatively you can use the standard cast() operator:
cast(the_timestamp_column as time)

Please go through this link
http://docs.aws.amazon.com/redshift/latest/dg/r_Dateparts_for_datetime_functions.html
timezone, timezone_hour, timezone_minute
Supported by the DATE_TRUNC function and the EXTRACT for time stamp
with time zone (TIMESTAMPTZ)
Examples is here
select extract(minute from timestamp '2009-09-09 12:08:43');
select extract(hours from timestamp '2009-09-09 12:08:43');

Related

Remove UTC from a TIMESTAMP field in SQL(BigQuery)

I have been able to convert a TIMESTAMP field that is in UTC to CST using:
SELECT
TIMESTAMP(started_at) AS UTC,
TIMESTAMP_SUB(TIMESTAMP (started_at), INTERVAL 5 HOUR) AS CST
This returns:
ROW
UTC
CST
1
2020-05-17 13:07:22 UTC
2020-05-17 08:07:22 UTC
The second TIMESTAMP displays the correct date and time but UTC still shows. What is the simplest way to replace 'UTC' with 'CST' or, alternatively, remove 'UTC' altogether since I don't need the designation in the field itself?
You can use a plain sql replace function inline
select replace(TIMESTAMP_SUB(TIMESTAMP (started_at), INTERVAL 5 HOUR), 'UTC','') AS CST
The above searches the result of your expression (provided in your question) for 'UTC' and replaces it with nothing ''. You could also replace it with 'CST' as you noted.
Obligatory warning: I believe your query will only be correct half the year as long as DST is observed? You might want to look into sys.time_zone_info and its reference on the MSDN.
My advise is to convert the timestamp to a datetime local value:
select datetime(started_at, 'America/Chicago') as started_at_cst
Note that the time zone is not stored in the data value. Instead, this encodes the value in the string.
If you want to include the time zone value, I have found that the best approach is to use a string (argghh!). The following constructs a string:
format_timestamp('%F %X%z', started_at, 'America/Chicago') as started_at_str
which can be converted easily into a timestamp for date/time calculations:
timestamp(started_at_str)

Extracting Time from Timestamp in Redshift

I am using Redshift and am looking to extract the time from the timestamp.
Here is the timestamp: 2017-10-31 23:30:00
and I would just like to get the time as 23:30:00
I tried using cast(the_timestamp_column as time) but ran into this error message:
error message: Function ""time"(timestamp without time zone)" not supported.
Use to_char with an appropriate format mask to extract the time component of your timestamp:
select to_char(the_timestamp_column, 'HH24:MI:SS')
from your_table;
Demo

Extract date,month,year and month name from the unix timestamp with postgresql

I use postgres for the rails app and I have a unix timestamp in postgresql db. I have a requirement to select and group by the dd-mm-yyyy and by month name.
Consider I have the following unix timestamp
1425148200
and I would need to change this to datetime and I used to_timestamp which returned
2015-02-28 18:30:00 UTC
and I tried to convert the datetime to local timezone using
::timestamp without time zone AT TIME ZONE 'IST'
but that did not give time in required timezone and instead it returned
2015-02-28 16:30:00 UTC
and I tried to get the date part using ::date which returned
Sat, 28 Feb 2015
So please help me get the dd-mm-yyyy in specified timezone and month name(March) from the unix timestamp.
Thanks in Advance!
select to_char(to_timestamp('1425148200')::timestamptz at time zone 'UTC-5:30','DD-MM-YYYY & of course Month')
01-03-2015 & of course March
It is postgres mistake I guess
according to http://www.postgresql.org/docs/7.2/static/timezones.html

ORACLE Casting DATE to TIMESTAMP WITH TIME ZONE WITH OFFSET

I need to cast a DATE value in a query to a TIMESTAMP WITH TIME ZONE, but currently I'm getting the TimeZone Region ('Europe / Paris') which is not valid to be used by EF.
For example, when doing this:
select CAST(FECHA AS TIMESTAMP WITH TIME ZONE) from test;
I currently get this output:
07/03/14 09:22:00,000000000 EUROPE/PARIS
But I need it to be like:
07/03/14 09:22:00,000000000 +01:00
Any idea how to accomplish this?
You can cast the DATE to a TIMESTAMP, then use FROM_TZ to convert this timestamp to a timestamp with time zone:
SQL> SELECT from_tz(CAST (SYSDATE AS TIMESTAMP), '+01:00') tz FROM dual;
TZ
-------------------------------------------------
07/03/14 09:47:06,000000 +01:00
With #Vincent Malgrat solution you need to get the TIMEZONE_HOUR and then, format it to use in your query. I don't know if there is any chance to make it automatically.
I can suggest you to nest some functions. It is not the cleanest solution but it works for me
SELECT TO_TIMESTAMP_TZ(TO_CHAR(CAST(FECHAHORA AS TIMESTAMP WITH TIME ZONE), 'DD-MM-YY HH24:MI:SS TZH:TZM'), 'DD-MM-YY HH24:MI:SS TZH:TZM' )FROM TEST;
And the result will be something like
03/03/14 09:58:02,000000000 +01:00
Regards!
Use
ALTER SESSION SET TIME_ZONE = '+01:00';
before your SELECT

How to get the date and time from timestamp in PostgreSQL select query?

How to get the date and time only up to minutes, not seconds, from timestamp in PostgreSQL. I need date as well as time.
For example:
2000-12-16 12:21:13-05
From this I need
2000-12-16 12:21 (no seconds and milliseconds only date and time in hours and minutes)
From a timestamp with time zone field, say update_time, how do I get date as well as time like above using PostgreSQL select query.
Please help me.
There are plenty of date-time functions available with postgresql:
See the list here
http://www.postgresql.org/docs/9.1/static/functions-datetime.html
e.g.
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 16
For formatting you can use these:
http://www.postgresql.org/docs/9.1/static/functions-formatting.html
e.g.
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI') ...
To get the date from a timestamp (or timestamptz) a simple cast is fastest:
SELECT now()::date
You get the date according to your local time zone either way.
If you want text in a certain format, go with to_char() like #davek provided.
If you want to truncate (round down) the value of a timestamp to a unit of time, use date_trunc():
SELECT date_trunc('minute', now());
This should be enough:
select now()::date, now()::time
, pg_typeof(now()), pg_typeof(now()::date), pg_typeof(now()::time)