Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Probably a newb question, but...

I have a table with a VARCHAR field containing numbers 1 through 300. I need to place a leading zero in front of any numbers less than 10 while retaining the original number if it is 10 or greater (i.e. 1=01, 10=10 and 300=300).

SELECT DISTINCT RIGHT('0'+CONVERT(VARCHAR,[FacilityCode]),3) FROM...

This returns 1=01, 10=010 and 300=300 (using the same examples as above)

EDIT: I'm trying to do this within a query so that I can do lookups without altering either table.

Can someone lend a brother a hand here? My brain just ain't working.

share|improve this question
    
Any particular reason you don't want 1 to be 001 ? – RolandoMySQLDBA Jul 24 '14 at 15:20
    
RolandoMySQLDBA - The related table has varchar values which follow the example above. Mark, I'm using MSSQL 2008 r2. All... I'm trying to do this through a query in order to select items in a related table which formats the numbers (which are actually varchars) in the manner illustrated above. – SnagQueensHubby Jul 24 '14 at 15:32
up vote 9 down vote accepted

The safest way is probably to only add zeroes when the length of the column is 1 character:

UPDATE
   Table
SET
   MyCol = '0' + MyCol
WHERE
   LEN(MyCol) = 1;

This will cover all numbers under 10 and also ignore any that already have a leading 0.

EDIT

To just select the data try:

SELECT
   MyColPadded = CASE WHEN LEN(MyCol) = 1 THEN '0' + MyCol ELSE MyCol END
FROM
   MyTable;
share|improve this answer

(it looks from your example code that you are using MS SQL Server, but you should add an appropriate tag (and/or body text) to your question to indicate this if so, or if not which DBMS you are actually using)

Any of the options above should work fine, I would generally use the "prefix 0s then use RIGHT() option as you have but the others are perfectly valid), but a word of warning if you expect new rows (or updates to existing rows) to contain low numbers - if that is possible then you will end up with unpadded values creeping back in. To avoid this you could use either an AFTER or INSTEAD OF trigger to force values into the right format as they are inserted/updated to stop the problem coming back (without needing to change all code points that save to that table). Be careful to verify performance though: triggers can be very powerful but it is also easy to create a performance nightmare with them.

share|improve this answer

You can use a case statement for this, if it must be formatted in the database and you can't persist it.

Select case when len(facilityCode) = 1 then 
          '0' else '' end + facilityCode as facilityCode
From YourTable

It sounds like you are doing this as a FK, if so you can't have a FK constraint on a calculation. To make doing the join easier, you might consider a calculated column.

 ALTER TABLE yourTable 
    ADD cFacilityCode AS (
      case when len(facilityCode) = 1 then '0' else '' end + facilityCode);
share|improve this answer

If you wanted a 3 digit number try this.

SELECT DISTINCT 
RIGHT('000'+CAST(ISNULL([FacilityCode],0) AS VARCHAR),3) 
FROM...

Result: 1=001, 10=010 and 300=300

An 8 digit number would be:

SELECT DISTINCT 
RIGHT('00000000'+CAST(ISNULL([FacilityCode],0) AS VARCHAR),8) 
FROM...

Result: 1=00000001, 10=00000010 and 300=00000300

This has the advantage of working with subqueries:

SELECT t_lac.FORCE_OVERWRITE
            , ISNULL(lay.ID,0) AS lacIDLayout
            --, t_lac.lacName
            , CASE WHEN t_lac.lacIDControlType = 18
                THEN 'PAN' + RIGHT('000' + CAST(ISNULL((SELECT ID FROM Layout l WHERE l.layName = t_lac.lacName),0) AS VARCHAR),3) + '_' + t_lac.lacName
                ELSE t_lac.lacName
                END AS lacName
            , t_lac.lacIDControlType
            , t_lac.lacStyle 
            FROM @T_LayoutControl t_lac
            LEFT JOIN Layout lay ON lay.layName = t_lac.layName
share|improve this answer
UPDATE MyTable
SET MyColumn = '0' + Mycolumn
WHERE MyColumn < '10'
AND MyColumn NOT LIKE '0%';
share|improve this answer
1  
The OP stated that the minimum value is 1, but in general, if you want at least two digits, and you have zero, you do want to add another zero, so I would use LEN (as in JNK's answer) rather than LIKE. – hvd Jul 24 '14 at 19:47
3  
The logic behind the answer is sane but the answer is not correct for a very different reason than the above comments. MyColumn is a varchar, so the condition MyColumn < '10' will not result in TRUE when the value in the column is '2' or '3' or ... '9'. For char values '9' < '10' is false. – ypercubeᵀᴹ Jul 25 '14 at 9:32
1  
Agree with @ypercube. You could change the first comparison to MyColumn < 10 but that might break as we don't know if MyColumn is guaranteed to ever contain only numbers. – Andriy M Jul 25 '14 at 10:16
 UPDATE table SET cust_id = concat('00',tbl_table.cust_id) WHERE CHAR_LENGTH(cust_id) = 1

Try this .. it concatenate a leading 00 to the row content where string lengh is one

share|improve this answer
    
I don't think SQL server has a CHAR_LENGTH() or a CONCAT() function. And the OP does not want to update the table, only select the column with some values padded with 0. – ypercubeᵀᴹ Dec 17 '15 at 9:21
    
this query for updation ..if you want to add leading 0 to the number where char lengh is 1 – Divesh choudhary Dec 21 '15 at 8:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.