?
Solved

SQL Update Script for Between Product Codes

Posted on 2016-07-27
4
Medium Priority
?
45 Views
Last Modified: 2016-07-28
Hello,

I need a SQL Script to update my Products Table.

I have Product Code & Barcodes in the Products Table like:
Product Code  Barcode
ABC                   123456789

I need to put a zero in front of the number so:
Product Code  Barcode
ABC                   0123456789

Can someone please provide an update script for between a product code range to add the zero in front of the barcode number?

My Table is called: Products
My Fields are called: ProductCode & ProductBarcode

Thanks.
0
Comment
Question by:jspc
[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
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Olaf Doschke
ID: 41732421
What is the final goal? A certain length like 10 char barcodes with leading 0s?

You can do that by right('0000000000'+ Barcode, 10)

declare @sampledata as table (barcode varchar(10));
insert into @sampledata values ('123'),('4567'),('987654321');

Select right('0000000000'+barcode,10) as paddedbarcode from @sampledata;
or
Select FORMAT(try_convert(int,barcode), 'd10') as paddedbarcode from @sampledata;

Open in new window


Once you're happy with the result the update is

Update yourtable Set barcode = ... Where Len(barcode)<10

Open in new window

That assumes a varchar barcode field with varying length.

Bye, Olaf.
0
 

Author Comment

by:jspc
ID: 41732457
The final goal is to insert a 0 in front of the barcode.
Or in other examples it may be a 9 in front.

I want to run this script between a range of product codes though.

Length is not a concern.
0
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 2000 total points
ID: 41732465
update Products
set ProductBarcode = '0'+ProductBarcode
where ProductCode between  'ABC' and 'DEF'
0
 
LVL 30

Expert Comment

by:Olaf Doschke
ID: 41732493
>Length is not a concern.
Well, if you want to prefix a 0 you implicitly want a +1 length. The one and the other way to describe the demand are interchangable.

Bye, Olaf.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

762 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