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>
Related
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 tried to ruh this query in postgres :
Select to_char((select add_months (to_date ('10/10/2019', 'dd/mm/yyyy'), '11/11/2019') ) , 'dd/mm/yyyy') as temp_date
I got an error :
Function add_months (date, unknown) does not exist
Hint: no function matches the given name and argument types. You might need to add explicit type casts.
Please help
As documented in the manual there is no add_months function in Postgres
But you can simply add an interval:
select to_date('10/10/2019', 'dd/mm/yyyy') + interval '10 months'
If you need to format that date value to something:
select to_char(to_date('10/10/2019', 'dd/mm/yyyy') + interval '10 months', 'yyyy-mm-dd')
No one, even running on Oracle, has run the original query- at least not successfully. It appears that query is expecting to add two months together (in this case Oct and Nov). That is not what the function does. It adds an integer number of months to the specified date and returns the resulting date. As indicated in Postgres just adding the desired interval. However, if you have many occurrences ( like converting) of this the following implements a Postgres version.
create or replace function add_months(
date_in date
, n_months_in integer)
returns date
language sql immutable strict
as
$$
-- given a date and an integer for number of months return the calendar date for the specified number of months away.
select (date_in + n_months_in * interval '1 month')::date
$$ ;
-- test
-- +/- 6 months from today.
select current_date "today"
, add_months(current_date,6) "6 months from now"
, add_months(current_date,-6) "6 months ago"
;
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')
How can I change below expression to give me a output in format MMM d, yyyy given the below act_start_date_time is a date column in ORACLE.
select DECODE(MAX(A.NAME), NULL, NULL, max(act_start_date_time))
If act_start_date_time is a column of datatype date then you should not be passing it into the to_date() function. If you do:
to_date(act_start_date_time, 'YYYY/MON/DD HH:MI:SS')
then you are implicitly converting the date to a string using your session's NLS settings, so it's really:
to_date(to_char(act_start_date_time), 'YYYY/MON/DD HH:MI:SS')
which is
to_date(to_char(act_start_date_time, <NLS_DATE_FORMAT>), 'YYYY/MON/DD HH:MI:SS')
which will either error, give you the same date back, or give you a different date, depending on what your current NLS_DATE_FORMAT is and the original value you're looking at. For instance:
alter session set nls_date_format = 'DD-MON-RR';
select to_date(sysdate, 'YYYY/MON/DD HH:MI:SS') from dual;
TO_DATE(S
---------
17-AUG-02
so 2017-08-02 has been translated to [20]02-08-17. Which is not helpful.
To convert a date into a formatted string you need to use the to_char() function instead, e.g. something like:
decode(max(a.name),null,null, to_char(max(rca.act_start_date_time), 'YYYY/MON/DD HH:MI:SS'))
or
to_char(case when max(a.name) is not null then max(rca.act_start_date_time) end, 'YYYY/MON/DD HH:MI:SS')
or to get the format you first asked about use format mask 'FMMon DD, YYYY'. The FM modifier removes leading zeros from the day number. But with all of those, be aware that NLS settings still have an effect; the abbreviated month name will be shown in the session's date language; if you need to make sure it's always shown in a specific language you can supply that as the optional third argument to the function:
select to_char(sysdate, 'FMMon DD, YYYY', 'NLS_DATE_LANGUAGE=ENGLISH') from dual;
TO_CHAR(SYSDATE,'FMMO
---------------------
Aug 2, 2017
select to_char(sysdate, 'FMMon DD, YYYY', 'NLS_DATE_LANGUAGE=FRENCH') from dual;
TO_CHAR(SYSDATE,'FMMONDD,YYYY
-----------------------------
Août 2, 2017
Quick demo with fake data from CTEs and relying on the session language:
with a (id, name) as (select 1, null from dual union all select 2, 'Joe' from dual),
rca (id, act_start_date_time) as (select 1, sysdate from dual union all select 2, sysdate from dual)
select a.id,
max(a.name),
decode(max(a.name), null, null,
to_char(max(rca.act_start_date_time), 'YYYY/MON/DD HH:MI:SS')) as string1,
to_char(case when max(a.name) is not null then max(rca.act_start_date_time) end,
'YYYY/MON/DD HH:MI:SS') as string2,
to_char(case when max(a.name) is not null then max(rca.act_start_date_time) end,
'FMMon DD, YYYY') as string3
from a
join rca on rca.id = a.id
group by a.id;
ID MAX STRING1 STRING2 STRING3
---------- --- -------------------- -------------------- ---------------------
1
2 Joe 2017/AUG/02 11:03:47 2017/AUG/02 11:03:47 Aug 2, 2017
(Without your table structures or sample data I can't comment on whether the aggregation and conversion is appropriate, so adapt as needed for your real scenario...)
The date format models are listed in the documentation.
Assume a user tries to cast a number (year of birth) into a date.
select year_of_birth, cast(year_of_birth as date) from visit_occurrence
And gets this error :
cannot cast type smallint to date
What is the proper way?
e.g., cast(cast(YOB as string)+'-01-01' as date) does not work either.
use
select year_of_birth, to_date(cast(year_of_birth as text), 'YYYY') from visit_occurrence ;