Forum Moderators: coopster

Message Too Old, No Replies

date range using ms sql

         

jm21

1:04 pm on Jan 11, 2010 (gmt 0)

10+ Year Member



I've developed a site using php/mysql but the client has shifted goal posts and wants me to port it to PHP/MSSQL 2005. After a few days of trial and error plus trolling through the net everything is working fine except a script i had developed similar to the facebook birthday app which lists your friends upcoming birthdays.
I had used MySQL's date functions and it works perfectely. The sql i used was:

$query = "SELECT * FROM bdays WHERE DAYOFYEAR( Bday_Date ) BETWEEN DAYOFYEAR( CURDATE( ) ) AND DAYOFYEAR( CURDATE( ) ) +7 ORDER BY DAY( Bday_Date ) ASC ";

My problem is that I havent found any function in MSSQL that can do something similar. Does anyone have any ideas on how i can replicate this in MSSQL?

vincevincevince

2:44 pm on Jan 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can play with FLOOR - basically round it off to the nearest year, and subtract this from the original value - multiply the remainder you get by the number of days in a year to get the equivalent of DAYOFYEAR.

syber

3:13 pm on Jan 13, 2010 (gmt 0)

10+ Year Member



I believe this will work:


SELECT *
FROM bdays
WHERE datepart(dy,Bday_Date) BETWEEN datepart(dy, GETDATE()) AND
datepart(dy,GETDATE()) +7
ORDER BY datepart(dd, Bday_Date ) ASC

jm21

7:20 am on Jan 14, 2010 (gmt 0)

10+ Year Member



The sql query works perfectly! Thanks syber, have spent days...i had actually tried similar queries using datepart but they werent working well for some reason.