Solved

Can't find the right sytax.

Posted on 2011-03-17
6
255 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
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:ewangoya
ewangoya 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now