How to Calculate Age in Ms-Sql server with SQL Example
3 Comments |
5776 Views | Post by: Admin
Calcluate age using SQL, SQL query to get age, Sql Tip
Here Pawan Kumar has explained, How to Calculate Age in Ms-Sql Server with SQL Example
Here Pawan Kumar has explained, How to Calculate Age in Ms-Sql server with SQL Example
In previous post I explained In MS-SQL Server How to get tables columns with increment value with SQL Query Example, How can we copy data From one Table to another Table in MS-SQL Server, Get todays Day Name SQL server, how can we convert negative a number to positive number in SQL Server 2005/2008.
In some condtions, We have to calculate the age of the employee so for that we have to calculate the age into the C# or also we can calculate age in Ms-Sql server. For this we can create a function in Ms-Sql server and when ever we find the need we can use that function.
Write the following SQL Query into SQL Server Management Studio. In this condition, I'm using the Ms-Sql server 2008 Edition.
This simple SQL will help you to calcuate you age using Ms-Sql server
DECLARE @BirthDate DATETIME
DECLARE @CurrentDate DATETIME
SELECT @CurrentDate = getdate(), @BirthDate = '08/21/1981' --Add your date of birth here
SELECT DATEDIFF(YY, @BirthDate, @CurrentDate) -
WHEN( (MONTH(@BirthDate)*100 + DAY(@BirthDate)) > (MONTH(@CurrentDate)*100 + DAY(@CurrentDate)) )
THEN 1 ELSE 0 END
Output will be like this
so guys, we have learned in this tutorial, How to Calculate Age in Ms-Sql server with SQL Example
Download Source Codes