Solved

SQL - Update Query with Case Condition

Posted on 2011-02-16
16
480 Views
Last Modified: 2012-05-11
SQL 2008 / 2005

I am trying to update a Column with Conditions :- If the Column Value has 9 digits, then Add 2 Zeros on its Prefix, If the Column Value has 8 digits, then Add 3 zeros on its Prefix.

This is an Update Query, - I am not sure, how to fix this issue.

0
Comment
Question by:chokka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +3
16 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34908896
If you use the Right command, you can achieve what you want:
RIGHT('000000000' + <field>, 9)

If you need to limit it to just fields with 8 or 9 digits, use a where clause that filters for values with lengths of 8 or 9.
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34908912
If the column is of integer datatype, try this query with cast
UPDATE YourTable
SET YourColumn = REPLICATE('0', 11 - LEN(CAST(YourColumn AS VARCHAR))) + CAST(YourColumn AS VARCHAR)
0
 

Author Comment

by:chokka
ID: 34908923


Name of the Table : BalanceReport

Name of the Column : NDC Varchar(50)
0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
LVL 13

Expert Comment

by:devlab2012
ID: 34908930
It will make the length 9 characters by appending zeros in the beginning.

update TblName set colName = right('000000000' + colName, 9)

It will work if you colName if of varchar type, you cannot prefix zeros in the numeric types
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34908934
REPLICATE will repeats the '0' so that total 11 characters will be there in result.
0
 
LVL 1

Expert Comment

by:Andrei Hetel
ID: 34908939
Something like that:  
update mytable  
 set prefix = case LEN(prefix)
                        when 9 then '00' + prefix
                        when 8 then '000' + prefix
                    end
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34908955
For VARCHAR Column, no need of that CASTing. Try this query
UPDATE YourTable
SET YourColumn = REPLICATE('0', 11 - LEN(YourColumn)) + YourColumn
0
 

Author Comment

by:chokka
ID: 34908980
Rajkumar and Devlab :-

In my Query, i need a condition whether total number of digits are 8 or 9

Based on that i need to add prefix zeros.

total number of digits in that column should be 11.
0
 
LVL 8

Expert Comment

by:rushShah
ID: 34909005

update BalanceReport
set NDC =case when len(NDC)=9 then '00' + NDC  when len(NDC )=8 then '000' + NDC

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34909007
Updated my query

UPDATE BalanceReport
SET NDC = REPLICATE('0', 11 - LEN(NDC)) + NDC
WHERE LEN(NDC) IN (8, 9)


create table #BalanceReport
(
	NDC varchar(50)
)

insert into #BalanceReport
select '12' union
select '1' union
select '12345' union
select '12345678'

UPDATE #BalanceReport
SET NDC = REPLICATE('0', 11 - LEN(NDC)) + NDC 
WHERE LEN(NDC) IN (8, 9)

select * from #BalanceReport

drop table #BalanceReport

Open in new window

0
 
LVL 8

Accepted Solution

by:
rushShah earned 500 total points
ID: 34909008
sorry try this,


update BalanceReport
set NDC =case when len(NDC)=9 then '00' + NDC  when len(NDC )=8 then '000' + NDC end

Open in new window

0
 

Author Comment

by:chokka
ID: 34909123
rushshah,

Thank you.

There is a small correction, i request your help in the Case Statement.

Some Columns already have 11 digits, in that scenario - we need to ignore.
Now the Case Condition runs all the Columns irrespective whether they have 7 or 8 or 9 or 10 or 11 digits.

If they have 7, we need to add 4 zeros
if they have 11, we dont want any zeros.

From your case condition, i am able to modify 7,8,9,10 length.
But for 11 digit length .. ?
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34909160
chokka:
Did you try my query - http:#34909007 ?
0
 

Author Comment

by:chokka
ID: 34909191
Rajkumar, Thank you for helping me.

I have not tried your query. Reason is that i - I am also concerned about QUery Performance.

In your query, you are creating a Temp Table and then Inserting .. Query Cost Estimation would be high.

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34909253
Oh man. That was for demonstrating purpose only
I was trying to show you how this query works with sample data
You need to query that I posted first in that comment alone.

Any way - Nice to see you got the solution.
Raj
0
 
LVL 8

Expert Comment

by:rushShah
ID: 34913256
hi chokka,

you can add different length case statements, for length=11, you don't need to add any condition as we don't need to update,

you can try like this,

you can also try Rajkumar's query which does the same thing in better readable format.
update BalanceReport
set NDC =case when len(NDC)=10 then '0' + NDC when len(NDC)=9 then '00' + NDC when len(NDC)=8 then '000' + NDC  when len(NDC )=7 then '0000' + NDC   when len(NDC )=6 then '00000' + NDC end

Open in new window

0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question