Avatar of Larry Brister
Larry Brister
Flag for United States of America asked on

SQL string from 3 cases

I have a function that is working and builds a string that is returned

What I need is to build a string that reads

'Error: Invalid '  And whichever variable or variables are = 'N'

Example...If all are invalid or = 'N'

@Output = 'Invalid or missing policyNumber,  IssuerName, CADate'

If ONLY CADate is invalid
@Output = 'Invalid or missing CADate'

ALTER FUNCTION [dbo].[uf_ucc_structured_WithCA] (@DealID int) 

RETURNS VARCHAR(8000)
AS
BEGIN
	Declare @policyNumber varchar(40)
	Declare @IssuerName varchar(100)
	Declare @CADate varchar(30)
	Select	@policyNumber = x.AnnnuitantPolicyNumber,
			@IssuerName   = x.IssuerName,
			@CADate		  = x.EventDate	   
			From	( Select IsNull(pd.AnnuitantPolicyNumber,'N') AnnnuitantPolicyNumber
						,IsNull(i.IssuerName, 'N') IssuerName
						,case when pe.[EventData] is null then 'N' else dbo.FormatDateTime(pe.[EventData], 'STANDARD') end eventDate
						 from crmprod_01.dbo.P_Deals pd 
								left join proc_ss.dbo.p_issuers i on pd.IssuerID = i.IssuerID
								left join crmprod_01.dbo.p_Events_Table pe on (pd.DealID = pe.dealID  and pe.EventTypeID = 84)
								where pd.DealID =@dealID) x
	DECLARE @Output VARCHAR(8000)
		SET		@Output =	@policyNumber + ' , ' + @IssuerName + ' , ' + @CADate
	RETURN @Output
END

Open in new window

Microsoft SQL Server 2008Microsoft SQL Server.NET Programming

Avatar of undefined
Last Comment
Larry Brister

8/22/2022 - Mon
Imran Javed Zia

Hi,
It seems you are getting null value in some of your field
so use

SET             @Output =       IsNull(@policyNumber, '') + ' , ' + isNull(@IssuerName, '') + ' , ' + IsNull(@CADate, '')
Larry Brister

ASKER
IJZ:
That is correct...but as this is a legal document...the person needs to get the message back on exactly what is missing

So if everything is fine...they get back the Output in my stored procedure

If something all somethings are null...they get back the list so they can go fix what needs to be changed
Ephraim Wangoya



ALTER FUNCTION [dbo].[uf_ucc_structured_WithCA] (@DealID int)

RETURNS VARCHAR(8000)
AS
BEGIN
      Declare @policyNumber varchar(40)
      Declare @IssuerName varchar(100)
      Declare @CADate varchar(30)
      Select      @policyNumber = x.AnnnuitantPolicyNumber,
                  @IssuerName   = x.IssuerName,
                  @CADate              = x.EventDate         
                  From      ( Select IsNull(pd.AnnuitantPolicyNumber,'N') AnnnuitantPolicyNumber
                                    ,IsNull(i.IssuerName, 'N') IssuerName
                                    ,case when pe.[EventData] is null then 'N' else dbo.FormatDateTime(pe.[EventData], 'STANDARD') end eventDate
                                     from crmprod_01.dbo.P_Deals pd
                                                left join proc_ss.dbo.p_issuers i on pd.IssuerID = i.IssuerID
                                                left join crmprod_01.dbo.p_Events_Table pe on (pd.DealID = pe.dealID  and pe.EventTypeID = 84)
                                                where pd.DealID =@dealID) x
      DECLARE @Output VARCHAR(8000)
      SET      @Output = STUFF(ISNULL(@policyNumber, '') + ISNULL(', ' + @IssuerName, '') + ISNULL(', ' + @CADate, ''), 1, 1, '')
      if @Output is not null
        SET @Output = 'Invalid or missing ' + @Output
        else
          SET @Output = ''
      RETURN @Output
END
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
GlobaLevel

as mentioned above I got a NULL value for this...put a step in before the RETURN...
If @Output = NULL
@Output = 'The Select returns a Null value'

RETURN @Output
Ephraim Wangoya


if you just need the list then
ALTER FUNCTION [dbo].[uf_ucc_structured_WithCA] (@DealID int) 

RETURNS VARCHAR(8000)
AS
BEGIN
      Declare @policyNumber varchar(40)
      Declare @IssuerName varchar(100)
      Declare @CADate varchar(30)
      Select      @policyNumber = x.AnnnuitantPolicyNumber,
                  @IssuerName   = x.IssuerName,
                  @CADate              = x.EventDate         
                  From      ( Select IsNull(pd.AnnuitantPolicyNumber,'N') AnnnuitantPolicyNumber
                                    ,IsNull(i.IssuerName, 'N') IssuerName
                                    ,case when pe.[EventData] is null then 'N' else dbo.FormatDateTime(pe.[EventData], 'STANDARD') end eventDate
                                     from crmprod_01.dbo.P_Deals pd 
                                                left join proc_ss.dbo.p_issuers i on pd.IssuerID = i.IssuerID
                                                left join crmprod_01.dbo.p_Events_Table pe on (pd.DealID = pe.dealID  and pe.EventTypeID = 84)
                                                where pd.DealID =@dealID) x
      DECLARE @Output VARCHAR(8000)
      SET      @Output = STUFF(ISNULL(@policyNumber, '') + ISNULL(', ' + @IssuerName, '') + ISNULL(', ' + @CADate, ''), 1, 1, '')
      RETURN @Output
END

Open in new window

Ephraim Wangoya


To compare null value use IS

If @Output IS NULL
@Output = 'The Select returns a Null value'

But use LEN
ALTER FUNCTION [dbo].[uf_ucc_structured_WithCA] (@DealID int) 

RETURNS VARCHAR(8000)
AS
BEGIN
      Declare @policyNumber varchar(40)
      Declare @IssuerName varchar(100)
      Declare @CADate varchar(30)
      Select      @policyNumber = x.AnnnuitantPolicyNumber,
                  @IssuerName   = x.IssuerName,
                  @CADate              = x.EventDate         
                  From      ( Select IsNull(pd.AnnuitantPolicyNumber,'N') AnnnuitantPolicyNumber
                                    ,IsNull(i.IssuerName, 'N') IssuerName
                                    ,case when pe.[EventData] is null then 'N' else dbo.FormatDateTime(pe.[EventData], 'STANDARD') end eventDate
                                     from crmprod_01.dbo.P_Deals pd 
                                                left join proc_ss.dbo.p_issuers i on pd.IssuerID = i.IssuerID
                                                left join crmprod_01.dbo.p_Events_Table pe on (pd.DealID = pe.dealID  and pe.EventTypeID = 84)
                                                where pd.DealID =@dealID) x
      DECLARE @Output VARCHAR(8000)
      SET      @Output = STUFF(ISNULL(@policyNumber, '') + ISNULL(', ' + @IssuerName, '') + ISNULL(', ' + @CADate, ''), 1, 1, '')
      if LEN(@Output) > 0 
        SET @Output = 'Invalid or missing ' + @Output
        else
          SET @Output = 'The Select returns a Null value'
      RETURN @Output
END

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ephraim Wangoya


Sorry, the logic in the last post does not look correct, use #35755508
Larry Brister

ASKER
ewangoya:

Close...but somethings a little backwards...

This is what is now returned when 1 and 3 are null
Invalid or missing , Metropolitan Life Ins Co, N

What I need is Invalid or Policy Number,CADateI
ASKER CERTIFIED SOLUTION
Ephraim Wangoya

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Larry Brister

ASKER
Gteat!  Thanks
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes