Solved

Can't find the right sytax.

Posted on 2011-03-17
6
281 Views
Last Modified: 2012-05-11
I would like to use a query to update some fields using the current month as a condition.
I can't get the notation right (I'm rather new to sql) but I guess it should look something like this:

SELECT CASE DATEPART(mm,getdate()) WHEN 1 THEN UPDATE empire SET darkforce='evil' where person='vader' ELSE
SELECT CASE DATEPART(mm,getdate()) WHEN 2 THEN UPDATE empire SET darkforce='evil' where person='palpetine' ELSE
SELECT CASE DATEPART(mm,getdate()) WHEN 3 THEN UPDATE empire SET transport='falcon' where person='hansolo'

Could anyone let me know what the right syntax should be?

Thanks in advance!

Best regards,
Clyde.
0
Comment
Question by:Clyde_Radcliffe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35156175
what are you trying to do here ? update has nothing to do with select.
Details please.
0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 166 total points
ID: 35156229

try this way

update empire
set darkforce = case
                            when  DATEPART(mm,getdate()) = 1 then
                                'vader'
                            when  DATEPART(mm,getdate()) = 2 then
                                'palpetine'
                          end
where DATEPART(mm,getdate()) IN (1, 2)
0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 167 total points
ID: 35156263
UPDATE table
SET CASE
            WHEN MONTH(getdate()) IN (1,2) THEN darkforce
            WHEN MONTH(getdate())=3 THEN transport
      END=CASE
            WHEN MONTH(getdate()) IN (1,2) THEN 'evil'
            WHEN MONTH(getdate())=3 THEN 'falcon'
      END
WHERE person=CASE
            WHEN MONTH(getdate())=1 THEN 'vader'
            WHEN MONTH(getdate())=2 THEN 'palpetine'
            WHEN MONTH(getdate())=3 THEN 'hansolo'
      END

0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 8

Assisted Solution

by:pdd1lan
pdd1lan earned 167 total points
ID: 35156275
UPDATE empire SET
  CASE DATEPART(mm,getdate()) WHEN 1 then darkforce='evil' where person='vader'
  CASE DATEPART(mm,getdate()) WHEN 2 thendarkforce='evil' where person='palpetine'
 CASE DATEPART(mm,getdate()) WHEN 3 then transport='falcon'  where person='hansolo'
 else
    darkforce='something'
 END
0
 

Author Comment

by:Clyde_Radcliffe
ID: 35156313
Sorry my explanation of the issue was rather poor... :-)
I would like to update a field on a certain month.

If the month is january then field x should be updated with value a
If the month is february then field x should be updated with value b
If the month is march then field x should be updated with value c etc...
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35156385
You should test the solutions before accepting them. I don't think pdd1lan's solution will work. You can't place conditions or assignments in a case. Only expressions.
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question