7

Is there a quick one-liner to call datepart in Sql Server and get back the name of the day instead of just the number?

select datepart(dw, getdate());

This will return 1-7, with Sunday being 1. I would like 'Sunday' instead of 1.

DT7
  • 1,615
  • 14
  • 26
Eric Z Beard
  • 37,669
  • 27
  • 100
  • 145

4 Answers4

12
select datename(weekday, getdate());
Erick B
  • 1,242
  • 10
  • 21
1

It actually took me more searching than I thought it would to find this answer. It's funny how you can use a technology for ages and never know about simple functions like this.

select datename(dw,  getdate())

I'm not sure how localization would work with this function. Getting the name client-side is probably the answer, but it would be nice to do it on the database. Would Sql Server use the collation setting to determine the output for this?

Eric Z Beard
  • 37,669
  • 27
  • 100
  • 145
0

This is not possible without using the result to select the day yourself. For one thing the textual representation of the day is locale-dependent. For another the returned value depends upon the 'datefirst' setting.

Andrew Grant
  • 58,260
  • 22
  • 130
  • 143
0

If you want a localizable solution, just join the result against a table with the names and numbers.

Lars Mæhlum
  • 6,074
  • 3
  • 28
  • 32