PL/SQL - to_char within DECODE - sql

I have a DECODE statement that works fine without putting to_char function in
Select DECODE(info.make_date, NULL,'SELECT ALL',info.make_date) as "listItemKey"
However I need info.make_date to be in a specific date format, so I use to_char
Select DECODE(info.make_date, NULL,'SELECT ALL',to_char(info.make_date, 'MM/DD/YYYY')) as "listItemKey"
But when I do this I get Unexpected '<' as my JSON instead of the data I need to return. Is there a reason I can't set my info.make_date to the format I need here?

I guess an alternative could be as below. Please try and let me know if this works.
select case when info.make_date is null then 'SELECT ALL'
else TO_CHAR (info.make_date, 'MM/DD/YYYY')
end as "listItemKey"
However i would say to cast the date before using to_char with existing DECODE:
TO_CHAR (to_date(info.make_date, 'MM/DD/YYYY'), 'MM/DD/YYYY' )

Related

How to pass bash date into sql timestamp

Hello i have code like this:
...AND o.creation_date > TO_DATE($DATE_TODAY, 'YYYYMMDD')...
When i want to pass variable into function TO_DATE it works.
But in another script, I have code like this...
...AND o.creation_date > TO_TIMESTAMP( $DATE_TODAY , 'YYYYMMDD')...
And in this case when i try pass $DATE_TODAY into function TO_TIMESTAMP i get error:
AND o.creation_date > TO_TIMESTAMP( 20220610 , 'YYYYMMDD') error in line 30:
ORA-00932 inconsistent datatypes Expected -, NUMBER obtained
I need to pass date in single quotes like '20220610', Is it possible to do this?
You should put single quotes in the script, there is no need to escape them. Just do:
AND o.creation_date > TO_TIMESTAMP( '$DATE_TODAY' , 'YYYYMMDD')

Is there a way to use the REPLACE function in a TO_DATE function in SQL HANA

SELECT ADD_MONTHS (TO_DATE(REPLACE('6/23/17','/', '-'), 'YYYY-MM-DD'), 1) "add months" FROM DUMMY;
If I execute just the replace portion I get the date in the correct format with '-' instead of '/'
but when I try to use both functions together it fails!
There is no need to replace: just use the relevant format specifier for your date string:
select add_months(to_date('6/23/17', 'mm/dd/yy'), 1) new_date
from dummy

ansi SQL date function in Oracle

I realize I could use a combination of to_char and to_date and what not as a work-around, but I'm trying to figure out why this doesn't work. I am using Oracle 12.1
select '2016-10-01'
from dual
union all
select to_char(2016)||'-10-01'
from dual;
Each side of the union produces identical output: 2016-10-01. If I then try to use the ANSI date syntax (as described here: http://blog.tanelpoder.com/2012/12/29/a-tip-for-lazy-oracle-users-type-less-with-ansi-date-and-timestamp-sql-syntax/ ), it only works on the first one, not the second one:
select date '2016-10-01'
from dual
This returns: 10/1/2016
But if I try this:
select date to_char(2016)||'-10-01'
from dual;
I get on:
ORA_00936 missing expression error.
I can code a work-around, but I'm stumped as to why one works and the other does not.
It won't work that way because DATE is not a function but a literal.
The terms literal and constant value are synonymous and refer to a fixed data value.
Date Literals
You can specify a DATE value as a string literal, or you can convert a character or numeric value to a date value with the TO_DATE function. DATE literals are the only case in which Oracle Database accepts a TO_DATE expression in place of a string literal.
You could use TO_DATE function.
select TO_DATE(to_char(2016)||'-10-01', 'YYYY-MM-DD')
from dual;
Rextester Demo
EDIT:
Using dynamic-SQL:
DECLARE
i DATE;
stmt VARCHAR2(100);
BEGIN
stmt := q'{SELECT DATE '}' || TO_CHAR(2016) || '-01-01' || q'{' FROM dual}';
EXECUTE IMMEDIATE stmt INTO i;
DBMS_OUTPUT.PUT_LINE('i =>' || i);
END;

Oracle sql - convert string to date

i am having problems with converting a varchar(yyyymmdd) to date(yyyymmdd).
i have a procedure with a parameter (pdate varchar2, yyyymmdd format) which needed to be converted to date type in yyyymmdd format as well.
so far i tried.
vdate date;
vdate := (to_date(SUBSTR(pdate,1,4)+SUBSTR(pdate,5,2)+SUBSTR(pdate,7,2), 'yyyymmdd'));
this threw a error of ORA-01840: input value not long enough for date format.
any help would be appreciated.
Just use a to_date
select to_date(MyDate, 'yyyymmdd')
from mytable
Test with:
select to_date('20170831','yyyymmdd')
from dual
Also, to concatenate in Oracle, use a double pipe ||
select 'Chicken'||'Food'
from dual
If pdate has other characters after yyyymmdd, and yyyymmdd is in the beginning of the whole text, you can just use
SELECT TO_DATE(SUBSTR(pdate,1,8), 'yyyymmdd')
FROM yourtable;
Example
SELECT TO_DATE(SUBSTR('20170831 10:30am',1,8), 'yyyymmdd')
FROM dual;
Otherwise, you can directly use TO_DATE() as suggested by most that replied

how to convert last_day format to ddmmyyyy in DB2

i have problem to convert last_day data from yyyymmdd to ddmmyyyy.
my field are integer.so i convert into char. then i try to format into to_date. When i try to convert to ddmmyyyy.There are show some error message.
This is my formula:
LAST_DAY(to_date(cast(IVL_SCHEDULER_PROC_DATE as char(8)),'yyyymmdd'))
Result
2016-08-18
Expected Result
18-08-2016
Anyone know about this?
You can use varchar_format as below;
SELECT VARCHAR_FORMAT(TO_DATE("IVL_SCHEDULER_PROC_DATE",'YYYY-MM-DD'),'MM-DD-YYYY') from db2inst1.test
18-08-2016
this is last_day function with varchar_format;
SELECT VARCHAR_FORMAT(LAST_DAY(TO_DATE("IVL_SCHEDULER_PROC_DATE",'YYYY-MM-DD')),'MM-DD-YYYY') from db2inst1.test
31-08-2016
Would this work?
select
varchar(mod(IVL_SCHEDULER_PROC_DATE,100)) || '-' ||
varchar(mod(IVL_SCHEDULER_PROC_DATE/100,100)) || '-' ||
varchar(IVL_SCHEDULER_PROC_DATE/10000)
Using a test value of 20151231 I get a '31-12-2015' result.