Formatting a decimal column into euro currency - oracle

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

Related

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

Convert String to Date field for SQL Oracle

I need to convert a string to a date field. The field stores 30 characters. Dates, when present, are formatted as 'yyyymmdd' (20170202). In all cases, dates have 22 spaces after. I need to format this field as a date field like this: dd-mm-yyyy.
I've tried several formulas:
TO_CHAR(PERSACTION.NEW_VALUE_02, 'dd-mm-yyyy') ,TO_CHAR(PERSACTION.NEW_VALUE_02, 'yyyymmdd'), trim(TO_CHAR(PERSACTION.NEW_VALUE_02, 'yyyymmdd')) with error message: invalid number format model. Your expertise is welcome and appreciated.
to_char(to_date( rtrim(new_value_02), 'yyyymmdd'), 'dd-mm-yyyy')
Should do the trick. rtrim removes spaces on right side of string. Then I convert it to date using the date format specified, and then convert it to a string again in the desired format.
Did tried to convert to date format and then to char again?
TO_CHAR(TO_DATE(PERSACTION.NEW_VALUE_02,'yyyymmdd'),'dd-mm-yyyy')
Please, please, please do not store DATEs and CHARACTER datatypes. This will only lead to issues that can be avoided when using the DATE datatype.
If you want to change the string 20170202 to another string and not actually a date (which would have no intrinsic formatted text representation), you could optionally use a regular expression to transform it, instead of converting to a date and back:
select regexp_replace('20170202 ', '^(\d{4})(\d{2})(\d{2}) +$', '\3-\2-\1')
from dual;
REGEXP_REPLACE(
---------------
02-02-2017
Or you could use substr instead of regexp_substr, which may perform better even if you have to call it three times; using a CTE just to avoid repeating the value:
with t(str) as (
select '20170202 ' from dual
)
select substr(str, 7, 2) ||'-'|| substr(str, 5, 2) ||'-'|| substr(str, 1, 4)
from t;
SUBSTR(STR
----------
02-02-2017
If you do convert to a date and back you would uncover any values which cannot be converted, as they will cause an exception to be thrown. That would imply you have bad data; which would have been avoided by using the right data type in the first place, of course. These will convert any old rubbish, with varying results depending on how far the strings stray from the pattern you expect - but including strings like '20170231' which represent an invalid date. And null value or strings of just spaces will be converted to odd things with the substr version, but you could filter those out.
You can see the kind of variation you would get with some sample data that doesn't match your expectations:
with t(str) as (
select '20170202 ' from dual
union all select '20170231 ' from dual
union all select '2017020c ' from dual
union all select '2017020 ' from dual
union all select '201702021 ' from dual
union all select ' ' from dual
union all select null from dual
)
select str,
regexp_replace(str, '^(\d{4})(\d{2})(\d{2}) +$', '\3-\2-\1') as reg,
substr(str, 7, 2) ||'-'|| substr(str, 5, 2) ||'-'|| substr(str, 1, 4) as sub
from t;
STR REG SUB
------------- ------------- -------------
20170202 02-02-2017 02-02-2017
20170231 31-02-2017 31-02-2017
2017020c 2017020c 0c-02-2017
2017020 2017020 0 -02-2017
201702021 201702021 02-02-2017
- -
--
With the anchors and whitespace expectation, the regular expression doesn't modify anything that doesn't consist entirely of 8 numeric characters. But it can still form invalid 'dates'.

Cannot SUM(TO_NUMBER(varchar2 field)) :ORA 01722 [ORACLE]

I have myfield as varchar2 type and I try to sum this field by using sum(to_number(myfield)) but the result is ORA-01722 invalid number.
before this error occured I used SUM(TO_NUMBER(REGEXP_REPLACE(BIKOU,'[[:alpha:]]', ''))) and it works but last week I put some decimal value in myfield so this code not work anymore.
Here is my example of data in myfield
10,12,13.5,NULL
If you're getting that error from a string like 13.5 then your session's NLS_NUMERIC_CHARACTERS seems to be set to use a comma as the decimal separator:
alter session set nls_numeric_characters=',.';
with your_table (bikou) as (
select '10' from dual
union all select '12' from dual
union all select '13.5' from dual
union all select null from dual
)
select SUM(TO_NUMBER(REGEXP_REPLACE(BIKOU,'[[:alpha:]]', '')))
from your_table;
SQL Error: ORA-01722: invalid number
You can either explicitly set the session to use a period as the decimal separator, or provide a format mask that uses a period:
select SUM(TO_NUMBER(REGEXP_REPLACE(BIKOU,'[[:alpha:]]', ''), '99999999.99999'))
from your_table;
SUM(TO_NUMBER(REGEXP_REPLACE(BIKOU,'[[:
---------------------------------------
35,5
Or use the decimal separator marker in the model and override the session's NLS setting:
select SUM(TO_NUMBER(REGEXP_REPLACE(BIKOU,'[[:alpha:]]', ''),
'99999999D99999', 'nls_numeric_characters=''.,'''))
from your_table;
SUM(TO_NUMBER(REGEXP_REPLACE(BIKOU,'[[:
---------------------------------------
35,5
The mask obviously has to be suitable for all the values you expect back from your regex; what I've used may not be quite right for your data.
This kind of issue is why you should not store numbers or dates as strings. Use the correct data type for your columns.

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;

convert oracle date string to number

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

Resources