I am formatting an input that I get in Timestamp datatype and sending the output as a text in the below format
2020-07-30 10:45:23.638 PM
For this I'm using the below query
select to_char(input_timestamp, 'YYYY-MM-DD HH:MI:SS.MS AM');
But I want the microsecond part truncated to 2 digits instead of 3 digits. The output will be in TEXT datatype. Expecting help on this.
Try this:
select
left(to_char(input_timestamp::timestamp(2), 'YYYY-MM-DD HH:MI:SS.MS'), 22) ||
to_char(input_timestamp, 'FM AM');
First round to two microsecond' digits, convert to string, chop to 22 characters and then concatenate the meridiem indicator.
Related
I have a column that return seconds from a task and I'd like to convert it into hours and minutes
Example: if the column has 16200 seconds I'd like to get the result as 4h30m
Also, need to get sum of all the values as hr and min,....sum(column name)=sum(hr)sum(min)
If your seconds column is a numeric type, you can use this:
SELECT TO_CHAR((16200 || ' second')::interval, 'fmHH24hfmMIm')
If its a string type, use this:
SELECT TO_CHAR('16200'::interval, 'fmHH24hfmMIm')
Of course, replace 16200 with your appropriate column name.
Precede HH and MI with fm for Fill Mode. This will remove the preceding zero values from hour and minute.
Output:
4h30m
See Fiddle.
To get the SUM of all values, add the SUM function to your seconds column like so:
SELECT TO_CHAR(SUM(seconds_column::interval), 'fmHH24hfmMIm') FROM test_table
See Fiddle.
Below is a script i am trying to run in Presto; Subtracting today's date from an integer field I am attempting to convert to date. To get the exacts days between. Unfortunately, it seems the highlighted block does not always convert the date correctly and my final answer is not correct. Please does anyone know another way around this or a standard method on presto of converting integer values to date.
Interger value in the column is in the format '20191123' for year-month-date
select ms, activ_dt, current_date, date_diff('day',act_dt,current_date) from
(
select ms,activ_dt, **CAST(parse_datetime(CAST(activ_dt AS varchar), 'YYYYMMDD') AS date) as act_dt**, nov19
from h.A_Subs_1 where msisdn_key=23480320012
) limit 19
You can convert "date as a number" (eg. 20180527 for May 27, 2018) using the following:
cast to varchar
parse_datetime with appropriate format
cast to date (since parse_datetime returns a timestamp)
Example:
presto> SELECT CAST(parse_datetime(CAST(20180527 AS varchar), 'yyyyMMdd') AS date);
_col0
------------
2018-05-27
You can use below sample query for your requirement:
select date_diff('day', date_parse('20191209', '%Y%m%d'), current_timestamp);
I have a column called PairDt, a string that contains a date value in the last 5 characters. I want to compare that date value with the date value in the Day column, which contains dates in the YYYY-MM-DD format.
PairDt Day
----------------------------------
DCS-CNY-Yunbi-42606 2016-08-24
DCS-CNY-Yunbi-42607 2016-08-25
DCS-CNY-Yunbi-42608 2016-08-26
DCS-CNY-Yunbi-42609 2016-08-27
DCS-CNY-Yunbi-42610 2016-08-28
How do I convert Day to a value?
I'm trying to isolate Date values in PairDt that does not match the date value in Days
This 5 digit number at the end of PairDt looks like number of days since December 30th 1899. To convert this number to date use DATEADD to add as many days. To convert a date to number, use DATEDIFF to calculate the number of days. Something like this code:
declare #PairDt varchar(50) = 'DCS-CNY-Yunbi-42606', #Day date = '2016-08-24'
select DATEADD(d, cast(right(#PairDt, 5) as int), '1899-12-30'), DATEDIFF(day, '1899-12-30', #Day)
Hello i'm trying to extract current day and month in a query. This query is filling table from csv file.
the file is named like this:
LOG_01_01_2018.csv
My query search for file:
LOG_1_1_2018.csv
With no Zero in front day and month. How to add Zero numbers?
Here is the code:
execute format ($f$COPY tmp_x FROM 'D:\Programs\PS\download_files_from_ftp_avtomat\files\LOG_%s_%s_%s.csv'
(header, FORMAT CSV, DELIMITER ',', NULL ' ', ENCODING 'WIN1251');
$f$,extract(day from now()),extract(month from now()),extract(year from now()));
One option uses LPAD:
execute format ($f$COPY tmp_x FROM 'D:\Programs\PS\download_files_from_ftp_avtomat\files\LOG_%s_%s_%s.csv'
(header, FORMAT CSV, DELIMITER ',', NULL ' ', ENCODING 'WIN1251');
$f$,
lpad(extract(day from now())::text, 2, '0'),
lpad(extract(month from now())::text, 2, '0'),
extract(year from now()));
The year would always be a fixed width four digit number, unless you plan to work with data which existed before computers were around.
https://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
extract(field from timestamp) returns double precision, so you either lpad zero to it (making the value text), or just use to_char with data mask, eg:
t=# select extract(day from now()), to_char(now(),'MM'),extract(year from now());
date_part | to_char | date_part
-----------+---------+-----------
19 | 01 | 2018
(1 row)
Looks shorter to use to_char():
SELECT to_char(now(),'DD')||'/'||to_char(now(),'MM')||'/'||to_char(now(),'YYYY')
I have a field that should be 6 digit character but it is numeric. I am using the following code to add the leading zero:
select CAST(CAST(CHD_OPEN_DATE AS FORMAT '9(6)') AS CHAR(9))
I'm using the following code to format this as a date:
cast(cast(lpad(to_char(CHD_OPEN_DATE),6,'0') as date format 'YYMMDD') as date format 'YYYY-MM-DD')
When using this date format 1990 comes up as 2090. Is there a work-around for this?
If your number has a YYMMDD format you can use the following to cast to a date without the need to cast to an intermediate string. Assuming a date range between 1930 and 2029:
SELECT 900331 AS CHD_OPEN_DATE,
Cast(CASE WHEN CHD_OPEN_DATE < 300000
THEN CHD_OPEN_DATE + 1000000
ELSE CHD_OPEN_DATE
END AS DATE)