473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

IF within SELECT

Hello,
I'm working on a DB on which I can run only SELECT commands, so I'd
like to modify these UPDATE commands, into one SELECT command, which
makes use of IF statements:

update Table
set field1=field2

update Table
set field1=field3
where 'condition'

I've modified it into this:
select *,
IF(condition,field3,field2)
END IF as newField
from Table

I tried this way too:
select *,
IF(condition) THEN field3
ELSE field2
END IF as newField
from Table
It doesn't work... I hope you can help me
Thank you very much in advance
Carmelo
Jun 27 '08 #1
2 2739
IF is a control of flow statement and you cannot use it like that in the
query. You can use the CASE function:

SELECT CASE WHEN condition
THEN col3
ELSE col2
END AS new_col
FROM Table;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #2
On 10 Giu, 00:28, "Plamen Ratchev" <Pla...@SQLStudio.comwrote:
IF is a control of flow statement and you cannot use it like that in the
query. You can use the CASE function:

SELECT CASE WHEN condition
* * * * * * * * * THEN col3
* * * * * * * * * ELSE col2
* * * * * END AS new_col
FROM Table;

HTH,

Plamen Ratchevhttp://www.SQLStudio.com
OK thanks. I'm using successfully the CASE function
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Abby Lee | last post by:
My code does what I want (works unless there is a lot of volume...works for this month cause not a lot of items marked paid yet...) but the page times out for last month because there is just so...
2
by: M Wells | last post by:
Hi All, This seems like a tricky question to me. I have a Stored Procedure that encapsulates a number of updates to various tables within a transaction. However, at a later part of the...
4
by: JJA | last post by:
I need to show data for the top 3 zipcodes for EACH of the top 5 counties. I feel totally blocked on how to make this work properly. Here is my code - anything you can suggest will be greatly...
0
by: Jeff Boes | last post by:
I hope this helps someone else ... I had struggled some time ago with attempts to get a rank of values query to work, but then I gave up and set it aside. I had another reason to attack it, and in...
4
by: bizt | last post by:
Hi, I want to find an easy way to select only rows within a certain range when I do a select. Im sure I have seen this done with a SELECT statement but cant remember if or how it was done. Can...
1
by: Birky | last post by:
I am truly stuck and need some help on how to put this form together; any help or suggestions you can provide would be greatly appreciated. (Sorry this one is so long but I believe it is necessary to...
0
by: super.raddish | last post by:
Greetings one and all, I have a single source XML file and I am parsing the "records" within it into separate XML files using xsl:result-document. All that works fine. I am using the Saxon...
2
by: deve8ore | last post by:
Hello, I've already created a dropdown so an end user can pull up a specific file, and this will create a link to a variety of files (.xls, .txt., .pdf, ect...). I'd like to set about twenty...
2
by: =?Utf-8?B?SmFzb24gSGFydHNvZQ==?= | last post by:
is it possible to somehow select certain records inside a data adapter for example i fill my adapter with: (Select * FROM tbProduct WHERE StyleN = ImageRef AND LNKorDNL='LNK' AND...
6
by: BobRoyAce | last post by:
Let's say that I am performing a bunch of insert/update queries within a transaction that is created as follows: Dim cnn As New SqlClient.SqlConnection(My.Settings.GRPConnectionString)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.