Is there any function in Sql Server 2008 that controls scale numbers.For example
if we have a decimal number 123.456 and in function if pass 0 it should return 123, if i pass 1 then should return 123.4 if pass 2 then return 123.45 and if pass 3 then return 123.456.If there is no inbult function then Please let me know any user define function.Thanks, Ravi
ROUND() can truncate;
select round(123.456, 3, 1) union
select round(123.456, 2, 1) union
select round(123.456, 1, 1) union
select round(123.456, 0, 1)
>>123.456
>>123.450
>>123.400
>>123.000
If you don't want the trailing zeros remove them in the presentation layer, cast to a varchar or cast(round(123.456, 3, 1) as float)
CREATE FUNCTION f_test (
#a INT,
#b FLOAT
) RETURNS DECIMAL(12,6)
AS
BEGIN
RETURN CAST(#b * POWER(10, #a) AS INT)*1.0 / POWER(10, #a)
END
SELECT dbo.f_test(2, 123.456) AS RESULT
RESULT
----------
123.450000
Related
I am a beginner with regex query therefore I want to ask you how to convert this regex query in Oracle to SQL Server?
select *
from Sales
where regexp_like(productname,'^[A-Z]{3}[0-9]+$')
I convert it to this query:
select *
from Sales
where substr(productname, 1, 3) in ([A-Z])
Is that correct?
Thankyou.
You can use the following query:
SELECT *
FROM Sales
WHERE LEFT(productname, 3) LIKE '[A-Z][A-Z][A-Z]' -- three chars ([A-Z]{3})
AND NOT RIGHT(productname, LEN(productname) - 3) LIKE '%[^0-9]%' -- only numbers after the first three chars
AND NOT LEN(RIGHT(productname, LEN(productname) - 3)) = 0 -- at least one number
demo on dbfiddle.uk
Based on regexp '^[A-Z]{3}[0-9]+$':
-- exactly 3 letters
-- at least one trailing digits
You could use TRIM:
SELECT *
FROM Sales
WHERE productname LIKE '[A-Z][A-Z][A-Z][0-9]%'
AND TRIM( '0123456789' FROM productname) LIKE '[A-Z][A-Z][A-Z]';
-- warning! expression is not SARGable
db<>fiddle demo
Think the most simple would be to use
WHERE
SUBSTRING(<column>, 1, 1) IN('A', 'B', 'C')
AND
SUBSTRING(<column>, 2, 1) IN('A', 'B', 'C')
AND
SUBSTRING(<column>, 2, 1) IN('A', 'B', 'C')
AND
TRY_CONVERT(
INT
, SUBSTRING(<column>, 4, LEN(<column>))
) <> 0
as you don't really need regex to do this.
see demo
I want to convert numeric value to Money but without Rounding value.W.r.t. to
Link : https://technet.microsoft.com/en-us/library/ms187928(v=sql.105).aspx It is rounding numeric to Money while casting.
But is it possible to give value upto 4 digit after decimal.
NUMERIC VALUE : 123456789.3333
MONEY VALUE OUTPUT required : 123,456,789.3333
May be you are looking for something like this
SELECT FORMAT(CONVERT(MONEY, CAST(123456789.3333 AS NUMERIC(18,4))), '###,###.####')
Result
123,456,789.3333
I guess you mean numerics where you have more than 4 digits, then you could use ROUND:
SELECT CAST(ROUND(123456789.33339, 4, 1) AS MONEY)
-- 123456789,3333
vs.
SELECT CAST(123456789.33339 AS MONEY)
-- 123456789,3334
Rextester Demo
if you wanna split number as 3 digit , you can use this code in your select command
Select LEFT(CONVERT(VARCHAR, CAST(YourPrice AS MONEY), 1), LEN(CONVERT(VARCHAR, CAST(UnitPrice AS MONEY), 1)) - 3 )as UnitPrice
Using Ruby you can produce an array with the following code:
some_range = (1..10).to_a
# Returns => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I'm looking for something similar in SQL Server using a SELECT statement. i.e. I would like to produce this table:
id
1
2
3
4
5
6
7
8
9
10
etc
With a statement that looks something like this:
DECLARE #ids TVP
INSERT #ids
(id)
SELECT RANGE(0..10000)
My use case is that I want a quick way of testing SPs that takes #ids as a TVP. Does SQL Server provide something that achieves this?
I usually use a recursive CTE like this if I want to generate a numbers table:
;WITH NumberGen AS
(
SELECT 1 AS Number
UNION ALL
SELECT Number + 1 AS Number
FROM NumberGen
WHERE Number < $#RowsToBeGenerated$
)
INSERT INTO $Table$($Field$)
(
SELECT Number
FROM NumberGen
WHERE Number BETWEEN 1 AND $#RowsToBeGenerated$
)
OPTION (MAXRECURSION 0);
DECLARE #Range INT = 10
;with cte
as
(
select 1 as value
union all
select value + 1 from cte where value < #Range
)
select * from cte
In a SQL Server stored procedure I am casting a variable as decimal:
CAST( #ToValue as decimal(38,5) )
If the result is say 57282.0000 (decimal value is 0 ) I want it to appear as 57282.
Else I want to appear as 57282.48300 for example.
I cannot use float because if the value is greater than an 8 digit number, its displayed as exponential format.
How can I solve this?
You should do this in the formatting options for your reporting software.
If for some reason that isn't possible then the following might work.
;WITH T(Val) AS
(
SELECT 57282 UNION ALL
SELECT 57282.48300
)
SELECT CASE WHEN 1= 0 THEN NULL
WHEN Val = FLOOR(Val) THEN CAST(CAST(Val AS INT)AS SQL_VARIANT)
ELSE CAST(Val AS DECIMAL(38,5))
END
FROM T
I have this number 0.581183781439, I need to round the number 0.582
I was trying with
SELECT ROUND (0.581183781439, 3)
-------------------------------
0.581000000000
Is it useful ROUND function for this?
DECLARE #Test TABLE (Col NUMERIC(38,12));
INSERT #Test (Col)
SELECT 0.581183781439
UNION ALL
SELECT 0.5815
UNION ALL
SELECT 0.581883781439
UNION ALL
SELECT -0.581883781439;
SELECT Col AS [Col],
ROUND(Col, 3) AS StandardRounding_3decimals,
ROUND(Col, 3, 1) AS Truncation_3decimals,
FLOOR(Col*1000)/1000 AS RoundDown_3decimals,
CEILING(Col*1000)/1000 AS RoundUp_3decimals
FROM #Test;
Results:
Col StandardRounding_3decimals Truncation_3decimals RoundDown_3decimals RoundUp_3decimals
--------------- -------------------------- -------------------- ------------------- -----------------
0.581183781439 0.581000000000 0.581000000000 0.581000 0.582000
0.581500000000 0.582000000000 0.581000000000 0.581000 0.582000
0.581883781439 0.582000000000 0.581000000000 0.581000 0.582000
-0.581883781439 -0.582000000000 -0.581000000000 -0.582000 -0.581000
Maybe you need this?
SELECT ROUND(0.581183781439, 3,1) + .001
but correct rounding is 0.581.
We can write a T-SQL function to round up for an arbitrary number of decimal places.
CREATE FUNCTION RoundUp (#value float, #places int) RETURNS float
AS
BEGIN
RETURN SELECT CEILING(#value * POWER(10, #places)) / POWER(10, #places)
END
GO
SELECT Result = dbo.RoundUp(0.581183781439, 3)
Result
0.582
Adding a half value and using normal round will get the correct result.
SELECT ROUND (0.581183781439 + .0005, 3)
Bonus: Subtract the half value instead, and it will round down.
EDIT: Actually, this has the same flaw as an answer above when the value is zeros after the 3rd decimal.