SQL添加日期到现有查询的转换

时间:2016-08-23 07:06:06

标签: sql sql-server

我想使用此功能转换日期。我正在使用SQL Server 2014

CONVERT(varchar, sc.StartDate,103) + '" + " " + "' + '" + " - " + "' + CONVERT(varchar, sc.EndDate,103) SIPDate 

因此它将以dd / mm / yyyy显示 - dd / mm / yyyy。如何将其添加到查询中?谢谢。

查询是:

SELECT CONCAT(sc.StartDate, sc.ENDDate) SIPDate,
   COUNT (sj.LOComment) WeekReviewed,
   COUNT(sjd.WeekNo) TotalWeek,
   SUM(sjj.TotalDaysRecord) TotalDaysRecord,
   COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
    ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
    SELECT sj.WeekNo, 
           CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord 
    FROM StudentJournal sj
) AS sjj
    ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
    ON sc.AdminNo = sjd.AdminNo
GROUP BY CONCAT(sc.StartDate, sc.ENDDate)

2 个答案:

答案 0 :(得分:3)

您可以SELECTGROUP BY这种格式化的日期表达式,即:

SELECT CONVERT(varchar, sc.StartDate, 103) + " - " + CONVERT(varchar, sc.EndDate, 103) AS SIPDate,
       COUNT (sj.LOComment) WeekReviewed,
       COUNT(sjd.WeekNo) TotalWeek,
       SUM(sjj.TotalDaysRecord) TotalDaysRecord,
       COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
    ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
    SELECT sj.WeekNo, 
           CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord 
    FROM StudentJournal sj
) AS sjj
    ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
    ON sc.AdminNo = sjd.AdminNo
GROUP BY CONVERT(varchar, sc.StartDate, 103) + " - " + CONVERT(varchar, sc.EndDate, 103)

答案 1 :(得分:1)

试试这个。由于您的SQL SERVER是2014,您可以使用格式来格式化日期

SELECT     CONCAT(FORMAT(sc.StartDate,'dd/MM/yyy'),+ ' - ' +FORMAT(sc.ENDDate,'dd/MM/yyy'))  SIPDate,
   COUNT (sj.LOComment) WeekReviewed,
   COUNT(sjd.WeekNo) TotalWeek,
   SUM(sjj.TotalDaysRecord) TotalDaysRecord,
   COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
    ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
    SELECT sj.WeekNo, 
           CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
           CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord 
    FROM StudentJournal sj
) AS sjj
    ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
    ON sc.AdminNo = sjd.AdminNo
GROUP BY CONCAT(FORMAT(sc.StartDate,'dd/MM/yyy'),+ ' - ' +FORMAT(sc.ENDDate,'dd/MM/yyy'))