I have a problem creating a function in T-SQL.
USE DB_ASSIG1
BEGIN
CREATE FUNCTION myFunction (@date DATETIME)
RETURNS INT
AS
BEGIN
DECLARE @day INT
CASE SELECT DATENAME(dw, @date)
WHEN 'Monday'
THEN SET @day = 1
WHEN 'Tuesday'
THEN SET @day = 2
WHEN 'Wednesday'
THEN SET @day = 3
WHEN 'Thursday'
THEN SET @day = 4
WHEN 'Friday'
THEN SET @day = 5
WHEN 'Saturday'
THEN SET @day = 6
WHEN 'Sunday'
THEN SET @day = 7
END
RETURN (@day)
END
END
Can't resolve it please help, by the way i'm kinda newish to T-SQL.
This is the error i get:
Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'FUNCTION'.
Msg 156, Level 15, State 1, Line 9 Incorrect syntax near the keyword 'CASE'.
Msg 137, Level 15, State 2, Line 9 Must declare the scalar variable "@date".
Msg 156, Level 15, State 1, Line 12 Incorrect syntax near the keyword 'WHEN'.
Msg 156, Level 15, State 1, Line 14 Incorrect syntax near the keyword 'WHEN'.
Msg 156, Level 15, State 1, Line 16 Incorrect syntax near the keyword 'WHEN'.
Msg 156, Level 15, State 1, Line 18 Incorrect syntax near the keyword 'WHEN'.
Msg 156, Level 15, State 1, Line 20 Incorrect syntax near the keyword 'WHEN'.
Msg 156, Level 15, State 1, Line 22 Incorrect syntax near the keyword 'WHEN'.
Msg 137, Level 15, State 1, Line 23 Must declare the scalar variable "@day".
Msg 137, Level 15, State 2, Line 26 Must declare the scalar variable "@day".
Completion time: 2020-05-22T22:54:38.4427014+02:00
begin/endblock. Aside: Is there some reason you're trying to writeDatePart?( @@DateFirst + DatePart( weekday, SampleDate ) - 1 ) % 7 + 1will always return an integer from1to7with1corresponding to Sunday regardless of the setting ofDateFirstorLanguage. You can offset it to start on Monday = 1. - HABOgoafteruse?Create functionneeds to be the first statement in a batch. - HABO