xsd format number , 5 decimal places - oracle

I have xsd code which is printing LINE_AMOUNT value as 14,952.59 , now i want to display this as 14,952.59000(5 decimal places).
How to achieve this?
Thank you

SQL Fiddle
Query 1 Either (if you want to use the current NLS values for decimal and thousands characters):
SELECT TO_CHAR(
14952.59,
'FM9G999G999G999G990D00000'
)
FROM DUAL
Results:
| TO_CHAR(14952.59,'FM9G999G999G999G990D00000') |
|-----------------------------------------------|
| 14,952.59000 |
Query 2 or:
SELECT TO_CHAR(
14952.59,
'FM9,999,999,999,990.00000'
)
FROM DUAL
Results:
| TO_CHAR(14952.59,'FM9,999,999,999,990.00000') |
|-----------------------------------------------|
| 14,952.59000 |
Update: SQL Fiddle
Query 3:
SELECT TO_CHAR(
TO_NUMBER(
'13,214,952.59',
'FM9G999G999G999G990D99999'
),
'FM9G999G999G999G990D00000'
) AS formatted_value
FROM DUAL
Results:
| FORMATTED_VALUE |
|------------------|
| 13,214,952.59000 |
Query 4:
SELECT TO_CHAR(
TO_NUMBER(
'13,214,952.59',
'FM9,999,999,999,990.99999'
),
'FM9,999,999,999,990.00000'
) AS formatted_value
FROM DUAL
Results:
| FORMATTED_VALUE |
|------------------|
| 13,214,952.59000 |

Try this, it will work for you I think.
select trim(to_char(14952.59,9999999999.99999)) from dual
OUTPUT
14952.59000

Related

using oracle sql, how to convert each digit to sum 9

I need help to convert a mobilenumber as a way of masking, i need to convert each digit to 9. Please see my sample below:
sample:
123456789
111111111
77771111
result:
876543210
888888888
22228888
notice that if you add each digit for (sample and result) the result is 9.
Use TRANSLATE:
SELECT value,
TRANSLATE( value, '1234567890', '8765432109' ) AS substitution
FROM table_name
Which, for your sample data:
CREATE TABLE table_name ( value ) AS
SELECT '123456789' FROM DUAL UNION ALL
SELECT '111111111' FROM DUAL UNION ALL
SELECT '77771111' FROM DUAL;
Gives:
VALUE | SUBSTITUTION
:-------- | :-----------
123456789 | 876543210
111111111 | 888888888
77771111 | 22228888
DO NOT use a substitution cipher to try to anonymise data.
However, this is not masking the data as it is (very) easy to get the original data back; you just put the substituted value back through the same translation and you have "unmasked" the data. So, if you are relying on this process to anonymise personal information then you have not achieved that goal as it is easy to reconstruct the original and de-anonymise the data.
SELECT value,
TRANSLATE( value, '1234567890', '8765432109' ) AS substitution,
TRANSLATE(
TRANSLATE( value, '1234567890', '8765432109' ),
'1234567890',
'8765432109'
) AS reversed_substitution
FROM table_name
Outputs:
VALUE | SUBSTITUTION | REVERSED_SUBSTITUTION
:-------- | :----------- | :--------------------
123456789 | 876543210 | 123456789
111111111 | 888888888 | 111111111
77771111 | 22228888 | 77771111
db<>fiddle here

Group by TIMESTAMP`S Date in Oracle

I am trying to group by Timestamp`s Date in oracle so far I used to_char. But I need another way. I tried like below:
SELECT d.summa,
d.FILIAL_CODE,
to_char(d.DATE_ACTION, 'YYYY-MM-DD')
FROM table1 d
WHERE d.action_id = 2
AND d.date_action Between to_date('01.01.2020', 'dd.mm.yyyy') AND to_date('01.03.2020', 'dd.mm.yyyy')
GROUP BY to_char(d.DATE_ACTION, 'YYYY-MM-DD')
table1
-----------------------------------------------------
summa | filial_code | date_action
--------------------------------------------------
100000.00 | 2100 | 2016-09-13 11:04:32
320000.12 | 3200 | 2016-09-12 21:04:58
400000.00 | 2100 | 2016-09-13 15:12:45
510000.12 | 3200 | 2016-09-15 09:30:58
------------------------------------------------------
I need like below
-------------------------------------------
summa | filial_code | date_action
------------------------------------------
500000.00 | 2100 | 2016-09-13
320000.12 | 3200 | 2016-09-12
510000.12 | 3200 | 2016-09-15
------------------------------------------
But I need except to_char function. I tried trunc but i could not do that
Using TRUNC should actually convert it to a date and remove the time part, but you also need to handle your other columns. Either group by them or use an aggregation function:
SELECT SUM( d.summa ) AS summa,
d.FILIAL_CODE,
TRUNC(d.DATE_ACTION) AS date_action
FROM table1 d
WHERE d.action_id = 2
AND d.date_action Between to_date('01.01.2020', 'dd.mm.yyyy')
AND to_date('01.03.2020', 'dd.mm.yyyy')
GROUP BY TRUNC(d.DATE_ACTION), d.FILIAL_CODE

Insert into a new table with values from session state and another table

I want to insert from a procdure into a new table like the below
The unit_id is from a table called lms_units and it has a FK to lms_qualifications called qualification_id
The table will have 1 Trainer with 1 qualification and all the units that go with it
insert into LMS_TRAINER_QUALI_UNITS
(
lms_trainer_quali_units_id,
Trainer_id,
Qualification_id,
unit_id
)
Values
(
lms_trainer_quali_units_id_seq.nextval,
:P2_Trainer_id,
:P6_qualification_id,
Unit_id --- this will have many values
)
Oracle Setup:
CREATE TABLE lms_qualifications (
Qualification_id, Title, Code
) AS
SELECT 33450, 'Lifter', 123 FROM DUAL;
CREATE TABLE LMS_UNITS (
Unit_id, Qualification_id, description
) AS
SELECT 69052, 33450, 'Elective' FROM DUAL UNION ALL
SELECT 69053, 33450, 'core' FROM DUAL UNION ALL
SELECT 69054, 33450, 'core' FROM DUAL UNION ALL
SELECT 69055, 33450, 'Elective' FROM DUAL UNION ALL
SELECT 69056, 33450, 'Elective' FROM DUAL;
Expected on LMS_TRAINER_QUALI_UNITS
+----------------------------+------------+------------------+---------+
| lms_trainer_quali_units_id | Trainer_id | Qualification_id | unit_id |
+----------------------------+------------+------------------+---------+
| 50001 | 4500 | 33450 | 69052 |
| 50002 | 4500 | 33450 | 69053 |
| 50003 | 4500 | 33450 | 69054 |
| 50004 | 4500 | 33450 | 69055 |
| 50005 | 4500 | 33450 | 69056 |
+----------------------------+------------+------------------+---------+
You can use INSERT INTO ... SELECT to copy values from one table to another:
insert into LMS_TRAINER_QUALI_UNITS (
lms_trainer_quali_units_id,
Trainer_id,
Qualification_id,
unit_id
)
SELECT lms_trainer_quali_units_id_seq.nextval,
:P2_Trainer_id,
Qualification_id,
Unit_id
FROM LMS_UNITS
WHERE Qualification_id = :P6_qualification_id

How I can distribute values in Oracle

Regards.
Can you help me with the following?
I have the following records:
**ITEM VALUES**
A001 29440
A002 29440
A003 29440
A004 29440
A005 29440
Σ of the field values is equal to 147200, but the real value is 148 200.
How I can distribute each value in the VALUES column for the Σ is exactly equal to 148 200.
Thank you very much.
Without a better guide as to how you got the "real" value and how the difference should be distributed, you could do something trivial to fix the data - such as adding an equal share of the difference to each value.
Like this:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE TEST ( ITEM, VALUE ) AS
SELECT 'A001', 29440 FROM DUAL
UNION ALL SELECT 'A002', 29440 FROM DUAL
UNION ALL SELECT 'A003', 29440 FROM DUAL
UNION ALL SELECT 'A004', 29440 FROM DUAL
UNION ALL SELECT 'A005', 29440 FROM DUAL;
UPDATE TEST
SET VALUE = VALUE + ( SELECT (148200 - SUM( VALUE ))/COUNT(1) FROM TEST );
Query 1:
SELECT * FROM TEST
Results:
| ITEM | VALUE |
|------|-------|
| A001 | 29640 |
| A002 | 29640 |
| A003 | 29640 |
| A004 | 29640 |
| A005 | 29640 |

add column check for format number to number oracle

I need to add a column to a table that check for input to be a max value of 999 to 999, like a soccer match score. How do I write this statement?
example:
| Score |
---------
| 1-2 |
| 10-1 |
|999-999|
| 99-99 |
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE SCORES (Score ) AS
SELECT '1-2' FROM DUAL
UNION ALL SELECT '10-1' FROM DUAL
UNION ALL SELECT '999-999' FROM DUAL
UNION ALL SELECT '99-99' FROM DUAL
UNION ALL SELECT '1000-1000' FROM DUAL;
Query 1:
SELECT SCORE,
CASE WHEN REGEXP_LIKE( SCORE, '^\d{1,3}-\d{1,3}$' )
THEN 'Valid'
ELSE 'Invalid'
END AS Validity
FROM SCORES
Results:
| SCORE | VALIDITY |
|-----------|----------|
| 1-2 | Valid |
| 10-1 | Valid |
| 999-999 | Valid |
| 99-99 | Valid |
| 1000-1000 | Invalid |

Resources