Sql Solutions
  • Home
  • Sql

Saturday, 23 July 2016

Calculate Age in SQL Server

 Unknown     00:06     No comments   


Please Pass your date of birth in "DD/MM/YYYY" format and get age .....


Create a Procedure...


create Proc dbo.CalculateAge_ravi  
(@date1 varchar(15))
as
DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days int
SELECT @date = CONVERT(datetime,@date1,103)

SELECT @tmpdate = @date

SELECT @years = DATEDIFF(yy, @tmpdate, GETDATE()) - CASE WHEN (MONTH(@date) > MONTH(GETDATE())) OR (MONTH(@date) = MONTH(GETDATE()) AND DAY(@date) > DAY(GETDATE())) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(yy, @years, @tmpdate)
SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) - CASE WHEN DAY(@date) > DAY(GETDATE()) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(m, @months, @tmpdate)
SELECT @days = DATEDIFF(d, @tmpdate, GETDATE())

SELECT @years as Years, @months as Months, @days as Days





Get Your Age

 exec CalculateAge_ravi  'Pass your DOB'
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

About Me

Unknown
View my complete profile

Popular Posts

  • How to use cursor in SQL Server
    Please Use  this in your procedures and functions    DECLARE  C21 CURSOR FOR  <Query to find Value on basis of ...>    OPEN C21  ...
  • Reduce log file size in sql server
    Dear all these are the query to reduce  size of log file in sql  Query 1 ALTER DATABASE databasename SET RECOVERY SIMP...
  • Shrink Database File
    By Using This query we can reduce database file Firstly we should get File Name by Using This with TblSpace as (     select       ...
  • Remove Deadloack Condition in sql server
    Use this query to drop all kind of Transaction Deadlock in Sql Server ALTER DATABASE <DataBase Name> SET SINGLE_USER WITH ROLLBACK ...
  • Get Script of Stored Procedures and views
    Please Write this qurey and get
  • Right Use of Transaction Using TRY catch
    This is right use of SQL transaction Using TRY Catch for  Avoid DeadLock Condition BEGIN TRANSACTION TransRavi BEGIN TRY <Do Y...
  • Get Complete Dependency of A table in databse
    Please pass your table name in this query and  Get name of all object which are dependent on that SELECT distinct A.name FROM syscomme...
  • Replace Null values in sql server
    This Function is used in replacing null or blank value as 0 select coalesce(@param, 0) this gives result 0 when @param is null or blank...
  • Find text in whole database procedures
    For Finding particular text in stored procedures  please put your text on the place of <Text> and get result... SELECT  c.name...
  • How to Show Single quotation mark apostrophe mark in sql
    Use Quotaion Mark like this for show this select 'Hello, He is Ravi''s brother.'

Copyright © Sql Solutions | Powered by Blogger
Design by Ravi Budania