Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have list of records from which I have to extract Birthdays between two given dates, regardless of the year.
That is, I want the birthdays falls between dates, say 2015-12-01 and 2015-12-31
The simple between Query checks whether the Date of Birth fields falls between these two or not.
Please help...
Thanks
Posted
Updated 20-Dec-19 0:43am

Use DATEPART and extract the month you want : SQL Server DATEPART() Function[^]
 
Share this answer
 
Comments
Member 12133159 12-Jan-16 4:19am    
I have to implement this in ASP.NET application.....???
Mehdi Gholam 12-Jan-16 4:47am    
... and what is the problem?
Member 12133159 12-Jan-16 4:55am    
Problem is that I can run this using sqldatasource but unable to run this query in Code behind.....
Member 12133159 12-Jan-16 5:01am    
SELECT name, dob,mobile FROM rpd WHERE ((DATEDIFF(dd, getdate(), DATEADD(yyyy, DATEDIFF(yyyy, dob, getdate()) + 1, dob))) % 366 <= 10) and status='Alive' and dob !='1900-01-01 00:00:00'
ORDER BY MONTH(dob)asc,DAY(dob)asc

This code is to fetch birthdays of upcoming 10 days from now, I want to implement this using Between command(between two dates).....
You can try this way using linq :

C#
DateTime fromDate = DateTime.Parse("2015-12-01");
DateTime toDate = DateTime.Parse("2015-12-31");

var finalValue = from a in yourlist where (a.DOB.Date >= fromDate.Date && a.DOB.Date <= toDate.Date) select a;

[Edit]
One small demo :
C#
   //User Class
   public class User
   {
       public string Name { get; set; }
       public DateTime DOB { get; set; }
   }

//Create a list with values :
  List<User> lst = new List<User>();
            lst.Add(new User{ Name = "Value 0", DOB = Convert.ToDateTime("2015-11-05") });
            lst.Add(new User{ Name = "Raj 0", DOB = Convert.ToDateTime("2015-12-01") });
            lst.Add(new User{ Name = "Raj 1", DOB = Convert.ToDateTime("2015-12-02") });
            lst.Add(new User{ Name = "Raj 2", DOB = Convert.ToDateTime("2015-12-03") });
            lst.Add(new User{ Name = "Raj 3", DOB = Convert.ToDateTime("2015-12-31") });

//then write filter code :
 DateTime fromDate = DateTime.Parse("2015-12-01");
            DateTime toDate = DateTime.Parse("2015-12-31");

            var finalValue = from a in lst where (a.DOB.Date >= fromDate.Date && a.DOB.Date <= toDate.Date) select a;

//here in finalValue you will see filtered data.



Good luck.
 
Share this answer
 
v3
Below solution will work

C#
using System;
using System.Collections.Generic;
using System.Linq;
			
  public class Person
   {
       public string PersonName { get; set; }
       public DateTime DateOfBirth { get; set; }
   }

public class Program
{
	public static void Main()
	{
		
		//Create a list with values :
  		List<Person> lst = new List<Person>();
            lst.Add(new Person{ PersonName = "Person 1", DateOfBirth = Convert.ToDateTime("2015-6-05") });
            lst.Add(new Person{ PersonName = "Person 2", DateOfBirth = Convert.ToDateTime("2014-05-01") });
            lst.Add(new Person{ PersonName = "Person 3", DateOfBirth = Convert.ToDateTime("2010-07-02") });
            lst.Add(new Person{ PersonName = "Person 4", DateOfBirth = Convert.ToDateTime("2005-08-03") });
            lst.Add(new Person{ PersonName = "Person 5", DateOfBirth = Convert.ToDateTime("2000-09-20") });
            lst.Add(new Person{ PersonName = "Person 6", DateOfBirth = Convert.ToDateTime("2000-06-15") });
            lst.Add(new Person{ PersonName = "Person 7", DateOfBirth = Convert.ToDateTime("2000-06-10") });
            lst.Add(new Person{ PersonName = "Person 8", DateOfBirth = Convert.ToDateTime("2000-08-21") });
 
		//then write filter code :
 		DateTime fromDate = DateTime.Parse("2055-06-15");
        DateTime toDate = DateTime.Parse("1015-08-20");
 
		var finalValue = from a in lst where (
					(a.DateOfBirth.Date.Month > fromDate.Date.Month || (a.DateOfBirth.Date.Month == fromDate.Date.Month && a.DateOfBirth.Date.Day >= fromDate.Date.Day) )
					&&
					(a.DateOfBirth.Date.Month < toDate.Date.Month || (a.DateOfBirth.Date.Month == toDate.Date.Month && a.DateOfBirth.Date.Day <= toDate.Date.Day))
				)
				select a;
		
		foreach( var p in finalValue){
			Console.WriteLine(p.PersonName);
		}
	}
}
 
Share this answer
 
Comments
Member 12133159 12-Jan-16 23:38pm    
Thanks Sir...
But tell me How to use my own Database instead of this List to fetch Birthdays from, as I have around Thousands of Records...
Sri Nivas (Vasu) 13-Jan-16 5:07am    
you can use same condition in the sql server query.

you can find how to get day part, month part from data in sql server
SQL QUERY

SELECT *
From TABLE
WHERE Datepart(day,@inputdate) = datepart(day, datecolumn)
and Datepart(month,@inputdate) = datepart(month, datecolumn)
 
Share this answer
 
I got the correct answer:

SQL
SELECT * From TABLE
WHERE Datepart(day,DATEFIELD) between datepart(day, @from_date)
and Datepart(day,@to_date) 
and 
Datepart(month,DATEFIELD) between datepart(month, @from_date)
and Datepart(month,@to_date)


Here, this Query will fetch Birthdays between date provided. Thanks to Everyone for their precious Knowledge and Information. :)
 
Share this answer
 
Comments
Sri Nivas (Vasu) 19-Jan-16 6:35am    
Cool :) . If you asked the question like.
SQL query for finding birthdays... then you would get the correct answer your question. Here you got answers for the c# and linq ;) .
We shall focus on Day and Month only so we can ignore specific Year. Specify any year just to complete date value (here I used 2016 as year value).


SQL Server Query to get employee records having birthday in July month:

SQL
Select * From tbl_EmployeeMaster 
Where Convert(Date, Convert(Varchar, Month(BirthDate)) + '/' + Convert(Varchar,Day(BirthDate)) + '/2016') Between '07/01/2016' And '07/31/2016' Order By BirthDate, EmployeeName
 
Share this answer
 
WHERE (Month(BDate) >= DATEPART(MONTH,'29-Oct-2019') AND Day(BDate) >= DATEPART(DD,'29-Oct-2019')) or (Month(BDate) <= DATEPART(MONTH,'02-Nov-2019') AND Day(BDate) <= DATEPART(DD,'02-Nov-2019'))
 
Share this answer
 
v2
Comments
CHill60 20-Dec-19 7:12am    
Not that different to Solution 5 from 4 years ago. Except Solution 5 is clearer and uses BETWEEN

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