Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2005
    Posts
    71

    Unanswered: CAST Integer to Character with leading 0's

    Currently I am using:
    Code:
    REPLICATE(0, 6-len(CONVERT(varchar(6), integerfield1))) + CONVERT(Varchar(255), integerfield1)
    to create a character field of
    000001 for the value of 1 and
    000010 for the value of 10 and so on...
    Is there a shorter method/function for creating these results.
    I absolutely need it to be a left justified number with leading zeroes because i and concatenating a string, number, sting and comparing it to a given string.

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    right('000000' + CONVERT(varchar(6), integerfield1), 6)
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  3. #3
    Join Date
    Apr 2007
    Posts
    183
    REPLACE(STR(IntegerField1, 6, 0), ' ', '0')

  4. #4
    Join Date
    Jun 2004
    Location
    Long Island
    Posts
    696
    Quote Originally Posted by hicpics
    Currently I am using:
    Code:
    REPLICATE(0, 6-len(CONVERT(varchar(6), integerfield1))) + CONVERT(Varchar(255), integerfield1)
    to create a character field of
    000001 for the value of 1 and
    000010 for the value of 10 and so on...
    Is there a shorter method/function for creating these results.
    I absolutely need it to be a left justified number with leading zeroes because i and concatenating a string, number, sting and comparing it to a given string.

    Sounds like a MicroHedge (Sungard) file requirement, had to do the same about 6 months ago.

  5. #5
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Code:
    --  ptp  20080822  See http://www.dbforums.com/showthread.php?t=1633256
    
    DECLARE @i		INT
    ,  @j			INT
    ,  @d1			DATETIME
    ,  @d2			DATETIME
    ,  @d3			DATETIME
    ,  @d4			DATETIME
    ,  @c1			VARCHAR(10)
    ,  @c2			VARCHAR(10)
    ,  @c3			VARCHAR(10)
    
    SET @i = 100000
    
    SELECT @j = @i, @d1 = GetDate()
    
    WHILE 0 < @j
       BEGIN
          SELECT @c1 = REPLICATE(0, 6 - Len(CONVERT(varchar(6), @j))) 
    +        CONVERT(Varchar(255), @j)
          SET @j = @j - 1
       END
    
    SELECT @j = @i, @d2 = GetDate()
    
    WHILE 0 < @j
       BEGIN
          SELECT @c2 = Right('000000' + Convert(VARCHAR(6), @j), 6)
          SET @j = @j - 1
       END
    
    SELECT @j = @i, @d3 = GetDate()
    
    WHILE 0 < @j
       BEGIN
          SELECT @c3 = Replace(Str(@j, 6), ' ', '0')
          SET @j = @j - 1
       END
    
    SELECT @j = @i, @d4 = GetDate()
    
    SELECT DateDiff(ms, @d1, @d2) AS original
    ,  DateDiff(ms, @d2, @d3) AS Blindman
    ,  DateDiff(ms, @d3, @d4) AS Peso
    ,  @c1, @c2, @c3
    -PatP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •