对于给定日期,查询应返回特定日期的周名称。
DECLARE @Date DATE ='2014-06-02 00:00:00.000'
如果我给出上述日期,则应返回该特定周的周名称。
期待这样的输出
______________________________________________________________________________________ mon tue wed thu fri sat sun ______________________________________________________________________________________ 2014-06-02 2014-06-03 2014-06-04 2014-06-05 2014-06-06 2014-06-07 2014-06-08 ______________________________________________________________________________________
我有这样的表
____________________________________________________________ empID empCheckInTime empCheckOut ____________________________________________________________ 1 Jun 2 2014 12:37 PM Jun 2 2014 12:44 PM 2 Jun 6 2014 12:02 PM Jun 6 2014 12:03 PM ____________________________________________________________
我的SQL查询是
DECLARE @StartDate DATETIME = '2014-06-02 00:00:00.000'
DECLARE @EndDate DATETIME = '2014-06-09 23:59:59.999'
SELECT CONVERT(VARCHAR(10),DATEDIFF(HOUR,@StartDate,@EndDate))+'.'
+CONVERT(VARCHAR(10),DATEDIFF(MINUTE,@StartDate,@EndDate)% 60)
AS NoOfHours
帮助我在SQL Server 2008中选择查询
答案 0 :(得分:1)
以下是查询:
DECLARE @Date DATE = '2014-06-02'
;WITH CTE AS
(
SELECT @Date [Date],
DATENAME(DW,@Date) DayOfTheWeek,
1 i
UNION ALL
SELECT DATEADD(DAY,1,[Date]),
DATENAME(DW,DATEADD(DAY,1,[Date])),
i + 1
FROM CTE
WHERE i + 1 <=7
)
SELECT DayOfTheWeek,
[Date]
FROM CTE
ORDER BY i ASC