Is there any way convert text to time in postgresql? - postgresql

Is there any way convert text to time type?
Code in postgresql as below:
to_char(localtimestamp(0),'HH24:mi:SS')
this way I will get the value like 15:15:20,but this type is varchar(or text).
How can I do to get the value in time type?
Thanks!

SELECT TO_TIMESTAMP('15:15:20', 'HH24:MI:SS')::TIME
Note that your very query (which returns local time) can be rewritten as
SELECT LOCALTIME
which would return TIME

Related

Can we alter type of epoc time stored as text column in postgresql

select to_timestamp(created)::timestamp from users limit 1;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
i am trying to convert epoc time column stored as string in postgresql to timestamp.But getting above error.Any suggestion.
There are two versions of the to_timestamp function, one which takes a float argument (expected to be a unix epoch offset) and the other which takes two text arguments, where the first argument is a text string, and the second maps the formatting of the first text argument.
In your case, you are storing the epoch offsets as text, but you basically want the first version of the function. You should be able to do something like:
select to_timestamp(created::float) from users limit 1;

How to truncate date in postgres?

I am using below condition to truncate date in postgres
to_date(to_char(trunc(appointment_date),'YYYYMMDD')||appointment_end_time,''YYYYMMDDHH24:MI:SS')AS tq
How I can use this in postgres ?
Strange data typing, sometimes requires strange, looking at least, queries. Try (see fiddle)
date_trunc('day',appointment_date)
+ substr(appoinment_end,12)::interval
As your to_char() call uses the format 'HH24:MI:SS' for the "time" column, you can cast that column directly to a time value, e.g. using the :: operator: appointment_end_time::time.
To build a new timestamp from the date part of the appointment_date and the time value, just add them:
appointment_date::date + appointment_end_time::time
So first the timestamp is converted to a date (that does not have a time), and then the time value is added to that, which yields a timestamp.
Note that to_date() returns a date so your code would remove the just added time part again. You would need to use to_timestamp() if you really want a timestamp as the result.
To answer the question's title "how to truncate date in Postgres?" (which in reality refers to a timestamp not a date): you can either cast it to a date (see above) or you can use date_trunc() (not trunc()) with a unit to which it should be truncated. However, date_trunc returns a timestamp not a date value, so you couldn't add a time to the result.

Convert varchar data to time format in postgresql

I currently have a range of transaction data, and I am trying to query the data for the sum of transactions within a certain time frame. One field, transaction_time is a varchar. An example of one of these times is "13:42", which corresponds to 1:42pm. I was experimenting around with to_timestamp, but I have been unable to figure it out. Would anyone have any idea as to how to alter this varchar to represent time data.
Kind regards
Casting can be done two ways:
select cast('12:47' as time)
and
select '12:47'::time
cast it to time.
select cast('13:42' as time)

Converting string timestamp into date

I have dates in a postgres database. The problem is they are stored in a string field and have values similar to: "1187222400000" (which would correspond to 07.08.2007).
I would like to convert them into readable dates usind some SQL to_date() expression or something similar but can't come up with the correct syntax to make it work.
There really isn't enough information here for a conclusion, so I propose this 'scientific-wild-ass-guess' to resolve your puzzle. :)
It appears this number is UNIX 'epoch time' in milliseconds. I'll show this example as if your string field had the arbitrary name, 'epoch_milli'. In postgresql you can convert it to a time stamp using this statement:
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + epoch_milli * INTERVAL '1 millisecond';
or using this built-in postgresql function:
SELECT to_timestamp(epoch_milli / 1000)
either of which, for the example '1187222400000', produces the result
"2007-08-15 17:00:00-07"
You can do some of your own sleuthing with quite a few values selected similarly to this:
SELECT to_timestamp(epoch_milli/1000)::DATE
FROM (VALUES (1187222400000),(1194122400000)) AS val(epoch_milli);
"Well, bollocks, man. I just want the date." Point taken.
Simply cast the timestamp to a date to discard the excess bits:
SELECT to_timestamp(epoch_milli / 1000)::DATE
Of course its possible that this value is a conversion or is relative to some other value, hence the request for a second example data point.

Is ISO8601 the best date-format for PostgreSQL jsonb when i want to filter by the date?

I'm new to PostgreSQL and I have the following question:
I have a table with just an id-column and a data-column, which uses the jsonb-type. Inside the jsonb-object I have a datetime field. I read in various posts, that I should use the ISO-8601 dateformat to store in the DB.
I want to filter my table by date like this:
SELECT * FROM table WHERE data->'date' > '2016-01-01T00:00'
Is this really the best date-format for this purpose?
Thanks in advance :)
IMHO Your query should produce
ERROR: operator does not exist: jsonb > timestamp with time zone
If I get it right. In case you change -> to ->> it should get a text value instead of jsonb field (which is also not comparable to timestamp).
It should be smth like
SELECT * FROM table WHERE (data->>'date')::timestamptz > '2016-01-01T00:00' to work
The big advantage of that format is that string order corresponds to date order, so a comparison like the one you quote in your question would actually work as intended.
A second advantage is that a timestamp in that format can easily be converted to a PostgreSQL timestamp with time zone value, because the type input function understands this format.
I hope you are not dealing with dates “before Christ”, because it wouldn't work so easily with those.