TSQL Challenge 18 - Generate text formatted month calendars
This is the first time we are coming up with a true calendar puzzle. Brad Schulz from TSQL Challenges Team has come up with a very interesting challenge which is all about generating calendars for given number of months. While the real-world application of this challenge is not very common, this is a good chance to test your date/calendar logic.
Have you got an interesting challenge idea? Send us your challenge idea and we will publish it through TSQL Challenges and you might even get paid!
Sample Data
Mth Yr
----------- -----------
8 2009
2 1900
10 1959
Your job is to take the above table and generate calendars for the months and years given in the table. A calendar should be generated for each row in the table, using a single query (and no temp tables or table variables)
Expected Output
+-----------------------------+
| FEBRUARY 1900 |
|=============================|
| Sun Mon Tue Wed Thu Fri Sat |
|-----------------------------|
| 1 2 3 |
| 4 5 6 7 8 9 10 |
| 11 12 13 14 15 16 17 |
| 18 19 20 21 22 23 24 |
| 25 26 27 28 |
+-----------------------------+
+-----------------------------+
| OCTOBER 1959 |
|=============================|
| Sun Mon Tue Wed Thu Fri Sat |
|-----------------------------|
| 1 2 3 |
| 4 5 6 7 8 9 10 |
| 11 12 13 14 15 16 17 |
| 18 19 20 21 22 23 24 |
| 25 26 27 28 29 30 31 |
+-----------------------------+
+-----------------------------+
| AUGUST 2009 |
|=============================|
| Sun Mon Tue Wed Thu Fri Sat |
|-----------------------------|
| 1 |
| 2 3 4 5 6 7 8 |
| 9 10 11 12 13 14 15 |
| 16 17 18 19 20 21 22 |
| 23 24 25 26 27 28 29 |
| 30 31 |
+-----------------------------+
Scripts
Use the following script to generate the sample data
declare @t table (Mth int, Yr int)
insert @t(Mth, Yr) select 8, 2009
insert @t(Mth, Yr) select 2, 1900
insert @t(Mth, Yr) select 10,1959
SELECT * FROM @t
Rules
This challenge demonstrates skill in using Date Functions, Grouping, Pivoting, Numbers Table, Recursion and CTEs.
- The resulting output is a single 31-character column called Calendar
- The Month should be uppercase and should be rendered in the language that is set at runtime
- The Month and Year are centered
- The Day-Of-The-Week names are the first 3 letters of the days of the week, rendered in the language that is set at runtime. Sunday must be the first column
- The calendars must be sorted in order
- The output must be unchanged regardless of the SET DATEFIRST setting
Here's a sample of the same output with SET LANGUAGE FRENCH:
+-----------------------------+
| FÉVRIER 1900 |
|=============================|
| dim lun mar mer jeu ven sam |
|-----------------------------|
| 1 2 3 |
| 4 5 6 7 8 9 10 |
| 11 12 13 14 15 16 17 |
| 18 19 20 21 22 23 24 |
| 25 26 27 28 |
+-----------------------------+
+-----------------------------+
| OCTOBRE 1959 |
|=============================|
| dim lun mar mer jeu ven sam |
|-----------------------------|
| 1 2 3 |
| 4 5 6 7 8 9 10 |
| 11 12 13 14 15 16 17 |
| 18 19 20 21 22 23 24 |
| 25 26 27 28 29 30 31 |
+-----------------------------+
+-----------------------------+
| AOÛT 2009 |
|=============================|
| dim lun mar mer jeu ven sam |
|-----------------------------|
| 1 |
| 2 3 4 5 6 7 8 |
| 9 10 11 12 13 14 15 |
| 16 17 18 19 20 21 22 |
| 23 24 25 26 27 28 29 |
| 30 31 |
+-----------------------------+
Notes
- Read the Submission Guidelines and make sure that your solution follows them.
- The solution should work on SQL Server 2005, 2008 or later versions
- Last date to submit your entries: Dec 14 2009 Midnight GMT
- Use this forum for any questions related to TSQL Challenge #18
| Syntax Bookmark of the Day! – IDENTITY Functions and commands |
|
|
About the Author
Challenge Evaluation Details
Though the evaluation of this TSQL Challenge is completed and winners are announced, you can still submit a solution and we will be very happy to review it. Before submitting, make sure that your solution passes the basic testing and logic testing and the performance statics are good. See the submission guidelines before submitting your solution.