Q:

Birthday next 7 days from 2 columns

I need to query my database regarding birthdays in next 7 days - 
with birthday date in two columns
  Name  Day   Month   
  Peter 15    8       
  Susi  20    8      
  Thor  14    9

Query = who has birthday next 7 days
Return 
Peter 15 - 8   
Susi  20 - 8

any help welcome -
Regards
Thorsten

A:

You did not specify RDBMS but this should return the results you want in SQL Server.  These will check if the month in the table is less than the current month, if so then it will use the following year:
select *
from 
(
    select name,
        cast(case 
                when datepart(month, getdate()) > [month] 
                 and datepart(day, getdate()) > [day]
                then cast(datepart(year, dateadd(year, 1, getdate())) as varchar(4))
                else cast(datepart(year, getdate()) as varchar(4)) end + '-'
            + cast([month] as varchar(2)) + '-'
            + cast([day] as varchar(2)) as datetime) bd
    from t1
) x
where datediff(day, getdate(), bd) >= 0 
  and datediff(day, getdate(), bd) < 7

See SQL Fiddle with Demo
Or:
select *
from 
(
    select name,
        cast(case 
        when datepart(month, getdate()) > [month] 
            and datepart(day, getdate()) > [day]
        then cast(datepart(year, dateadd(year, 1, getdate())) as varchar(4))
        else cast(datepart(year, getdate()) as varchar(4)) end + '-'
    + cast([month] as varchar(2)) + '-'
    + cast([day] as varchar(2)) as datetime) bd
    from t1
) x
where bd >= DateAdd(day, DateDiff(day, 0, getdate()), 0)
    and bd < DateAdd(day, DateDiff(day, 0, getdate()), 7)

MySQL version:
SELECT *
FROM
(
  select name,
    str_to_date(concat(case 
      when month(current_date()) > month
           and day(current_date()) > day
      then year(date_add(current_date, interval 1 year))
      else year(current_date()) 
    end, '-', month, '-', day), '%Y-%m-%d') birthdate
  from t1
) x
where birthdate >= Date(current_date())
  and birthdate < Date(Date_Add(current_date(), interval 7 day))

See SQL Fiddle with Demo

