Solved

SQL Update Script for Between Product Codes

Posted on 2016-07-27
4
29 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
  • 2
4 Comments
 
LVL 29

Expert Comment

by:Olaf Doschke
Comment Utility
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
Comment Utility
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 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
Comment Utility
update Products
set ProductBarcode = '0'+ProductBarcode
where ProductCode between  'ABC' and 'DEF'
0
 
LVL 29

Expert Comment

by:Olaf Doschke
Comment Utility
>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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now