This question already has answers here:
Truncate (not round) decimal places in SQL Server
(21 answers)
Closed 3 years ago.
The function TRUNCATE works in MySQL but I get an error in Microsoft SQL Server : Incorrect syntax near (
Basically I have list of value under discount. (Some with lots of decimal)
I just want to keep the 2 decimal without rounding.
E.g from 110.975 to 110.97, 10.259 to 10.25. How do I go about doing that?
SELECT TRUNCATE(110.975,2)
You could use round this way
ROUND (110.975 , 2 , 1 )
ROUND ( numeric_expression , length [ ,function ] )
function Is the type of operation to perform. function must be
tinyint, smallint, or int. When function is omitted or has a value of
0 (default), numeric_expression is rounded. When a value other than 0
is specified, numeric_expression is truncated.
https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2005/ms175003(v=sql.90)?redirectedfrom=MSDN
Related
This question already has answers here:
Check if a string contains only number
(5 answers)
Detect if value is number in MySQL
(15 answers)
Closed 5 months ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
I have a variable declared as nvarchar in the SP, its value is dynamic, it can be a string or a number, its value comes from the UI when user writes in the global search textbox to search a table. I need to cast its value from a string to a decimal only when the value is a number, however, I'm getting error when casting because the cast() returns error when casting a string like 'abc' but works fine if the value is '2000'.
How to check if the value is castable or not?
SELECT CAST('abc' AS DECIMAL(7,2) ) -- retruns error Error converting data type varchar to numeric.
SELECT CAST('2.0000' AS DECIMAL(7,2) ) -- retruns 2.00
You tagged this mysql, but MySQL does not raise an error when you try to cast a non-numeric string to a number. It returns 0 in that case (which I consider a design flaw in this DBMS).
The error message you are showing suggests another DBMS, most likely Microsoft SQL Server.
In SQL Server use TRY_CAST, which returns null if the string is not numeric:
SELECT *
FROM mytable
WHERE numcol = TRY_CAST(#searchstring AS DECIMAL(7,2));
The above query returns no rows if the string contains a non-numeric value. If you want to return all rows instead:
SELECT *
FROM mytable
WHERE numcol = TRY_CAST(#searchstring AS DECIMAL(7,2))
OR TRY_CAST(#searchstring AS DECIMAL(7,2)) IS NULL;
Docs: https://learn.microsoft.com/en-us/sql/t-sql/functions/try-cast-transact-sql?view=sql-server-ver16
This question already has answers here:
Integer division in sql server
(8 answers)
Closed last year.
I Exec below code in SQL Server Management Studio
SELECT CAST (9/100 AS DECIMAL(8,3))
this should return 0.090 but Return 0.000 .
Who can tell what the problem is and why it gives me this output?
The problem with your current approach is that the 9/100 ratio is computed using integer division before you make the cast to decimal. By the time that cast happens, it is already too late and you've lost the decimal component. Simply I would just do:
SELECT 9 / 100.0; -- 0.090000
So long as the numerator or denominator be a decimal, the quotient will also carry the decimal result.
This question already has answers here:
TSQL Round up decimal number
(4 answers)
Closed 2 years ago.
I need to round off a numeric value in SQL Server.
Consider this example - my value is 179.8744.
I want it to be converted to 179.88
I have tried ROUND function, but it only converts if 3rd value is greater than 5. Is there any other function?
Thanks in advance.
You could get creative with the ceiling() function. This function will always round up, but to the next integer. For 2 decimal places this can be resolved by multiplying and dividing by 100.0.
-- move decimal separator up
select 179.8744 * 100.0
-- round up to next integer
select ceiling(179.8744 * 100.0);
-- move decimal separator down (complete formula)
select ceiling(179.8744 * 100.0) / 100.0;
Fiddle
use decimal or round like below!
Examples decimal
select cast(179.8744 as decimal(10,2))
OR
select round(179.8744,2)
This question already has answers here:
Integer division in sql server
(8 answers)
Closed 7 years ago.
If I do
select 18000*13/100000
it returns 2
when it should return 2.34
How do I make it return the correct number?
thanks
select 18000*13/100000.0
Use a float either in numerator or denominator to get a float.
Use a decimal point to avoid integer division:
select 18000.0*13/100000
You can also CAST() as DECIMAL or FLOAT a field, but I like to just use mycol *1.0
This question already has answers here:
Truncate (not round) decimal places in SQL Server
(21 answers)
Closed 6 years ago.
Assume i have the following decimal value:
4.584406
I need a simple quick way to truncate the decimal without rounding up, so the output would be 4.5
I'm using T-SQL (SQL Server 2005/2008).
Any help will be appreciated.
using the round function you can try this
select round(4.584406, 1, 1)
the output will be
4.5
the key is the third parameter
ROUND ( numeric_expression , length [ ,function ] )
function
Is the type of operation to perform. function must be tinyint,
smallint, or int. When function is
omitted or has a value of 0 (default),
numeric_expression is rounded. When a
value other than 0 is specified,
numeric_expression is truncated.