About Me

My photo
Paul has 18 years experience with Microsoft SQL Server. He has worked in the roles of production DBA, database developer, database architect, applications developer, business intelligence and data warehouse developer, and instructor for students aspiring for MCDBA certification. He has performed numerous data migrations and supported large databases (3 Terabyte, 1+ billion rows) with high transactions. He is a member of PASS, blogs about lessons learned from a developer’s approach to SQL Server administration, and has been the president of the Boulder SQL Server Users’ Group from January 2009 to present.

Tuesday, November 22, 2011


Handy function for ensuring a given path ends in a backslash. 

After being burned enough times calling a path + filename without the necessary backslash at the end of the path, I decided to fortify all my code with this function. 

It simply checks for a backslash at the end.  If not present, it adds one.

USE Admin

CREATE FUNCTION dbo.udfAddBackslashIfNec(
      @In varchar(1000)
RETURNS varchar(1001)
/*    DATE        AUTHOR            REMARKS
      11/22/11    PPaiva            Initial creation.

            SELECT dbo.udfAddBackslashIfNec('C:\blahblah')
            SELECT dbo.udfAddBackslashIfNec('C:\blahblah\')

            Returns @In with a backslash at the right, if necessary.


      DECLARE @LenIn int,
                  @Out varchar(201)
      SET @LenIn = Len(@In)

      IF Right(@In, 1) = '\'  OR   @In = ''
            SET @Out = @In
            SET @Out = @In + '\'

      RETURN @Out