Padding Statement

  • Hi

     

    Is there any replacement for Oracle LPad, RPad statements in SQL Server.

     

    Wishes

    Jawad

  • What exactly do you want to do....there're a couple of functions that could be used - STUFF(), SPACE(), REPLICATE()...etc..







    **ASCII stupid question, get a stupid ANSI !!!**

  • Try these.

     

    CREATE  FUNCTION dbo.fn_PadLeft  (@String varchar(8000), @padchar char(1),@maxLen tinyint)

    /*--=========================================================================

      Procedure ........... dbo.fn_PadLeft

      File Name ........... dbo.fn_PadLeft.SQL

      Author .............. Robert Nabolotnyj

      Date Written ........ 03 Aug 1999

      Purpose ............. Left pads characters to string.

     

      Syntax ..............

           declare @String varchar(8000), @padchar char(1),@maxLen tinyint

           SET @String = '1'

           SET @padchar = '0'

                SET @maxLen = 5

           SELECT dbo.fn_PadLeft(@String ,@padchar,@maxLen)

      Modification.........

      Date  By  Description

    --=========================================================================*/

    RETURNS varchar(8000)

     WITH SCHEMABINDING

     AS

    BEGIN

     DECLARE @sString varchar(8000)

     SET @sString = RIGHT(REPLICATE(@padchar,@maxLen) + LTRIM(RTRIM(@String)),@maxLen)

     RETURN(@sString)

    END

    -- END PROCEDURES

    GO

    CREATE FUNCTION dbo.fn_PadRight  (@String varchar(8000), @padchar char(1),@maxLen int)

    /*--=========================================================================

      Procedure ........... dbo.fn_PadLeft

      File Name ........... dbo.fn_PadLeft.SQL

      Author .............. Robert Nabolotnyj

      Date Written ........ 03 Aug 1999

      Purpose ............. Left pads characters to string.

     

      Syntax ..............

           declare @String varchar(8000), @padchar char(1),@maxLen tinyint

           SET @String = '1'

           SET @padchar = '0'

                SET @maxLen = 5

           SELECT dbo.fn_PadRight(@String ,@padchar,@maxLen)

      Modification.........

      Date  By  Description

    --=========================================================================*/

    RETURNS varchar(8000)

     WITH SCHEMABINDING

     AS

    BEGIN

     DECLARE @sString varchar(8000)

     SET @sString = LEFT(LTRIM(RTRIM(@String))+ REPLICATE(@padchar,@maxLen) ,@maxLen)

     RETURN(@sString)

    END

    -- END PROCEDURES

    GO

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply