Get Day of Week in SQL Server 2005/2008

Get day of week in SQL Server 2005/2008

Use DATENAME or DATEPART:

SELECT DATENAME(dw,GETDATE()) -- Friday
SELECT DATEPART(dw,GETDATE()) -- 6

How to get first day of the week and last day of the week in sql server 2008?

Here's how you can do it:

DECLARE @yourdate date = getdate()
Select dateadd(ww, datediff(ww, 0, @yourdate), 0)
Select dateadd(ww, datediff(ww, 0, @yourdate), 4)

You set @yourdate to the date you want. The first SELECT will give you the first day and the second SELECT will give you the last date

Start the first day of the week as Sunday in SQL

This should do the trick:

SELECT
UID,
DATEADD(d, -1, DATEDIFF(d, -1, t.DT)/7 * 7) [WeekBeginDate] ,
SUM(HOURS) AS TOTAL_HOURS
FROM myTable t
WHERE DT >= DATEADD(WEEK, -6, GetDate())
GROUP BY UID, DATEDIFF(d, -1, t.DT)/7

Get day name from Datetime using SQL Server 2008 R2

select DATENAME(weekday,getdate())

SELECT CONVERT(VARCHAR(10), '2014-11-14 00:00:00', 105) AS DATE,
Datename(weekday, '2014-11-14 00:00:00') AS DayNameofDate

MS SQL Server 2008 :Getting start date and end date of the week to next 8 weeks

Try this:

DECLARE @startDate DATETIME
DECLARE @currentDate DATETIME
DECLARE @numberOfWeeks INT

DECLARE @dates TABLE(
StartDate DateTime,
EndDate DateTime
)

SET @startDate = GETDATE()--'2012-01-01' -- Put whatever you want here
SET @numberOfWeeks = 8 -- Choose number of weeks here
SET @currentDate = @startDate

while @currentDate < dateadd(week, @numberOfWeeks, @startDate)
begin
INSERT INTO @Dates(StartDate, EndDate) VALUES (@currentDate, dateadd(day, 6, @currentDate))
set @currentDate = dateadd(day, 7, @currentDate);
end

SELECT * FROM @dates

This will give you something like this:

StartDate           EndDate 
21/03/2013 11:22:46 27/03/2013 11:22:46
28/03/2013 11:22:46 03/04/2013 11:22:46
04/04/2013 11:22:46 10/04/2013 11:22:46
11/04/2013 11:22:46 17/04/2013 11:22:46
18/04/2013 11:22:46 24/04/2013 11:22:46
25/04/2013 11:22:46 01/05/2013 11:22:46
02/05/2013 11:22:46 08/05/2013 11:22:46
09/05/2013 11:22:46 15/05/2013 11:22:46

Or you could tweak the final select if you don't want the time component, like this:

SELECT CONVERT(VARCHAR, StartDate, 103), CONVERT(VARCHAR, EndDate, 103) FROM @dates


Related Topics



Leave a reply



Submit