Tuesday, November 3, 2009

List of SQL Server Functions like Cast(), Round() Ceiling() Floor()

Sql server has many inbuild function like sum(), count(),round(), cast() .....
examples of some are

select ((30.250250/80)*100) as Percentage result: 37.812812500

select round(((30.250250/80)*100),1) result: 37.800000000
select round(((30.250250/80)*100),2) result: 37.810000000
select round(((30.250250/80)*100),3) result: 37.812000000
select round(((30.250250/80)*100),4) result: 37.812800000
select round(((30.250250/80)*100),5) result: 37.812810000

select ROUND(748.58, -1) result: 750.00
select ROUND(748.58, -2) result: 700.00
select ROUND(748.58, 0) result: 749.00
select ROUND(748.58, 1) result: 748.60
select ROUND(748.58, 2) result: 748.58


SELECT ROUND(123.4567, -1); result: 120.0000
SELECT ROUND(123.4567, -2); result: 100.0000
SELECT ROUND(123.4567, -3); result: 0.0000
SELECT ROUND(123.4567, -4); result: 0.0000
SELECT ROUND(123.4567, -5); result: 0.0000


select Ceiling(((30.250250/80)*100)) result: 38
select Floor(((30.250250/80)*100)) result: 37

select CAST(((30.250250/80)*100) AS DECIMAL (9,0)) result: 38
select CAST(((30.250250/80)*100) AS DECIMAL (9,1)) result: 37.8
select CAST(((30.250250/80)*100) AS DECIMAL (9,2)) result: 37.81
select CAST(((30.250250/80)*100) AS DECIMAL (9,3)) result: 37.8121
select CAST(((30.250250/80)*100) AS DECIMAL (9,4)) result: 37.8128
select CAST(((30.250250/80)*100) AS DECIMAL (9,5)) result: 37.81281

Note: In this case DECIMAL (9,0) : 9 represent no of digit before decimal point
and 0,1,2,3,4... represent digit after decimal point

No comments:

Post a Comment