TSQL: fnPadLeft: fills up a string with padChar from left

— Description: fills up a string  with @len  @padChar+@var from left

— Example: SELECT dbo.[fnPadLeft](‘1’, ‘0’, 3) returns ‘001’

— =============================================

CREATE Function[dbo].[fnLBCustomPadLeft]

(@varvarchar(max),

@padCharchar(1)=‘ ‘,

@lenint)

returnsvarchar(max)

as
begin

return replicate(@PadChar,@lenLen(@var))+@var

end

 

Advertisements

TSQL: fnCharCount: counting char in a string

CREATE  FUNCTION[dbo].[fnCharCount](@stringvarchar(8000),@characterchar(1))

RETURNS SMALLINT

AS

BEGIN

DECLARE@stringtruncvarchar(8000);

SET@stringtrunc=REPLACE(@string,@character,);— remove the specified character

RETURN (LEN(@string)LEN(@stringtrunc));— return the difference in length, this is the char count

END