date and time concatenation in postgres - postgresql

I wanted to get the only the hour of the time and concatenate it with the date.
here's my query
SELECT distinct TOTALIZER_METER_READINGS.date + to_char(TOTALIZER_METER_READINGS.time ,'HH')
FROM TOTALIZER_METER_READINGS
is there any other way to get the hour of the time without turning it into text?

+ is the operator to add numbers, dates or intervals.
The string concatenation operator in SQL is ||.
As you are storing date and time in two columns rather then using a single timestamp column, I would convert them to a single timestamp value, then apply to_char() on the "complete" timestamp:
Adding a time to a date returns a timestamp that can then be formatted as you want:
SELECT distinct to_char(TOTALIZER_METER_READINGS.date + TOTALIZER_METER_READINGS.time, 'yyyy-mm-dd HH')
FROM TOTALIZER_METER_READINGS

You can use EXTRACT (or the date_part function):
SELECT EXTRACT(hour FROM current_timestamp);
The result type is double precision.

Related

How to extract data from DWH on a certain date? Is there the special pattern for this case in Postgres SQL?

Usually we use EXTRACT (FROM YEAR date_column) = 2000 (let it be 2000 year). Also we can add EXTRACT (MONTH FROM date_column) = 1 (let it be January). Also we can extract a day - EXTRACT (DAY FROM date_column) = 5 (let it 5). But is it possible to use the pattern for this data? How does it look like in Postgres SQL
Say we have the table Shipment, the columns - date_payment, quantity, sum.
I'd like to get the table that content all shipments for 01.01.2020
How to query this table with data format 'YYYY-MM-DD', not using EXTRACT-function?
If the date column is a date type then:
SELECT * FROM some_table WHERE date_col = '2020-01-01';
If the column is timestamp or timestamptz then:
SELECT * FROM some_table WHERE date_trunc('day', date_col) = '2020-01-01'
Beware that with timestamptz time zones come into play when doing the date_trunc. From here date_trunc:
When the input value is of type timestamp with time zone, the truncation is performed with respect to a particular time zone; for example, truncation to day produces a value that is midnight in that zone. By default, truncation is done with respect to the current TimeZone setting, but the optional time_zone argument can be provided to specify a different time zone. The time zone name can be specified in any of the ways described in Section 8.5.3.
For a timestamp value per the above link:
A time zone cannot be specified when processing timestamp without time zone or interval inputs. These are always taken at face value.

Postgres Double Precision to Date

I'm looking to cast/convert a decimal data into date data. I've looked online and am still struggling. Can you help? "value" is a double precision, and here I convert it to varchar. From that point on, I've tried using convert but I'm still unable to get a date value.
Thanks in advance!
select cast(value as varchar(8) )date_value, value,
For example: The value 43099 should read 12/30/2017.
Guessing from your meager example, you might want this:
SELECT date '1899-12-30' + 43099; -- returns date '2017-12-30'
You need to cast your value column to integer for this:
SELECT date '1899-12-30' + value::int;
Meaning, the number might represent the count of days since 1900 (with an off-by-2 error I can't explain).
Adding an integer to a date means adding the number of days.
If your value can have fractional digits, you can compute a timestamp in similar fashion:
SELECT timestamp '1899-12-30' + interval '1 day' * value;

query to fetch records between two date and time

I have been using postgreSQL. My table has 3 columns date, time and userId. I have to find out records between the given date and time frame. Since date and time columns are different, 'BETWEEN' clause is not providing valid results
Combine the two columns into a single timestamp by adding the time to the date:
select *
from some_table
where date_column + time_column
between timestamp '2017-06-14 17:30:00' and timestamp '2017-06-19 08:26:00';
Note that this will not use an index on date_column or time_column. You would need to create an index on that expression. Or better: use a single column defined as timestamp instead.

Convert Time Zone in Amazon Redshift

I have a datetime column in a redshift table. It contains date in UTC format.
So, when I am querying based on time_zone like UTC+5:30/ UTC+4:30, I want to convert the table datetime column to chosen time_zone and proceed further. It gives me wrong result.
Using this method :
CONVERT_TIMEZONE ( ['source_zone',] 'target_zone', 'timestamp')
Query Type 1: Wrong input, correct answer
SELECT id, convert_timezone('UTC+5:30','UTC', date) as converted_time, ingest_date
FROM table_name
WHERE conditions
Query Type 2: Correct input, wrong answer -> It again subtracting 5:30 from the date in column
SELECT id , convert_timezone('UTC','UTC+5:30',ingest_date) as converted_time, ingest_date
FROM table_name
WHERE conditions
Query Type 3: Wrong input, correct answer
SELECT id, convert_timezone('UTC','UTC-5:30',ingest_date) as converted_time, ingest_date
FROM table_name
WHERE conditions
How to convert / parse the UTC column into selected timezone?
In Redshift, CONVERT_TIMEZONE interprets the offset as the time from UTC. For example, an offset of +2 is equivalent to UTC–2, and an offset of -2 is equivalent to UTC+2. CONVERT_TIMEZONE does not use the prefix string when calculating the offset, even if the string represents a valid time zone. For example, 'NEWZONE+2’, 'PDT+2', and'GMT+2' all have the same result. If a string does not include an offset, then it must represent a valid time zone or CONVERT_TIMEZONE returns an error.
For converting time_zone, if you send "UTC+5:30/UTC-4:30", amazon interpreting it as "UTC-5:30 / UTC+4:30".
Now you can convert + into - and vice versa before sending it to redshift.
(http://docs.aws.amazon.com/redshift/latest/dg/CONVERT_TIMEZONE.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.