Friday, January 07, 2005

Get Weekday (name) from a date in SQL Server

Today one of a friend wanted to know how to find out if todays is Sunday/Friday/Monday etc or not (ofcourse thats from within SQL Server, or else ur mom wud have told u :) )..
so here is the query that you can use:

select DATENAME(dw , )

you can pass "dw" or "weekday" as a first parameter to date name which specifies the part of date returned.

so a sample query will be

SELECT case when DATENAME(dw , getdate()) <> 'Sunday' then 'Its Not Sunday today' else 'Its Sunday today' end

since today is friday the result was

"Its Not Sunday today"

No rocket science, just doing small things the right way!

No comments: