SQL Server Date Formats — ddd

随声附和 提交于 2019-12-20 01:38:30

问题


How do I get the day of the week (in ddd format, so Mon, Tue etc.) in SQL ? I don't see anything about it in the CAST and CONVERT documentation..


回答1:


Many ways to do it, here's one way:

SELECT LEFT(DATENAME(dw, GETDATE()), 3)



回答2:


You could use the DATENAME method to extract the day and then use the SUBSTRING to only take the first 3 chars.

SELECT SUBSTRING(DATENAME(DW, '09/11/2009'), 1, 3)



回答3:


I would use
SELECT CONVERT(CHAR(3),DATENAME(weekday,GETDATE()))
so as to avoid using another function in the SQL, the conversion to a CHAR(3) implicitly takes the first 3 characters.

Jonathan




回答4:


try this

SELECT DATEPART(DW, '1/1/2009')

Read up DATEPART here

http://msdn.microsoft.com/en-us/library/ms174420.aspx




回答5:


You can't ddd out of the box but you can do the full day

e.g.

select datename(weekday,getdate())

returns 'Monday' and you can just take the first 3 letters.




回答6:


For SQL Server 2012+:

SELECT FORMAT(GETUTCDATE(),'ddd') AS ShortDayName

This is much cleaner than a substring solution. The FORMAT() function can also be passed a culture for locale-aware formatting.

Note that this works for other date parts, such as month:

SELECT FORMAT(GETUTCDATE(),'MMM') AS ShortMonthName

FORMAT() Reference



来源:https://stackoverflow.com/questions/1701850/sql-server-date-formats-ddd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!