Postgres: How do I format an int timestamp as readable date string? - postgresql

Say I have a column that contains unix timestamps - an int representing the number of seconds since the epoch. They look like this: 1347085827. How do I format this as a human-readable date string in my SELECT query?

Postgresql has a handy built-in function for this: to_timestamp(). Just wrap that function around the column you want:
Select a, b, to_timestamp(date_int) FROM t_tablename

Related

Syntax for Combining BETWEEN and LIKE

I have a syntax, but it doesn't work.
Here is my query:
SELECT *
FROM aqua.reading
WHERE
CAST(reading.pres_date AS VARCHAR)
BETWEEN LIKE '2022-10-18%' AND LIKE '2022-10-18%'
it says:
ERROR: type "like" does not exist
LINE 1: ... WHERE CAST(reading.pres_date AS VARCHAR) BETWEEN LIKE '2022...
^
SQL state: 42704
Character: 77
I am trying to get all the data with timestamp and timezone and implement a date range
Don't compare dates (or timestamps) as strings. Compare them to proper date (or timestamp) values. Given the fact that you use the same "date" but with a wildcard at the end, I am assuming(!) that pres_date is in fact a timestamp column and you want to find all rows with a given date regardless of the time value of the timestamp.
The best approach is to use a range query with >= ("greater than or equal) on the lower value and < (strictly lower than) on the next day:
SELECT *
FROM aqua.reading
WHERE reading.pres_date >= DATE '2022-10-18'
AND reading.pres_date < DATE '2022-10-19'
Alternatively you can cast the timestamp to a date and use the = operator if you really want to pick just one day:
SELECT *
FROM aqua.reading
WHERE cast(reading.pres_date as DATE) = DATE '2022-10-18'
However that will not make use of a potential index on pres_date so is likely to be slower than the range query from the first solution

Splunk: Extract string and convert it to date format

I have such events:
something;<id>abc123<timeStamp>2021-12-10T23:10:12.044Z<timeStamp>2021-12-10T23:08:55.278Z>
I want to extract the Id abc123 and the two timeStamps.
index = something
|rex field=_raw "id>(?<Id>[0-9a-z-]+)"
|rex "timeStamp>(?<timeStamp>[T0-9-\.:Z]+)"
| table _time Id timeStamp
This works with the query above. But what I struggle now is to convert the timeStamp-string to date format to get at the end the min(timeStamp) extracted in order to compute the difference between the event's _time and the min(timeStamp) by the id field. I am struggling because of the special format of the timestamp with T and Z included in it.
There's nothing special about those timestamps - they're in standard form. Use the strptime function to convert them.
index = something
|rex field=_raw "id>(?<Id>[^\<]+)"
|rex "timeStamp>(?<timeStamp>[^\<]+)"
| eval ts = strptime(timeStamp, "%Y-%m-%dT%H:%M:%S.%3N%Z")
| eval diff = ts - _time
| table _time Id timeStamp diff
Check out strftime.org, and the related strptime function used with eval
Something on the order of this (pulled the microseconds out of your rex, since Unix epoch time has no concept of subsecond intervals):
| rex field=_raw "timeStamp\>(?<timeStamp>[^\.]+)\.\d+Z"
| eval unixepoch=strptime(timeStamp,"%Y-%m-%dT%H:%M:%S")

how to cast a timestamp into a date format in kdb

I am trying to cast a column of a timestamp into date format
eventTimestamp
2016.11.02D04:25:01.599000000
Into:
eventTimestamp
2016.11.02
using update"D"$column from table
does not work. I guess I need to parse it out of the string first!
The use of upper case letters for casting is used on string inputs as you say the cast you're looking for is as follows
q)show tab:([]100?0p;100?0t)
q)tab
x x1
------------------------------------------
2001.03.18D08:40:47.804237904 21:10:45.900
2001.10.11D22:11:37.961901872 20:23:25.800
2001.10.06D22:58:22.399235216 19:03:52.074
2002.11.27D20:28:07.114942080 00:29:38.945
2003.12.31D10:15:38.085363056 04:30:47.898
// Cast the timestamp column to date
q)update `date$x from tab
x x1
-----------------------
2001.03.18 21:10:45.900
2001.10.11 20:23:25.800
2001.10.06 19:03:52.074
2002.11.27 00:29:38.945
2003.12.31 04:30:47.898

Convert packed DB2 iseries value to YYYY-MM-DD

I'm trying to select records from a DB2 Iseries system where the date field is greater than the first of this year.
However, the date fields I'm selecting from are actually PACKED fields, not true dates.
I'm trying to convert them to YYYY-MM-DD format and get everything greater than '2018-01-01' but no matter what I try it says it's invalid.
Currently trying this:
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYY-MM-DD') >= '2018-01-01';
it says expression not valid using format string specified.
Any ideas?
char(dateshp) is going to return a string like '20180319'
So your format string should not include the dashes.. 'YYYYMMDD'
example:
select to_date(char(20180101), 'YYYYMMDD')
from sysibm.sysdummy1;
So your code should be
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYYMMDD') >= '2018-01-01';
Charles gave you a solution that converts the Packed date to a date field, and if you are comparing to another date field, this is a good solution. But if you are comparing to a constant value or another numeric field, you could just use something like this:
select *
from table1
where val = 145
and dateShp >= 20180101;

Converting date format in denodo database

I'm trying to convert value for DIM_DT_ID to MMddYY. I'm successful in doinf that. However, query fails because ultimately I'm comparing a character value to date here. Is there a way by which I can get value for DIM_DT_ID in MMddyy format and its data type still remains DATE ?
Here DIM_DT_ID
SELECT DIM_DT_ID
DIM_DT_ID >= FORMATDATE('MMddyy',ADDDAY(TO_date('yyyy-MM-dd','2016-12-21'), -25)); from abc;
Regards,
Ajay
In Denodo, to convert a string to a date field, use "to_date()" (which returns a date).
Then, don't convert back to a string, leave that field as a date (so don't use "Formatdate()", which returns a string).
So:
SELECT *
FROM MyTable
WHERE now() >= to_date('yyyy-MM-dd',myStringFieldThatLooksLikeADate)
In my example, "now()" is a date, and so is the output of the to_date function... so you can do a comparison.
If you try to convert the date back to a string using formatdate, it won't work:
#This doesn't work:
SELECT *
FROM MyTable
WHERE now() >= formatdate('MMddyy',to_date('yyyy-MM-dd',myStringFieldThatLooksLikeADate))
It doesn't work because we are comparing a date ("now()") to a string.