SQL If statement

  • Thread starter Thread starter Brendan MAther
  • Start date Start date
B

Brendan MAther

Hi There,

I'm trying to find records in a table whose ID Number is less than 27000 and
if it is add 12000 to this number. I'm trying to make and if statment how
is this done (ie what is the format) in SQL??

Thanks,
Brendan
 
You could do it using a WHERE clause:

SELECT ID + 12000 AS NewID, Field1, Field2
FROM MyTable
WHERE ID < 27000

If you want to to see all of the records, whether or not the ID is less than
27000 to begin with, you could either use a UNION query:

SELECT ID + 12000 AS NewID, Field1, Field2
FROM MyTable
WHERE ID < 27000
UNION
SELECT ID, Field1, Field2
FROM MyTable
WHERE ID >= 27000

or you could use an IIf (Immediate If) statement in your query

SELECT IIf([ID] < 27000, [ID] + 12000, [ID]) AS NewID, Field1, Field2
FROM MyTable
 
NewID: iif(ID<27000,ID+12000,ID)

NewID will be set equal to id+12000 if and only if it is
less than 27000, otherwise it will be set equal to ID.
All records will be returned.
 
Back
Top