William
William

Reputation: 6610

Adding Leading Zeros to Records + Cast

I have an incremental field for each record in my table.

I want to add leading zeros to the number to make them all the same number of characters but also CAST() letters to the front of the number as an identifier.

Adding the leading zeros;

SELECT RIGHT('00000' + CONVERT(VARCHAR, No_), 6)

Adding my identifier;

SELECT 'FC' + CAST(No_ as varchar(50))

What is the correct syntax for combining these two statements to the one field? Is it possible?

Upvotes: 0

Views: 2847

Answers (2)

Joe G Joseph
Joe G Joseph

Reputation: 24046

try

SELECT 'FC' +RIGHT('00000' + CONVERT(VARCHAR, No_ ), 6)

Result: For No_ = 1

FC000001

For No_ = 222

FC000222

Upvotes: 2

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174329

Simply use this:

SELECT 'FC' + RIGHT('00000' + CONVERT(VARCHAR, No_), 6)

Upvotes: 1

Related Questions