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
 General SQL Server Forums
 New to SQL Server Programming
 Day of the week test

Author  Topic 

masterdineen
Aged Yak Warrior

550 Posts

Posted - 2010-11-01 : 12:40:31
I have a little script below to enter a record into a table, but depending what day of the week it is. I want to populate the last column eg mon, tues, wed etc.....

declare @day char(8)
set @day = 0
select case @day
WHEN DATEPART(dw, getdate()) IN (1) THEN @day = 'Sun'
WHEN DATEPART(dw, getdate()) IN (2) THEN @day = 'Mon'
WHEN DATEPART(dw, getdate()) IN (3) THEN @day = 'Tues'
WHEN DATEPART(dw, getdate()) IN (4) THEN @day = 'Wed'
WHEN DATEPART(dw, getdate()) IN (5) THEN @day = 'Thurs'
WHEN DATEPART(dw, getdate()) IN (6) THEN @day = 'Fri'
WHEN DATEPART(dw, getdate()) IN (7) THEN @day = 'Sat'
end

insert into priceing values ( '1', '50.00', '120', '17.5', '@day' )

when i run the script i receive the following error.

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


MCTS / MCITP certified

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-01 : 12:42:54
it should be:-


declare @day char(8)
set @day = ''
select @day=case
WHEN DATEPART(dw, getdate()) =1 THEN 'Sun'
WHEN DATEPART(dw, getdate()) =2 THEN 'Mon'
WHEN DATEPART(dw, getdate()) =3 THEN 'Tues'
WHEN DATEPART(dw, getdate()) =4 THEN 'Wed'
WHEN DATEPART(dw, getdate()) =5 THEN 'Thurs'
WHEN DATEPART(dw, getdate()) =6 THEN 'Fri'
WHEN DATEPART(dw, getdate()) =7 THEN 'Sat'
end

insert into priceing values ( '1', '50.00', '120', '17.5', @day)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

masterdineen
Aged Yak Warrior

550 Posts

Posted - 2010-11-01 : 12:53:30
thank you very much
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-01 : 12:55:12
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -