Oracle sql - convert string to date - sql

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

Related

DATE FORMAT :DB2 _ SQL

I have date format (TIMESTAMP) like 2018-03-26-08.30.00.000000 and i want to get it as 2018-03-26-08 how i can do it in sql in DB2(just i want year-month-day-hour )'
Read DB2's manual:
https://www.ibm.com/docs/en/db2-for-zos/12?topic=sf-char
Using that docu:
I need to convert the non-standard in-format with a dash between day and hour, and dots as hour/minute and minute/second separators to a timestamp, using TO_TIMESTAMP().
From the obtained timestamp, I can use TO_CHAR() with a format string to give me back a string with the format I desire.
WITH
indata(ts) AS (
SELECT
TO_TIMESTAMP(
'2018-03-26-08.30.00.000000','YYYY-MM-DD-HH24.MI.SS.US'
)
FROM sysibm.sysdummy1
)
SELECT TO_CHAR(ts,'YYYY-MM-DD-HH24') AS new_format FROM indata;
new_format
---------------
2018-03-26-08
VARCHAR_FORMAT(<timestamp>, '<desired format>')
So:
SELECT VARCHAR_FORMAT(CURRENT TIMESTAMP, 'YYYY-MM-DD-hh24')
FROM SYSIBM.SYSDUMMY1

How to compare date to format date on oracle

How can we compare a date to a format in Oracle?
Something like this: if MyDate is on format DD MONTH YYYY THEN /....
elsif MyDate is on format YYYY-MONTH-DD Then...
EDIT: My dates are in varchar2 and i want to keep them that way. I want just to know how to write a regex that would reprensent for example 10 October 2010.
Is it possible ? If it is a regex how would its format be please
Echoing what was mentioned in the comments to your question, best practice would be to have an actual DATE type field instead of VARCHAR2, and if you needed specific display formats, store those in another field as a format pattern. That said, you can use REGEXP_LIKE to check the format using the patterns in the below example.
with dateinfo as (
select 1 as id, '2018-MARCH-10' as dtString from dual
union all
select 2 as id, '10 MARCH 2018' as dtString from dual )
select id, dtString,
case
when regexp_like(dtString, '^[0-9]{4}-.[a-zA-Z]{3,}-.[0-9]{1,2}$') then 'format1'
when regexp_like(dtString, '^[0-9]{1,2} [a-zA-Z]{3,} [0-9]{4}$') then 'format2'
else 'no format'
end as dtFormat
from dateinfo;

Covert NVARCHAR to number (digits after decimal sign vary)

Do you know how I can convert NVARCHAR in Oracle if the digits after decimal signs vary?
I need this in order to convert it to date, using this formula:
select to_date('12/30/1899', 'MM/DD/YYYY') + 40676.2641666667
from dual;
The thing is that the digits in the second part (40676.2641666667) vary so I can use to_number(t.column, '99999D9999999999'), because it doesn't work this way.
Do you have any suggestions?
These all work for me:
select (date '1899-12-30') + '40676.2641666667'
from dual
and:
select (date '1899-12-30') + cast('40676.2641666667' as number)
from dual
and:
select (date '1899-12-30') + 40676.2641666667
from dual
One caveat is that internationalization settings might require comma instead of period.
Oracle doesn't have NVARCHAR, it has NVARCHAR2. The following works for me:
DECLARE
nv_Number NVARCHAR2(0032) := 40676.2641666667;
dt_Base DATE := TO_DATE('12/30/1899', 'MM/DD/YYYY');
dt_Result DATE;
BEGIN
dt_Result := dt_Base + TO_NUMBER(nv_Number);
DBMS_OUTPUT.PUT_LINE(TO_CHAR(dt_Result,'yyyymmdd hh24:mi:ss'));
END;
Result: 20110513 06:20:24

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;

PL/SQL - to_char within DECODE

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' )