Solved

Can't find the right sytax.

Posted on 2011-03-17
6
263 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 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