The day of the week can be retrieved in SQL Server by using the DatePart function. The value returned by function is between 1 (Sunday) and 7 (Saturday). To convert this to a string representing the day of the week, use a CASE statement.
Method 1:
Create function running following script:
CREATE FUNCTION dbo.udf_DayOfWeek(@dtDate DATETIME)
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @rtDayofWeek VARCHAR(10)
SELECT @rtDayofWeek = CASE DATEPART(weekday,@dtDate)
WHEN 1 THEN 'Sunday'
WHEN 2 THEN 'Monday'
WHEN 3 THEN 'Tuesday'
WHEN 4 THEN 'Wednesday'
WHEN 5 THEN 'Thursday'
WHEN 6 THEN 'Friday'
WHEN 7 THEN 'Saturday'
END
RETURN (@rtDayofWeek)
END
GO
Call this function like this:
SELECT dbo.udf_DayOfWeek(GETDATE()) AS DayOfWeek
ResultSet:
DayOfWeek
———-
Monday
Method 2: (This is update from comments I received below)
SELECT DATENAME(dw, GETDATE())
Reference : Pinal Dave (https://blog.sqlauthority.com)
56 Comments. Leave new
Can i get spanish names whit DATENAME ?
Yes Only if the default language of the server is Spanish
my question is i putup a any date of future and want to make calendre according to this way of any flight
my table is like
flightno sun mon tues wed thurs friday saturday
1001 y y n n n y n
1002 n y n y y n y
Sebastian,
This may help you
;WITH CTE AS
(
SELECT
1 AS ID,
SUBSTRING(days,0,CHARINDEX(‘,’,days)) AS Days,
SUBSTRING(days,CHARINDEX(‘,’,days)+1,LEN(days))RemainingStr,
2%8 AS DayID
FROM
sys.sysLanguages
WHERE
alias = ‘spanish’
UNION ALL
SELECT
ID+1 AS ID ,
CASE WHEN CHARINDEX(‘,’,RemainingStr) > 0 THEN SUBSTRING(RemainingStr,0,CHARINDEX(‘,’,RemainingStr))ELSE RemainingStr END AS Days,
SUBSTRING(RemainingStr,CHARINDEX(‘,’,RemainingStr)+1,LEN(RemainingStr))RemainingStr,
(DayID+1)%8 AS DayID
FROM
CTE
WHERE
ID < 7
)
SELECT Days,DAyID FROM CTE
HI, great post…
But, it’s possible with only data result “15-07-2007” (called date) you know what is the first day of the week?
For example I want this result:
datepart(month, date) + ‘ ‘ + datepart(week, date) + ‘ ‘ + “First day of the week”
How I catch “First day of the week” ???
thanks in advance
Probably you need
select dateadd(week,datediff(week,0,date),0)
Time and Date only
SELECT CONVERT(VARCHAR(12), GETDATE(), 106)
SELECT CONVERT(VARCHAR(8), GETDATE(), 108)
@Nagarajan,
Note that all your TIME and DATEs are just VARCHARs
You should do this formation in the front end application
or
If you use SQL Server 2008, make use of new datatypes Time and Date
Hi All,
I want to show the first date of the week, example today date 25-02-2010, i want to 21-02-2010.
Inputs given year,month,week.(example:2010,02,4)
i want to output of this week first date
Gururajan.K
Hi Pinal,
I want the start date of week and the end date of week.A date will be passed as a parameter,based on the week number of that date I need the start & end date of that week.
Hi,
Can anyone help me to get the first “Sunday” in (Integer) of the given date… which will return the day of the first sunday
Example:
Date: ’05/22/2010′
Then I want to get the day (integer) of the first sunday of this date ’05/22/2010′
It should Return 2
Because the first sunday in the month of May 2010 is 2
Continued…
Because I want to get the Week1 to Week5
In my example there are 5 sundays in the Month of May 2010
Week 1 – date will be May 1 to May 2
Week 2 – date will be May 3 to May 9
Week 3 – date will be May 10 to May 16
Week 4 – date will be May 17 to May 23
Week 5 – date will be May 24 to May 30
Week 6 – date will be May 31
The only thing i want to know is the first sunday of the given date in integer in this case…
I want to get the 2,
Because the first sunday in the month of May 2010 is 2
just want to ask…
how to parse the each date in a year into MS SQL database? if i must store those date in an array?? if so, how can i parse them ??
hope to hear some comments…..
really need.
how to display weeknumbers and and its correponding year?
Hi, sorry I’m new on SQL codification … could you please tell me how can I include this function in a SELECT codification?
Regards.
Alejandro
Hi Pinal,
i have day of week , on the basis of day of week i want to find the next particular day (date)
for Example i have 3 =Tuesday ,i want to calculate next tuesday date on basis of current current date
Hi,
I want Monday – Sunday week numbers.
I have to group by this week number and i have to get count of records in between that week.
Thanks in advance.
Hi,
I want a method like DATEPART(WK,getdate()) , but it should take from monday – sunday.
Thanks in advance.
HI Everyone,
Can please help me any one on this issue:
when i’m runing this query:
SELECT DATENAME(wk,GETDATE())
ans:33
but,with this :
SELECT DATENAME(wk,GETDATE()-1)
ans:33
can anyone plz tel why it is this ,actualy i think output should be 32 when we do -1.
thanks in advance
SELECT DATENAME(wk,GETDATE())-1
ans: 32
thanks
I want to subtract from StartDate from current Date
from Table
select Hours = datediff(hh,0,DtDiff,StartDate) , Minutes = datepart(minute,DtDiff)
(select DtDiff = convert(datetime,’20110715 10:5:45′)- convert(DateTime,GetDate())
)a
from
Table Name
this is my query plz correction on it
To get a deterministic value for the day of week for a given date you could use a combination of datepart and @@firstday. Otherwise your dependent on the settings on the server.
Check out the following site for a better solution:
Hi,
I have a question that, I want to see a Table design in SQL Server. So which query I want to write to see the the design.
Thanks and Regards,
Madhu…
EXEC sp_help ‘table_name’