Femmer
Femmer

Reputation: 132

Conditional Update Statement

I have a process that is dumping a CSV file into a temp table then I am formatting the data to put into an actual table.

I have run into an issue where sometimes one of the values is coming up in scientific notation and my table is set to DECIMAL(13,9). Essentially what I am wanting to do is update any of the values that are in scientific notation to 0 because they are so low they might as well be 0 in this use case.

There are 9 columns that could potentially have this scientific notation and I know that I could just have 9 update statements that update each column one at a time. I can do this if I have to, just thought someone might have a better idea than that.

So, my question is: is there a way to update all of these 9 columns with one statement?

Here is the dataset:

SELECT Value, MyPropOccupancy, CompSetOccupancy, IndexOccupancy,
       MyPropADR, CompSetADR, IndexADR, MyPropRevPar, CompSetRevPar, IndexRevPar
    FROM #fileColumnsChanges

enter image description here

So in this circumstance, I would want to update CompSetOccupancy (Year To Date) and CompSetADR (Running 12 Month) to 0.

Upvotes: 0

Views: 159

Answers (1)

Thailo
Thailo

Reputation: 1404

You can write just one UPDATE here with 9 'setters'. If your numeric values in that temp table are based on (n)varchar you need to take this into account before resetting the actual value. Below I used 0.0001 as an example threshold:

UPDATE
  #fileColumnsChanges
SET
  CompSetOccupancy = IIF(CAST(CompSetOccupancy AS FLOAT) < 0.0001, 0, CompSetOccupancy)
, CompSetADR = IIF(CompSetADR AS FLOAT) < 0.0001, 0, CompSetADR);

Otherwise you can just omit the CAST:

  CompSetOccupancy = IIF(CompSetOccupancy < 0.0001, 0, CompSetOccupancy)

Obviously you have to amend the UPDATE with the other fields.

PS It helps answering questions like these if you'd show us what is/was in the actual #table.

Upvotes: 1

Related Questions