Help with SQL query - show today birthday on top of the table
I have a table in SQL Server 2008
I need to have the first results be people who's birthday is today, and the rest of the results sort by fname,lname
for example, i have this table:
fname|lname|Tdate
AA |111 |03/08
BB |222 |18/01
FF |11 |20/01
CC |333 |31/07
DD |444 |10/04
EE |22 开发者_运维百科 |20/01
today = 20/01/2011
and i need to see this:
fname|lname|Tdate
FF |11 |20/01
EE |22 |20/01
AA |111 |03/08
BB |222 |18/01
CC |333 |31/07
DD |444 |10/04
How to do it ?
thanks in advance
Something like this at the end of your query.
order by
case when cast(tdate as date) = cast(getdate() as date) THEN 1
else 0 end desc,
fname, lname
SELECT FNAME, LNAME, TDATE ,1 AS RANK FROM WHERE TDATE=GETDATE()
UNION
SELECT FNAME, LNAME, TDATE ,2 AS RANK FROM WHERE TDATE<>GETDATE()
ORDER BY
SIGN(ABS(DATEDIFF(day, Tdate, GETDATE()))),
Tdate
精彩评论