1

I have the following query that retrieves what I need from the database:

SELECT     
   dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, 
   dbo.SafetySuggestionsLog.Description, dbo.employee.Name, 
   dbo.SafetySuggestionsLog.Username, dbo.Divisions.DivisionShortcut, 
   dbo.SafetySuggestionsType.Type, 
   ISNULL(dbo.SafetySuggestionsStatus.Status, '-') AS Status, 
   dbo.SafetySuggestionsLog.DateSubmitted
FROM 
   dbo.employee 
INNER JOIN
   dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username 
INNER JOIN
   dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode 
INNER JOIN
   dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID 
LEFT OUTER JOIN
   dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
ORDER BY 
   dbo.SafetySuggestionsLog.DateSubmitted DESC

Instead of showing the date under DateSubmitted column as (6/23/2012 7:15:00 AM), I want to display it as (Jun-2012).

How to do that?

4

2 回答 2

1

You can have above format using below example query :

  SELECT REPLACE(RIGHT(CONVERT(VARCHAR(11), CAST('6/23/2012 7:15:00 AM' AS DATETIME), 106), 8), ' ', '-') AS [Mon-YYYY]

In your query, you can try like this :

SELECT     dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, 
                      dbo.SafetySuggestionsLog.Username, dbo.Divisions.DivisionShortcut, dbo.SafetySuggestionsType.Type, ISNULL(dbo.SafetySuggestionsStatus.Status, '-') AS Status, 
                      REPLACE(RIGHT(CONVERT(VARCHAR(11),dbo.SafetySuggestionsLog.DateSubmitted, 106), 8), ' ', '-') AS [Mon-YYYY]
FROM         dbo.employee INNER JOIN
                      dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
                      dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode INNER JOIN
                      dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID LEFT OUTER JOIN
                      dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
ORDER BY dbo.SafetySuggestionsLog.DateSubmitted DESC

Be sure,dbo.SafetySuggestionsLog.DateSubmitted column should be datetime type, otherwise cast it to datetime type

于 2012-07-08T11:49:00.620 回答
0

Another alternative that is more self-explanatory:

SELECT CONVERT(CHAR(3), DATENAME(MONTH, GETDATE())) + '-' + RTRIM(YEAR(GETDATE()));

Used in your query (along with aliases, which will make your queries much easier for you and others to read):

SELECT 
  sl.ID, 
  sl.Title, 
  sl.[Description], 
  e.employee.Name, 
  sl.Username, 
  d.DivisionShortcut, 
  st.[Type], 
  ISNULL(ss.[Status], '-') AS [Status],
  sl.DateSubmitted,
  MonthSubmitted = CONVERT(CHAR(3), DATENAME(MONTH, sl.DateSubmitted)) 
    + '-' + RTRIM(YEAR(sl.DateSubmitted))
FROM 
  dbo.employee AS e
  INNER JOIN dbo.SafetySuggestionsLog AS sl
    ON e.Username = sl.Username 
  INNER JOIN dbo.Divisions AS d
    ON e.DivisionCode = d.SapCode 
  INNER JOIN dbo.SafetySuggestionsType AS st
    ON sl.TypeID = st.ID 
  LEFT OUTER JOIN dbo.SafetySuggestionsStatus AS ss 
    ON sl.StatusID = ss.ID
ORDER BY 
  sl.DateSubmitted DESC;

In SQL Server 2012, this will be much easier with the new FORMAT() function, which has rough parity with the C# format function, supports optional culture, and doesn't require having to memorize style numbers:

SELECT FORMAT(GETDATE(), 'MMM-yyyy');
于 2012-07-08T12:57:29.950 回答