Required Day as string; Ex: Sunday
Hi
I have a string which contains the year and month, now i wanted to know what is the starting day of that month in that particular year.
For Ex: string STR = "Jan\2006" then by any chance i can know the starting day of this month as whether it is Sunday etc. I need the string "sunday" as output. Also, I need to know how many number of days that particular month contains. Any function or stored procedure which will give out these two outputs will be much appreciated.
Thanks!
Santhosh
[540 byte] By [
edukulla] at [2007-12-22]
SQL Server has some built in functions to accomodate this functionality. Look at DateName and DatePart in Books On Line.
Here's an example to get you started.
Declare @Temp VarChar(20)
Set @Temp = 'Jan\2006'
Select DateName(Weekday, Convert(DateTime, '01-' + Replace(@Temp, '\', '-'))) As StartingWeekday,
Day(DateAdd(day, -1, DateAdd(Month, 1, Convert(DateTime, '01-' + Replace(@Temp, '\', '-'))))) As DaysInMonth
Where are you getting such a string? Can't you modify the application to send the date value in a proper format? Yes, you can parse and get the weekday etc but it will just be clumsy piece of code and probably unnecessarily complicated. It is best to use a data format that is easy to manipulate and understand. For example, using the ISO unseparated date format (YYYYMMDD) for date values will help a lot. It also protects you from different language or dateformat settings on the server. The code posted by the other user will work only for certain language / dateformat settings. It will fail if the language of the session is Czech for instance. Lastly, you will be better off having a calendar table in your database that has this information. You can then answer these type of questions with a simple query, handle language settings easily, have flexibility to make changes etc.