convert oracle date string to number - oracle

I need to convert a date string -'2013-01-01' to number --20130101 type..How can I accomplish it in Oracle efficiently?
My Input-
'2013-01-01'
My Output
Output-20130101

select to_number(replace('2013-01-01', '-')) from dual;

You can convert the string to date and convert back using the format that you want to:
select to_char(to_date('2013-01-01', 'YYYY-MM-DD'), 'YYYYMMDD');
More information about datetime format:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm

to_char(your_date,'YYYYMMDD')
Just the basics of using to_char with a date here.

You can also "exploit" the format mask:
select to_number('2013-01-01', '9999G99G99', 'nls_numeric_characters=,-')
from dual
Here is a sqlfiddle demo

To convert date string to number:
Always convert it to char first then to number:
Select TO_NUMBER(TO_CHAR(to_date('2013-01-01', 'YYYY-MM-DD'),'YYYYMMDD')) from dual
if your column datatype is DATE then use your column name in place of sysdate in the below:
Select TO_NUMBER(TO_CHAR(sysdate,'YYYYMMDD')) from dual

Related

Formatting a decimal column into euro currency

I need to format a decimal value into a string with the format ###.###,##.
I've already tried:
SELECT to_char (11230.3423, '999,990.00') FROM DUAL
I'll get 11,230.34 where i want 11.230,34.
If i change the format as:
SELECT to_char (11230.3423, '999.990,00') FROM DUAL
I'll get an error.
Note: I need to format Euro(€) values so decimal separator is ','.
Thanks.
Use:
SELECT to_char (11230.3423, 'FM999G990D00', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL

Change a decimal string into a timestamp in Impala

How do you convert a string type like
t1.updte_timestamp
2018-06-02-08.18.45.562742
2018-05-26-09.18.16.594824
into a timestamp? SHOULD RESULT IN:
2018-06-02-08.18.45
2018-05-26-09.18.16
ETC
The values had been imported from excel and are in STRING-TYPE
I tried:
SELECT
to_timestamp(cast (t1.updte_timestamp as string), 'yyyy-mm-dd hh:mm:ss') as updted_timestamp FROM OLD;
but results in NULL for all values
thank you
you can substr your string and apply to_timestamp as follow
select to_timestamp(substr('2018-06-02-08.18.45.562742', 1, 19) , 'yyyy-MM-dd-HH.mm.ss');
Make sure you use MM for month and HH for hour in upper case

Converting a timestamp value to number

I've a timestamp value in the format 2005-01-31T00:00:00.000-05:00. I want covert it to a number in the format20050131, to equate it to a column which has the datatype of number.
I tried to do it by to_number(to_date(timestamp, 'yyyymmdd')). But it is resulting in error not a valid month.
Could you please help me in resolving it.
try using like below
to_number(to_char(timestamp, 'yyyymmdd'))
first you have to convert to char and then to number.
If your timestamp value is a VARCHAR2 value, maybe this will help:
WITH tstmp AS (
SELECT '2005-01-31T00:00:00.000-05:00' AS val FROM dual
)
SELECT TO_NUMBER(TO_CHAR(FROM_TZ(TO_TIMESTAMP(SUBSTR(val, 1, 23), 'YYYY-MM-DD"T"HH24:MI:SS.FF3'), SUBSTR(val, 24)) , 'YYYYMMDD')) AS newval
FROM tstmp;

Using Oracle to_date function for date string with milliseconds

I have to perform some inserts into an Oracle DB. I have some dates
in the following format
'23.12.2011 13:01:001'
Following the documentation I wrote inserts to_date as follows:
to_date('23.12.2011 13:01:01', 'DD.MM.YYYY HH24:MI:SS')
which works properly. Now I have dates with milliseconds with the format
'23.12.2011 13:01:001'
I've tried the following:
to_date('23.12.2011 13:01:001', 'DD.MM.YYYY HH24:MI:SSFF3')
which is incorrect (delivers an error 01821. 00000 - "date format not recognized").
Which "String" should I use for this format with milliseconds?
An Oracle DATE does not store times with more precision than a second. You cannot store millisecond precision data in a DATE column.
Your two options are to either truncate the string of the milliseconds before converting it into a DATE, i.e.
to_date( substr('23.12.2011 13:01:001', 1, 19), 'DD.MM.YYYY HH24:MI:SS' )
or to convert the string into a TIMESTAMP that does support millisecond precision
to_timestamp( '23.12.2011 13:01:001', 'DD.MM.YYYY HH24:MI:SSFF3' )
TO_DATE supports conversion to DATE datatype, which doesn't support milliseconds. If you want millisecond support in Oracle, you should look at TIMESTAMP datatype and TO_TIMESTAMP function.
Hope that helps.
For three digits millisecond:
TO_CHAR(LN_AUTOD_UWRG_DTTM,'MM/DD/YYYY HH24:MI:SS.FF3')
For six digits millisecond:
TO_CHAR(LN_AUTOD_UWRG_DTTM,'MM/DD/YYYY HH24:MI:SS.FF'),
You can try this format SS.FF for milliseconds:
to_timestamp(table_1.date_col,'DD-Mon-RR HH24:MI:SS.FF')
For more details:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm
You have to change date class to timestamp.
String s=df.format(c.getTime());
java.util.Date parsedUtilDate = df.parse(s);
java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedUtilDate.getTime());

How do I convert a daynumber (day nr 331) into yyyymmdd in PL/SQL?

If I know the number of the day is 331 within the year, how do I convert it into yyyymmdd in PL/SQL?
To convert to a DATE:
to_date(331, 'DDD')
You can then format that date if required using TO_CHAR.
DECLARE #datetime2 datetime2 = '2007-01-01 13:10:10.1111111'
SELECT 'day',DATEADD(day,331,#datetime2)
Another option could be something like this.
select to_char(trunc(sysdate, 'YY') + 331, 'YYYYMMDD') as day from dual

Resources