x

How to view the Suggsetions sorted in the Database for the last three months?

I am a New ASP.NET Developer and I am trying to develop a simple suggestion box system. I have the following part of my database desing:

User Table: Username, Name, DivisionCode... etc

Division Table: SapCode, Division

SuggestionLog Table: ID, Title, Description, submittedDate, Username

(The first attribute is the primary key in each table and the attribute (submittedDate) is of DateTime data type)

Now, I need to develop a table that shows suggestions for the last three months. I already developed a query that shows the Employee Name, Username, Division, Suggestion Title, Suggestion Description. All what I want now is to show the Month. For example, to show the suggestions for the last three months, the Month column should show: Jan-2012, Dec-2011, Nov-2011 So how to do that?

My current SQL query:

SELECT     dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsType.Type, dbo.SafetySuggestionsLog.Username, 
                      dbo.employee.Name, dbo.Divisions.DivisionShortcut
FROM         dbo.Divisions INNER JOIN
                      dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode INNER JOIN
                      dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
                      dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID

The desired output is to display:

Employee Name, Username, Division, SuggestionTitle, SuggstionDescription, SuggestionType Month(submissionDate)
more ▼

asked Feb 26, 2012 at 08:22 AM in Default

essence388 gravatar image

essence388
21 11 11 13

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

Hi there, try adding the following to your select statement

LEFT(DATENAME(mm,submissionDate),3) + '-' + CAST(DATEPART(yyyy, submissionDate) AS VARCHAR(4))

This will add the required output

Thanks
more ▼

answered Feb 26, 2012 at 11:26 PM

Dave Morrison gravatar image

Dave Morrison
472 2 3 4

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x595
x375

asked: Feb 26, 2012 at 08:22 AM

Seen: 770 times

Last Updated: Feb 26, 2012 at 11:26 PM