Quantcast
Channel: Conditional Update Statement T-SQL - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Conditional Update Statement T-SQL

$
0
0

I'm trying to update a very wide table in SQL that has misused a few columns for years resulting in some very messy data. I want to write a conditional update statement to shift data from one column to another as they are all basically one column off where they are supposed to be if they are not null.

UPDATE SOME_TABLESET Data1 = (CASE WHEN Data1 IS NOT NULL THEN Data1 = Data0 ELSE Data1),SET Data2 = (CASE WHEN Data2 IS NOT NULL then Data2 = Data1 ELSE Data2),SET Data3 = (CASE WHEN Data3 IS NOT NULL then Data3 = Data2 ELSE Data3)GO

How can I construct this to "shift" data conditionally as I am trying to do?

EXAMPLE - Data0 is ALWAYS NULL (Was never used properly)

So Data is always like so: NULL || ABC || XHG || XYZ

In this case, I need to move each of these columns one to the left in a given row. The data within is all VARCHAR.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images