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')
Related
I have a date time field which is coming from an external system in UTC format 2022-01-02T08:00:00.000+00:00. This value should be queried in DB2 to determine whether the record exists or not. The date stored in DB2 is in the format 2022-01-01 08:00:00.000 Is there any way to convert the incoming date in the format 2022-01-01 08:00:00.000 ?
The final query should be something like
select * from table where changedate = '2022-01-02T08:00:00.000+00:00'
Db2 doesn't store timestamps in a string format. Some binary format is used for that.
So, if I got you right, your question should be changed to "how to convert YYYY-MM-DDTHH24.MI.SS.FF3XXXXXX string representation of timestamp to the timestamp data type".
Unfortunately, there is no such a built-in pattern in the TO_DATE / TIMESTAMP_FORMAT function, but you can use the following expression. T column has the timestamp data type, and you may use this expression in the select * from table where changedate = ... statement.
SELECT
S
, TO_DATE (TRANSLATE (SUBSTR (S, 1, 23), ' ', 'T'), 'YYYY-MM-DD HH24:MI:SS.FF3')
+ CAST (TRANSLATE (SUBSTR (S, 24), '', ':', '') || '00' AS DEC (6)) AS T
FROM
(
VALUES
'2022-01-02T08:00:00.000+00:00'
, '2022-01-02T08:00:00.000+03:30'
, '2022-01-02T08:00:00.000-03:30'
) T (S)
S
T
2022-01-02T08:00:00.000+00:00
2022-01-02-08.00.00.000000
2022-01-02T08:00:00.000+03:30
2022-01-02-11.30.00.000000
2022-01-02T08:00:00.000-03:30
2022-01-02-04.30.00.000000
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.
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'm fairly inexperienced with SQL let alone PostgreSQL so appreciate your help.
Let's say we have a varchar column called ID that is a combination of IP address and a unix epoch time string.
So when I run this query:
SELECT substring(b.id from char_length (r.IP_address)+2 for 10), b.id, r.ip_address
FROM bq b
INNER JOIN event r ON r.visitor_id::TEXT = b.id::TEXT
LIMIT 3;
Output
substring id ip_address
1460854333 97.128.39.256.1460854333288493 97.128.39.256
So the output of the substring is a unix time stamp but it is in a varchar format. How do I convert the varchar into a unix timestamp? Ultimately I am going to convert that time stamp into date I just don't think I can directly go from the varchar of a unix string to a date?
As Terra said, use the to_timestamp() function:
# select to_timestamp('1460854333') ;
to_timestamp
------------------------
2016-04-16 17:52:13-07
(1 row)
If a date is the type you're after, just add the ::date cast:
# select to_timestamp('1460854333')::date ;
to_timestamp
------------------------
2016-04-16
(1 row)
The function is basically executing this:
select ('epoch'::timestamptz + '1460854333'::float * '1 second'::interval) ;
Following is my oracle update query that`s throwing error--
Update table Set col1 = To_date(:dateFill, 'mm/dd/yyyy hh24:mi:ss') Where Fil1 = :ID;
dateFill = 01/05/2012,
ID= 15
this statement is executing in a procedure,
error -:ORA-01722: invalid number(date field)
Can someone tell me why is 'select To_date('01/05/2012 00:00:00', 'mm/dd/yyyy hh24:mi:ss') from dual;' giving me result like '05-JAN-2012 00:00:00'.???
Please suggest me some answers.
If dateFill = 01/05/2012, why are you specifying a date format that includes "hh24:mi:ss"?
An ORA-01722 error occurs when an attempt is made to convert a character string into a number, and the string cannot be converted into a valid number. Valid numbers contain the digits '0' through '9', with possibly one decimal point, a sign (+ or -) at the beginning or end of the string, or an 'E' or 'e' (if it is a floating point number in scientific notation).
to_date always work with 'character' ie
to_date(char[,'format'[,nls_lang])
Your variable 'dateFill' is of Number datatype. Cast or convert this 'dateFill'
field into varchar and things will work.
Can someone tell me why is 'select To_date('01/05/2012 00:00:00',
'mm/dd/yyyy hh24:mi:ss') from dual;' giving me result like
'05-JAN-2012 00:00:00'.???
how a date displays is down to your client / Nls date format setting:
SQL> select To_date('01/05/2012 00:00:00','mm/dd/yyyy hh24:mi:ss') from dual;
TO_DATE('01/05/20120
--------------------
05-jan-2012 00:00:00
SQL> alter session set nls_date_format='mm/dd/yyyy hh24:mi:ss';
Session altered.
SQL> select To_date('01/05/2012 00:00:00','mm/dd/yyyy hh24:mi:ss') from dual;
TO_DATE('01/05/2012
-------------------
01/05/2012 00:00:00
SQL>