Any way to improve performance of SQL Insert that uses a function

I created an SP that parses a record that had been imported as one long record, into individual fields.
The routine ran pretty quickly until I added a function to handle some situations with the incoming data.

Everything works but after adding the function the performance took a big hit.  I have seen many times that functions can be slow but this is the first time I've been bitten by it.

Just wondering if anyone can suggest a more efficient way of accomplishing the same thing.

The SP now looks like this:
USE [JTSConversion]
GO
/****** Object:  StoredProcedure [dbo].[aConvertspSQDTLienDtlBig1toImport_UsingSQL]    Script Date: 3/19/2018 7:18:23 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- 
-- =============================================
ALTER PROCEDURE [dbo].[aConvertspSQDTLienDtlBig1toImport_UsingSQL] 
	-- Add the parameters for the stored procedure here

AS
BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	--
	-- Clear the table to receive the import
	--
	Execute dbo.spDeleteAllRecsFromSQLTable 'SQDT_LienDetail_Import'   

    -- Insert statements for procedure here
INSERT INTO [dbo].SQDT_LienDetail_Import (
	    ControlNumber,
	    TaxYear,
	    DequenceNumber,
	    RecordType,
	    TaxType,
	    MultiUse,
	    PaidFlag,
	    ReceiptNumber,
	    PaidYYYY,
	    PaidMM,
	    PaidDD,
	    FaceAmtPaid,
	    PenaltyAmtPaid,
	    InterestAmtPaid,
	    CostAmtPaid,
	    CommissAmtPaid,
	    PaidToCounty,
	    SatByCounty,
	    SoldFlag,
	    PenaltyOverideAmtDue,
	    InterestOverideAmtDue,
	    CostOverideAmtDue,
	    OrigFaceAmtDue,
	    InterstStart_IntAdd_YYYY,
	    InterstStart_IntAdd_MM,
	    InterstStart_IntAdd_DD,
		Filler
		)
	SELECT
	    dbo.checkBlankString(SUbString(EntireRec,01,07)) as ControlNumber, 
	    dbo.checkBlankString(SUbString(EntireRec,08,04)) as TaxYear, 
	    dbo.checkBlankString(SUbString(EntireRec,12,03)) as DequenceNumber, 
	    dbo.checkBlankString(SUbString(EntireRec,15,01)) as RecordType, 
	    dbo.checkBlankString(SUbString(EntireRec,16,01)) as TaxType, 
	    dbo.checkBlankString(SUbString(EntireRec,17,98)) as MultiUse, 
	    dbo.checkBlankString(SUbString(EntireRec,115,01)) as PaidFlag, 
	    dbo.checkBlankString(SUbString(EntireRec,116,08)) as ReceiptNumber, 
	    dbo.checkBlankString(SUbString(EntireRec,124,04)) as PaidYYYY, 
	    dbo.checkBlankString(SUbString(EntireRec,128,02)) as PaidMM, 
	    dbo.checkBlankString(SUbString(EntireRec,130,02)) as PaidDD, 
	    dbo.checkBlankString(SUbString(EntireRec,132,09)) as FaceAmtPaid, 
	    dbo.checkBlankString(SUbString(EntireRec,141,09)) as PenaltyAmtPaid, 
	    dbo.checkBlankString(SUbString(EntireRec,150,09)) as InterestAmtPaid, 
	    dbo.checkBlankString(SUbString(EntireRec,159,06)) as CostAmtPaid, 
	    dbo.checkBlankString(SUbString(EntireRec,165,09)) as CommissAmtPaid, 
	    dbo.checkBlankString(SUbString(EntireRec,174,01)) as PaidToCounty, 
	    dbo.checkBlankString(SUbString(EntireRec,175,01)) as SatByCounty, 
	    dbo.checkBlankString(SUbString(EntireRec,176,01)) as SoldFlag, 
	    dbo.checkBlankString(SUbString(EntireRec,177,09)) as PenaltyOverideAmtDue, 
	    dbo.checkBlankString(SUbString(EntireRec,186,09)) as InterestOverideAmtDue, 
	    dbo.checkBlankString(SUbString(EntireRec,195,09)) as CostOverideAmtDue, 
	    dbo.checkBlankString(SUbString(EntireRec,204,09)) as OrigFaceAmtDue, 
	    dbo.checkBlankString(SUbString(EntireRec,213,04)) as InterstStart_IntAdd_YYYY, 
	    dbo.checkBlankString(SUbString(EntireRec,217,02)) as InterstStart_IntAdd_MM, 
	    dbo.checkBlankString(SUbString(EntireRec,219,02)) as InterstStart_IntAdd_DD, 
		dbo.checkBlankString(SUbString(EntireRec,221,33)) as Filler

	FROM aView_SQDT_EntireRecAs1Field_Import
END

Open in new window


The function that is being called is very simple:
USE [JTSConversion]
GO
/****** Object:  UserDefinedFunction [dbo].[checkBlankString]    Script Date: 3/19/2018 7:26:09 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER FUNCTION [dbo].[checkBlankString] (@passedString varchar (Max)) 
					RETURNS varchar(max)
-- combine RTrim and LTrim
AS
BEGIN
	
	return iIF( @passedstring is NULL or len(Ltrim(rtrim(@passedString)))<1,null,ltrim(rtrim(@passedstring)))

	

END

Open in new window

LVL 1
mlcktmguyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ManjuIT - Project ManagerCommented:
If the function is just for trim, then why not use Case?

Case when (SubString(EntireRec,01,07)) = null or LEN(LTRIM(RTrim((SubString(EntireRec,01,07))))) < 1 Then
                  Null Else Ltrim(Rtrim((SubString(EntireRec,01,07)))) End as ControlNumber
and so on for other fields. It should improve the performance.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
Why do you need to verify? Just apply the LTRIM and RTRIM functions directly:
(...)
SELECT
	    LTRIM(RTRIM(SUbString(EntireRec,01,07))) as ControlNumber, 
	    LTRIM(RTRIM(SUbString(EntireRec,08,04))) as TaxYear, 
(...)

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.