Monday, 12 May 2014

Round values to N number of Decimal points in sql server

Round (expression , length)

SELECT ROUND(3.1415 , 2)        -- 3.14

SELECT ROUND(33.234234234 , 1)  -- 33.2

SELECT ROUND(55.6345345 , 0)    -- 56


Monday, 31 March 2014

Find Length of string with LEN function in sql server

It returns the number of characters of string expression but with excluding trailing blanks.

SELECT LEN('Sql Server Database')

-- Result : 19


Tuesday, 25 March 2014

Find Age from Date of Birth in Sql Server


declare @dob datetime = '1952-08-14 21:11:19.300'

select CAST( DATEDIFF(Y , @dob , getdate() )/365.25 as int)

-- 365 are the Number of Average days in 4 years


Sunday, 23 March 2014

Select Top N rows from a table in Sql Server

Suppose if N=10 then ,

Select Top 10 * from Person

IF you want to Select only particular columns then,

Select Top 10 Name , City , Age from Person