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

 

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

TSQL: excute a SSIS/dts package in a stored procedure bypassing parameters

DECLARE @returncode int
DECLARE @cmd varchar(4000)
DECLARE @ImgURL varchar(250)
DECLARE @ImageToolPath varchar(250)
DECLARE @Msg varchar(500)
SET @ImgURL = ‘http://www.webdatenblatt.de/cds/de/?pid=f822e0c1E_’
SET @ImageToolPath = ‘C:\Daten\imgtest’

 

–execute ssis package with parameters

SELECT @cmd = ‘dtexec /f  “C:\SSIS\GetImages.dtsx”‘
+ ‘ /SET \Package.Variables[ImgURL].Properties[Value];’ + @ImgURL
+ ‘ /SET \Package.Variables[ImageToolPath].Properties[Value];’ + @ImageToolPath
EXEC @returncode = xp_cmdshell @cmd
IF @ReturnCode <> 0
BEGIN
SELECT @Msg = ‘SSIS package execution failed GetImages.dtsx on SQL Server\Instance: ‘ + QUOTENAME(@@servername,'”‘)
EXEC msdb.dbo.sp_send_dbmail @profile_name=’SQLServerMail’, @recipients = ‘xy@xy.xy’ , @body = @Msg, @subject = ‘SSIS Execution Failure’
END
ELSE
BEGIN
print ‘image download for import items finished’
END