SQL Server Date Formats — ffffd

前端 未结 6 501
迷失自我
迷失自我 2021-01-13 19:11

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

相关标签:
6条回答
  • 2021-01-13 19:30

    try this

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

    Read up DATEPART here

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

    0 讨论(0)
  • 2021-01-13 19:37

    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

    0 讨论(0)
  • 2021-01-13 19:39

    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)
    
    0 讨论(0)
  • 2021-01-13 19:50

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

    SELECT LEFT(DATENAME(dw, GETDATE()), 3)
    
    0 讨论(0)
  • 2021-01-13 19:53

    You can't ffffd 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.

    0 讨论(0)
  • 2021-01-13 19:55

    For SQL Server 2012+:

    SELECT FORMAT(GETUTCDATE(),'ffffd') 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

    0 讨论(0)
提交回复
热议问题