Postgresql How extract date - postgresql

I need get only the date from now() at my time zone, I have this query:
SELECT now() AT TIME ZONE 'America/Santiago'
And I'm getting something like this "2015-06-08 23:59:34.142569"
but I need extract only the date, how can I get it?
Thanks.

If you want the server's date,
SELECT current_date;
If you need the date for any timestamp, eg the one you've gotten into your timezone, use date().
SELECT date(now() AT TIME ZONE 'America/Santiago');
Docs: http://www.postgresql.org/docs/current/static/functions-datetime.html

For postgres you want
select current_date;
if you need to extract any of those fields out of the returned value you can use extract
EXTRACT (field FROM source)

Related

How to change the timestamp format in Postgresql to extract day part of the str?

I have create a datetime with type timestamp. datetime timestamp NOT NULL I am not sure why the output is like this:
I want to extract the day part. I have tried these different approach but in both cases I am getting an error. How can I fix it?
extract(DAY FROM TIMESTAMP min(to_char(u.datetime ,'YYYY-MM-DD HH24:MI'))::timestamp)
EXTRACT(DAY FROM TIMESTAMP min(to_char(u.datetime ,'YYYY-MM-DD HH24:MI')))
date_part('day', min(to_char(u.datetime ,'YYYY-MM-DD HH24:MI')))
As mentioned in response I modified query to be like below and it does work.
extract(day from MIN(datetime)) as Day
All you need is:
select *, extract(day from activated_at) as Day from yourTable;
What you are seeing is a timestamp formatted as text for the display. Underlying data is timestamp as you said, directly use it.

Need help to identify the actual date time format

I have been struggling since few days to identify correct format for sample input date:
2020-01-30 14:39:25.022000 +00:00:00
Background :
I am working on data migration with postgres. And, have identified that in DB, the datetime related fields like 'modifiedAt' have datatype as varchar, which is bit weird. These records have values in above mentioned format.
I have fixed it in new postgres DB instance with datatype as timestamp. After migrating data, these fields are showing datetime in following format:
2020-01-30 14:39:25
How can I format above date to give output in this dateformat 2020-01-30 14:39:25.022000 +00:00:00?
Any help will be appreciated.
First you want to use timestamptz not timestamp. timestamptz does not actually store the time zone, it just makes the value time zone aware. For more information on this see:
Time Stamps 8.5.1.3. Time Stamps.
Second it looks like you did something like timestamp(0) which reduced the precision to whole seconds. If you leave the precision alone you get:
select '2020-01-30 14:39:25.022000 +00:00:00'::timestamptz;
timestamptz
-----------------------------
01/30/2020 06:39:25.022 PST
-- I am in PST so the value gets rotated to that time zone for display.
-- If you want something closer to you desired output, then:
select to_char('2020-01-30 14:39:25.022000 +00:00:00'::timestamptz AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS.USOF');
to_char
-------------------------------
2020-01-30 14:39:25.022000+00
While your timestamp strings do appear to have a time zone component, it is +00, which is the default for timestamps. So, you might be able to just use TO_TIMESTAMP here with an appropriate format mask covering microseconds:
WITH yourTable AS (
SELECT '2020-01-30 14:39:25.022123 +00:00:00'::text AS ts
)
SELECT TO_TIMESTAMP(LEFT(ts, 26), 'YYYY-MM-DD HH24:MI:SS.US')
FROM yourTable;
Demo
The output value from the demo above is 2020-01-30 14:39:25.022123+00

Redshift: converting timestamp to date in specific time zone

Using psycopg to connect to a Redshift DB I am trying to group by date (in a specific time zone, given at query time). All date_times are stored without time zone (and correspond to UTC timestamps).
I have been trying the following:
SELECT DISTINCT DATE(TIME_ZONE(%(time_zone)s, date_time_dim.timestamp)) AS date,
SUM(meals.sugar_in_g) AS total_sugar
FROM meals
INNER JOIN date_time_dim
ON meals.created_at = date_time_dim.timestamp
WHERE meals.patient_id=%(patient_id)s
AND date_time_dim.timestamp >= %(utc_start_date_time)s
AND date_time_dim.timestamp <= %(utc_end_date_time)s
GROUP BY date
ORDER BY date ASC;
with the following query dictionary:
utc_start_date_time UTC-converted date time
utc_end_date_time UTC-converted date time
patient_id an integer
time_zone, a string, ex: 'US/Hawaii'
The goal being to have one entry for each date (in the given time zone).
What I tried gives me:
function time_zone("unknown", timestamp without time zone) does not exist
What am I missing ?
Edit: This is the same with CONVERT_TIME_ZONE, with and without source time_zone, with and without type casting time_zone to VARCHAR.
The function in Redshift to return a timestamp in a different timezone is TIMEZONE() not TIME_ZONE() - see https://docs.aws.amazon.com/redshift/latest/dg/Date_functions_header.html

Convert bigint data type to timestamp (and subsequently to date) in redshift

I need to convert the value stored in a bigint column to a date field. The first step of the conversion involves converting it to timestamp, and subsequently use the TRUNC method to convert this column to a date value.
However, my query is failing while converting the bigint value to timestamp.
The error that I'm getting is:-
Amazon Invalid operation: cannot cast type bigint to timestamp without time zone;
The query I'm trying for now is something like this:-
select ts::timestamp from events limit 1;
I was able to avoid the time zone error by using the method described in this thread: https://stackoverflow.com/a/36399361
My dates are based on epochs, and I was able to do the following:
SELECT
(TIMESTAMP 'epoch' + contract_start_date * INTERVAL '1 Second ')
FROM
table_name
SELECT TIMESTAMP 'epoch' + {column of bigint}/1000 * INTERVAL '1 second' as adate FROM tbl
If you are starting with a POSIX timestamp, and trying to get a timezone aware datetime value, you will need to supply a timezone - even if you later want to truncate the time part away. I'm not familiar with redshift, but perhaps there is a way to specify you mean UTC.

postgres query with current_date is not working

Below is my query in postgres
select dns_time,update_time from dns_lookup where update_time=current_date;
Here update_time is of type timestamp with time zone.
I have tried this one also.
select dns_time,update_time from dns_lookup where update_time like current_date;
Then also getting no results.
I have records with current_date values.But it is not showing me any records.How to compare timestamp with time zone value with current_date.please help me
Thank you.
First: LIKE on a date or timestamp column does not make any sense.
A timestamp contains a time part so a comparison with a plain date won't work. You need to remove the time from the timestamp by casting it to a date:
where cast(update_time as date) = current_date;
Note that this will void the usage of an index on update_time. If that is a problem you can create a function based index on that expression.
You might be trying to compare a timestamp field with the Date field.
Try casting timestamp to Date
select dns_time,update_time from dns_lookup where update_time::Date=current_date;
Hope this helps.