Home > other >  SQL-server 'WEEKDAY' is not a recognized built-in function name [closed]
SQL-server 'WEEKDAY' is not a recognized built-in function name [closed]

Time:09-17

I'm using SQL Server Management Studio v18.9.2 which I downloaded about a month ago (Aug. 2021). I've searched for help here on SO, SQLServerTutorial.net, and w3schools.com, and can't figure out what I'm doing wrong.

I'm trying to get three columns in the results: RentalID, StartTime (which exists as a datetime data type), and a new column with the day of the week (Monday, Tuesday, etc.). I've limited the query to just the records since June 2021.

I've tried these three function names: weekday, dw, and w, in upper and lower case combinations, and all return the same error:

SELECT RentalID, StartTime, DATENAME(WEEKDAY(StartTime)) AS DayOfWeek
FROM FullData
WHERE StartTime > '2021-05-31'

Error msg: 'WEEKDAY' is not a recognized built-in function name.

Then I get an error with this:

DATENAME(WEEKDAY(StartTime)) AS DayOfWeek
FROM FullData
WHERE StartTime > '2021-05-31'

Error msg: Incorrect syntax near 'WEEKDAY'.

Then I get an error with this:

DATENAME(WEEKDAY, StartTime) AS DayOfWeek
FROM FullData
WHERE StartTime > '2021-05-31'

Error msg: Incorrect syntax near 'WEEKDAY'.

When I try out the MONTH function, I get a different error:

SELECT RentalID, StartTime, DATENAME(MONTH(StartTime)) AS Month
FROM FullData
WHERE StartTime > '2021-05-31'
ORDER BY Month

Error msg: The datename function requires 2 argument(s). I thought the 2 arguments would be the MONTH and StartTime but I guess, not.

CodePudding user response:

You're using the date part as a function call not as an argument to the datename function:

DATENAME(WEEKDAY, StartTime) AS DayOfWeek
-- Two args ----^

Or

DATENAME(MONTH, StartTime) AS [Month]
-- Two args --^

CodePudding user response:

The correct syntax for DATENAME() has two arguments:

DATENAME(WEEKDAY, StartTime)

DATENAME(MONTH, StartTime)
         
  • Related