Solved

Can't find the right sytax.

Posted on 2011-03-17
6
242 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

22 Experts available now in Live!

Get 1:1 Help Now