简体   繁体   中英

datename sql function not showing the correct day

I went online and saw for example that 03 Nov 2011 is Thursday and 04 Nov 2011 is Friday. I used the function

SELECT datename (dw, 04-11-2011) on my server and it says Saturday instead of Friday. How can I set it up to display correctly? Or what am I missing?

EDIT : Thank you all for your inputs. However, most of you pointed out the format I am using and not the fact that it isn't set up correctly. Someone posted (and think deleted his comment) of this link: https://msdn.microsoft.com/en-us/library/ms181598.aspx .

so, I used set datefirst 1 and to get 4th of November i used select datename(dw, '2011-11-04')

Use ISO standard date formats, so you know what date you are getting. Either:

select datename(dw, '2011-04-11') 

or

select datename(dw, '2011-11-04') 

(Or, because the above formats are subject to one internationalization setting where they can be misinterpreted, you can leave out the hyphens so they are unambiguously in the YYYYMMDD format.)

Also, enclose date constants in single quotes.

1 Use single double for date values

2 In SQL Server use unambiguous date format YYYYMMDD or YYYY-MM-DDTHH:MM:SS

select datename(weekday, '20110411') as day_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM