如何在SQL Server中使用datetime DataType更改日期的格式?

时间:2012-07-08 11:42:08

标签: sql sql-server-2008-r2

我有以下查询从数据库中检索我需要的内容:

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

我希望将其显示为DateSubmitted,而不是将6/23/2012 7:15:00 AM列下的日期显示为((Jun-2012))。

怎么做?

2 个答案:

答案 0 :(得分:1)

您可以使用以下示例查询获得以上格式:

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

在您的查询中,您可以尝试这样:

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

请确保dbo.SafetySuggestionsLog.DateSubmitted列应为datetime类型,否则将其投放到datetime类型

答案 1 :(得分:0)

另一种更加不言自明的替代方案:

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

在您的查询中使用(以及别名,这将使您和其他人更容易阅读您的查询):

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;

在SQL Server 2012中,使用新的FORMAT()函数会更加容易,该函数与C# format function具有粗略的平等,支持可选的文化,并且不需要记住样式编号:

SELECT FORMAT(GETDATE(), 'MMM-yyyy');