• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

sql server update colA based on value in colB

I have to update a column in a table based on the value of another column in the same table.  

col A holds the varchar value of a number
col B is the range of that number

if col A = '30' then col B needs to be updated to '30-40'
if col A = '50' then col B needs to be updated to '50-100'

please help.
1 Solution
SharathData EngineerCommented:
How do you determine the ranges? Do you want a range of gap 10 or 50? What are all possible ranges you are looking for?
Scott PletcherSenior DBACommented:
You'd be far better off with colB as a computed column, since you wouldn't have to UPDATE when colA changed:

ALTER TABLE dbo.tablename
    WHEN colA LIKE '3[0-9]' OR colA = '40' THEN '30-40'
    WHEN colA LIKE '[56789][0-9]' OR colA = '100' THEN '50-100'
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now