So I'm having this problem , basicly I have a number 350 and I need to show it on 10 digits like this 350$$$$$$$ (I need to complete the number value with the character '$'). Thanks.
select rpad('350',10,'$') from dual
you can concatenate the values as $ is not number for example
select 350 ||'$' from dual
Related
One of the columns of my table has values that can typically range from 3500 to 8 million. Is it possible to specify a format that can divide the number when formatting?
For example, I have the following values:
3500
81000
1678500
Ideally I would like a format value (coming from another config table) that would format the numbers in "thousands" with 1 decimal place:
3.5
81.0
1678.5
But this format value could also be different for other cases, so they could be formatted in millions with two decimal places:
0.00
0.08
1.68
Is this possible, or do I need to divide the numbers myself before applying the formatting?
Is this possible, or do I need to divide the numbers myself before applying the formatting?
You need to do the division. You can use CASE WHEN, by the way, if you aren't having numeric values to track what to divide by:
SELECT tablevalue / CASE divby WHEN 'thousand' THEN 1000 WHEN 'million' THEN 1000000 ELSE 1 END
I presume you'll have some column in your "format" table that also specifies what to divide by..
So you don't want to add a column.. you can store the info in the existing column.. you just have to work more to get it out:
SELECT
TO_CHAR(
somenumber / CASE RIGHT(format, 1) WHEN 'k' THEN 1000 WHEN 'M' THEN 1000000 END,
LEFT(format, -1)
)
So now you can make your format like 99D99k and the k will cause a divide by 1000 and the result is formatted to 99.99, so if you have 1234, format 9.99k you'll get '1.23'out of it.
If you want the [k] at the start it's just some jiggling of the LEFT and RIGHT functions..
TO_CHAR(
somenumber / CASE LEFT(format, 3) WHEN '[k]' THEN 1000 WHEN '[M]' THEN 1000000 END,
RIGHT(format, -3)
)
I have one requirement in SQL where
We have couple of market ids
Let say 20,30,40
And here for each market using one plsql function we are getting one campaign number let's say for market 20 I got 20210306,for mrkt 30 we got 20210307, and for 40 mrkt id 20210308.
Now what I want to achieve for each market we should have current campaign no as 20210306 as well as 12 future campaign should be produced by SQL query based on each current campaign of the market.
I am doing it using union all and campaign+1,2,3 and so on which is taking time . Do we have some brief logic for it.
Please suggest
Thanks,
Vijay
You can generate series of data (numbers or dates or even characters) using the LEVEL pseudo column with a CONNECT BY clause.
In your case, it sounds like you want generate the next 10 numbers starting from 20210306.
That could be done like this:
WITH current_campaign ( nr ) AS
( SELECT 20210306 FROM dual )
SELECT
nr + level
FROM
current_campaign
CONNECT BY
level < 13;
20210307
20210308
20210309
20210310
20210311
20210312
20210313
20210314
20210315
20210316
20210317
20210318
The 20210306 could also represent the date March, 6, 2021. If you want to generate those numbers date based numbers you could do that too. For example (starting from 0629 so the result shows the month change, only showing 5 rows)
WITH current_campaign ( cdate ) AS
( SELECT DATE'2021-06-29' FROM dual ) SELECT
TO_CHAR(cdate + level,'YYYYDDMM')
FROM
current_campaign
CONNECT BY
level < 6;
20213006
20210107
20210207
20210307
20210407
Column amount contains data like - 200.203, 200, 5.10, 45.20, 10 and 5000213.012
Now I want to select if the number does not include any decimal digit then we add .00 in the returning result.
Expected result - 200.203, 200.00 ,5.10, 45.20 , 10.00 and 5000213.012
You can use TO_CHAR to format the value so that it has at least 2 decimal places:
SELECT value,
TO_CHAR( value, 'FM9999999990.009' ) AS formatted_value
FROM table_name
Which, for the sample data:
CREATE TABLE table_name ( value ) AS
SELECT 200.203 FROM DUAL UNION ALL
SELECT 200 FROM DUAL UNION ALL
SELECT 5.10 FROM DUAL UNION ALL
SELECT 45.20 FROM DUAL UNION ALL
SELECT 10 FROM DUAL UNION ALL
SELECT 5000213.012 FROM DUAL;
Outputs:
VALUE | FORMATTED_VALUE
----------: | :--------------
200.203 | 200.203
200 | 200.00
5.1 | 5.10
45.2 | 45.20
10 | 10.00
5000213.012 | 5000213.012
db<>fiddle here
Following some comments, the OP's question is effectively:
How can I change this number to have a given format?
When you consider that a NUMBER data type has no format and 5, 5.0 and 5.00000 are exactly the same value and the database does not store how many trailing zeroes a decimal value has then this question does not entirely make sense as there is no way to give a number a format.
Instead the question can be formulated as either:
How can I get <insert name of client program> to display numbers so that they always have at least 2 decimal places?
We can't answer this question without knowing the client program so I'll skip it.
Or:
How can I display a number from an Oracle query so that it is formatted with at least 2 decimal places?
Since NUMBERs never have any formatting of their own then this must involve a conversion to another data-type which can represent the number with a format (i.e. a string). So the result is that if you want a NUMBER data type then Oracle will not give it a format (but the client program might) but if you want to change the NUMBER to a string data type then you can give it a format (but it won't be a NUMBER as its now a string that just happens to contain digits) and, as described above, TO_CHAR does that.
This will do:
select to_number(to_char(value, '9999999.99')) from dual;
I have an age column which calculates age for each member in my report.The output is a whole number followed by a decimal point and numbers. I would like the first number only right after the decimal point .
I tried trunc but it gives me everything before the decimal and then the number I want after .Then I tried to trunc with a call out with a comma and it doesnt work.
trunc(age,',')
Example -
age 15.7
expected output 7
Here the mathematical answer
take the decimal part by susbriacting the whole part (trunc).
multiply by 10 and take the whole part
.
with age as (select 15.7231 age from dual)
select trunc(10*(age-trunc(age))) dp1 from age
DP1
----------
7
try like below
select substr(to_char(15.7,'9999.0'),-1,1) as col from dual
it will return 7
Multiply by 10, trunc it and take the remainder of the division by 10.
with age as (select 15.7231 age from dual)
select mod(trunc(10*age), 10) dp from age
Output:
DP
--
7
I need to find the next 10 digit number in a query.
I try to use round(n,-1) but it rounds off to nearest 10 digit but I need next 10 digit.
Please help me.
select round(5834.6,-1) from dual
gives 5830 but i need 5840
select ceil(5834.6/10)*10 from dual
Then add "5":
select round(5834.6 + 5,-1) from dual
A generic solution - Use MOD to find the last 10th place and then add 10 to the result.
select (5834.6 - MOD(5834.6,10)) + 10 from dual;
If you need the next 8th place, just replace any "10" with "8".
Select ceil(5834.6/10)*10 from dual;