I need to set the value of a field in table depending on conditions whose value I am getting from another table. The data type of this field is float. The requirement is I need to have 2 digits before decimal and 4 after. So if a value is 9.45, I need to set it 09.4500. I am wondering what might be the best way to do this. Should I convert the value into varchar and then do substring? Or can this be done setting precision?
Thanks!
First, don't store such a value as a float. You have described the decimal(6, 4)/numeric(6, 4) data type very closely.
If you have this as a float or decimal, you can use str() and then pad to the right:
select right('000000' + str(9.45, 6, 4), 7)
Related
I know REAL data type is not accurate and normally for currency I should use numeric data type.
But, I'm asked to do some stuff and one of the conditions is that the data type is real.
When I try to do round((....),2) for example, I get that round function does not exist for this data type.
My question is, without converting, is there any function that can return a REAL value rounded to 0?
Many thanks!1
As you can see here it's no way to round without any type cast. It's only two kinds of function exists:
round(dp or numeric) - round to nearest integer
round(v numeric, s int) - round to s decimal places
Real = double precision. So you need to use convert anyway if you want to get some decimal places:
select round('123.456789'::real::numeric,2)
upd. Keep care about rounding+cast at big real numbers:
select round('12122156.567'::real::numeric, 2); --< rounding up to 6 digits, result = 12122200
select round('12122156.567'::real::DOUBLE PRECISION::numeric,2); --<< rounding result = 12122157
Or you can use round without decimal places:
select round('123.456789'::real)
round a numeric value to 0 after the dot?
ROUND(numeric_value, 0)
After investigation, converting to ::numeric is the only way around
I am retrieving a numeric value from one of the column in Sql. However I have to retrieve it by three decimal point (eg below).
Though if the value in the table is in the decimal point, then its giving the result as expected. But in case the value is not in decimal format and only an integer it must display .000
Below is the example.
Value in table= 2
Retrieval Value = 2
Required Value = 2.000
As per your requirement convert your incoming value in Decimal(18,3). like given below,
select convert(decimal(18,3),2)
To really show a value in a particular format, you can convert the value to a string. One method uses the str() function:
str(x, 18, 3)
Note that you can also cast() the decimal:
select cast(cast(x as decimal(18, 3)) as varchar(19))
I am trying to round a value in SQL, here is the code that I have:
select round(600.000,2)
How do I get the value 600.00?
Instead of round() convert to a decimal:
select cast(600.000 + 0.5 as decimal(10, 2) )
round() changes the value but it might not change the type of the result. Hence, you might still see extra decimal points (depending on the database and the application). Converting to a decimal with two digits of precision converts both the value and the type.
I have a field cost with values 0.987878656435798654 , 0.765656787898767
I am trying to figure out what would be the datatype for this.
When I give decimal 15,15 and trying to load data it is throwing me an error
Arithmetic overflow error converting varchar to data type numeric.
The problem is that you are not allocating any length to the value before the decimal.
DECIMAL (15, 15) means that it has a precision of 15 digits after the decimal, but only enough room for 15 digits total - thus leaving no room for values greater than 1.
This means that DECIMAL (15, 15) only supports values in the following range:
-0.999999999999999 to 0.999999999999999 (15 digits after the decimal).
You have 18 digits in your first example, so I would recommend using something like DECIMAL (21, 18)
DECIMAL (21, 18) will support values in the range from: -999.999999999999999999 to 999.999999999999999999 (18 digits after the decimal).
But, you should analyze your own data to see what the maximum value would be that you need to support.
Try this...
SELECT LEN(YourColumn)
FROM YourTable
Then , if they are below 1 every time, try this...
SELECT CONVERT(DECIMAL(X,X-1),YourColumn)
Where X is what is returned in the LEN statement. and X-1 is one less than that.
Remember, it's DECIMAL(TotalLength,Precision) so you need to make sure have enough space for the total value.
I have a column in my table which showing an amount. The amount is varying from one column to another and they are more than 15 digits.
What is the best way to format the number to show commas and decimal points?
My query is
select amount from ccamounts
How can I format the number
205511892078
to show as
205,511,892,078
and if there is a radix point it will also appear.
I believe you can use TO_CHAR to do this, the issue is that this is just a formatting function within SQL. It requires that your number is always going to be in the same format.
taking the example above you could do
TO_CHAR('205511892078', '999,999,999,999')
and this would format the number as you have specified, with a decimal place this can be done aswell but the decimal needs to be specified:
TO_CHAR('20551189207842', '999,999,999,999.99')
which would give you 205,511,892,078.42
I think if the field length is going to vary sql will just ignore anything that doesn't fit into the format string (It's a mask). Perhaps you want to consider formatting the number in this case on whichever front end you may be using?
I would format the number in the UI / Reporting tool / Presentation layer not Oracle
but if you MUST format it in oracle try:
SELECT
CASE WHEN INSTR( TO_CHAR(205511892078),'.')>0 THEN
TO_CHAR(205511892078 ,'999,999,999,999.99')
ELSE
TO_CHAR(205511892078 ,'999,999,999,999')
END
FROM DUAL
this will return the number as a string.
declare #d3 decimal (10, 2)
set #d3 = 12309809.5494
SELECT convert(varchar(15),cast(CAST(ROUND(#d3,2,1) AS DECIMAL (30,2)) as money),1) as Value
SELECT CAST(ROUND(convert(varchar(30), cast(#d3 as money),2),2,1) AS DECIMAL (30,2)) as Value
Output:
12,309,809.55
12309809.55