?
Solved

Can't find the right sytax.

Posted on 2011-03-17
6
Medium Priority
?
283 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 664 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 668 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
LVL 8

Assisted Solution

by:pdd1lan
pdd1lan earned 668 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

762 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