Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
I have sql table member like:-
id name dob
1 Ashwin 18/02/1989
2 Bhushan 30/12/1986
3 sachin 15/03/1991
4 ash 02/11/1980
5 kevin 03/03/1993


how to get record form table having age between 18 to 23 years.. Please Help..
Posted

You should use DATEDIFF[^] function.

SQL
SELECT id, name, dob
FROM member
WHERE DATEDIFF(yy, dob, GETDATE()) BETWEEN 18 AND 23
 
Share this answer
 
or DATEADD[^] function:

SQL
select * from member
where DOB between DATEADD(YEAR, -23, getdate()) and DATEADD(YEAR, -18, getdate())


but datediff better :)
 
Share this answer
 
Comments
Maciej Los 3-Mar-14 4:41am    
Good one, +5!
Vedat Ozan Oner 3-Mar-14 4:55am    
yours is still better. I've just showed another way.
[no name] 3-Mar-14 4:49am    
++5
try the below script..
Create table member
(
mno int identity(1,1) primary key,
name varchar(50),
dob varchar(50)
)
insert into member(name,dob) values('Ashwin','18/02/1989')
insert into member(name,dob) values('Bhushan','30/12/1986')
insert into member(name,dob) values('Sachin','15/03/1991')
insert into member(name,dob) values('Ash','02/11/1980')
insert into member(name,dob) values('Kevin','03/03/1993')

SELECT mno id,name,dob, DATEDIFF(YEAR,CONVERT(DATETIME, dob,105),GETDATE()) Age 
from member
where DATEDIFF(YEAR,CONVERT(DATETIME, dob,105),GETDATE()) between 18 and 23
drop table member


if you feel like useful then accept my answer and up vote it.
 
Share this answer
 
Comments
Vedat Ozan Oner 3-Mar-14 4:11am    
it is not good to store a date info as varchar !!
You need to create a user function first
SQL
create function [dbo].[fn_GetAge]
(@dob AS datetime,@currentDate as datetime)
returns int
AS
BEGIN
DECLARE @age int

IF cast(datepart(m,@currentDate) as int) > cast(datepart(m,@dob) as int)
  SET @age = cast(datediff(yyyy,@dob, @currentDate) as int)
else
  IF cast(datepart(m,@currentDate) as int) = cast(datepart(m,@dob) as int)
    IF datepart(d,@currentDate) >= datepart(d,@dob)
      SET @age = cast(datediff(yyyy,@dob, @currentDate) as int)
    ELSE
      SET @age = cast(datediff(yyyy,@dob, @currentDate) as int) -1
  ELSE
    SET @age = cast(datediff(yyyy,@dob, @currentDate) as int) - 1
RETURN @age
END

Once the user function has been created, use it like the one mentioned below
SQL
SELECT ID, NAME, DOB, fn_GetAge(DOB, GetDate()) AS AGE FROM TABLE

You can put the result in a temporary table to get the desired output.
Hope this solution helps
 
Share this answer
 
v3
SQL
SELECT mno, name, dob
FROM member  where datediff(year,convert(datetime,dob,103),GETDATE()) BETWEEN 18 AND 23
 
Share this answer
 
SQL
SELECT *
FROM member
WHERE DATEDIFF(yy, dob, GETDATE()) BETWEEN 18 AND 23
 
Share this answer
 
Comments
Maciej Los 3-Mar-14 4:43am    
The plagiat of the solution 1. That's the reason of 1 star.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900