Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Express Edition and Compact Edition (2005)
 Issue With SQL Script

Author  Topic 

SilentCodingOne
Starting Member

20 Posts

Posted - 2008-12-07 : 00:18:17
I'm working on a homework problem that requires the creation of a script that determines the integer value of the current day of the week using GETDATE() and DATEPART(). Then using a CASE statement I have to take the integer value of the day of the week and assign it the appropriate character representation of the day. The last step is to use PRINT to show the day of the week. Whenever I run the script I have I get the following error:

Msg 156, Level 15, State 1, Line 15
Incorrect syntax near the keyword 'PRINT'.

Not sure what I'm doing wrong and hoping you guys can help. Here is my code:


DECLARE @DayOfWeek INT
DECLARE @DayName VARCHAR(10)

SET @DayOfWeek = DATEPART(weekday,GETDATE())

SELECT @DayName = CASE @DayOfWeek
WHEN 1 THEN 'Sunday'
WHEN 2 THEN 'Monday'
WHEN 3 THEN 'Tuesday'
WHEN 4 THEN 'Wednesday'
WHEN 5 THEN 'Thursday'
WHEN 6 THEN 'Friday'
WHEN 7 THEN 'Saturday'

PRINT @DayName

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-07 : 01:31:44
Your CASE statement should be like this:

CASE
WHEN @var1 = 1 THEN 'asdf'
WHEN @var2 = 2 THEN ...
...
END

PRINT ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SilentCodingOne
Starting Member

20 Posts

Posted - 2008-12-07 : 02:02:59
I updated my code to what you recommended. I no longer get the incorrect syntax near PRINT error but now it is saying incorrect syntax near =. If I change CASE @DayOfWeek to just CASE the error goes back to incorrect syntax near PRINT


SELECT @DayName = CASE @DayOfWeek
WHEN @DayOfWeek = 1 THEN 'Sunday'
WHEN @DayOfWeek = 2 THEN 'Monday'
WHEN @DayOfWeek = 3 THEN 'Tuesday'
WHEN @DayOfWeek = 4 THEN 'Wednesday'
WHEN @DayOfWeek = 5 THEN 'Thursday'
WHEN @DayOfWeek = 6 THEN 'Friday'
WHEN @DayOfWeek = 7 THEN 'Saturday'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-07 : 02:07:19
quote:
Originally posted by SilentCodingOne

I updated my code to what you recommended. I no longer get the incorrect syntax near PRINT error but now it is saying incorrect syntax near =. If I change CASE @DayOfWeek to just CASE the error goes back to incorrect syntax near PRINT


SELECT @DayName = CASE @DayOfWeek
WHEN @DayOfWeek = 1 THEN 'Sunday'
WHEN @DayOfWeek = 2 THEN 'Monday'
WHEN @DayOfWeek = 3 THEN 'Tuesday'
WHEN @DayOfWeek = 4 THEN 'Wednesday'
WHEN @DayOfWeek = 5 THEN 'Thursday'
WHEN @DayOfWeek = 6 THEN 'Friday'
WHEN @DayOfWeek = 7 THEN 'Saturday'



it should be below

DECLARE @DayOfWeek INT
DECLARE @DayName VARCHAR(10)

SET @DayOfWeek = DATEPART(weekday,GETDATE())

SELECT @DayName = CASE @DayOfWeek
WHEN 1 THEN 'Sunday'
WHEN 2 THEN 'Monday'
WHEN 3 THEN 'Tuesday'
WHEN 4 THEN 'Wednesday'
WHEN 5 THEN 'Thursday'
WHEN 6 THEN 'Friday'
WHEN 7 THEN 'Saturday'
END
PRINT @DayName




Go to Top of Page

SilentCodingOne
Starting Member

20 Posts

Posted - 2008-12-07 : 02:08:40
Thanks that worked
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-07 : 02:13:51
welcome
Go to Top of Page
   

- Advertisement -