473,465 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Scalar function returns Null

1,271 Recognized Expert Top Contributor
I cannot figure out why this function returns Null for every customer. Even when I run a query based on the credit card file, which means every row has something to be returned by this function.

The function should read all the rows in table x and concatenate the CC last 4 digits along with expiration info. So valid return values could look like this:
Visa 1234: 01 2014
Visa 2345: 02 2014 MCRD 1234 02 2014
....

I've played with the code. Sometimes it gets results and sometimes it doesn't and I haven't been smart enough to see what I did differently when it worked. I think it works sometimes when I return a value from the first query but it never works when I let go into the loop where it should concatenate info from multiple CCs.

Thanks for you time.
Expand|Select|Wrap|Line Numbers
  1. USE [MAS_SPC]
  2. GO
  3. /****** Object:  UserDefinedFunction [dbo].[DealerCreditCards]    Script Date: 04/16/2014 08:41:45 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. -- =============================================
  9. -- Author:        Jim Wolf
  10. -- Create date: 2/4/13
  11. -- Description:    Returns concatenated list of brands for each vendor/productline combination
  12. -- =============================================
  13. ALTER FUNCTION  [dbo].[DealerCreditCards] 
  14. (
  15.     -- Add the parameters for the function here
  16.     @DealerNo varchar(7))
  17. RETURNS  NVARCHAR(100) 
  18. AS
  19. BEGIN
  20.  
  21.     -- Declare the return variable here
  22.     DECLARE @ResultVar as nvarchar(100)
  23.     declare @CC as nvarchar(50)
  24.     declare @PriorCC as nvarchar(50)
  25.     Declare @RowNumber as Integer
  26.     declare @Row_Counter as nvarchar(3)
  27.  
  28.     Set @PriorCC = ''
  29.     Set @CC = ''
  30.     Select TOP 1 @CC=PaymentType + ' '+ Last4UnencryptedCreditCardNos + ': ' + ExpirationDateMonth + ' ' +ExpirationDateYear 
  31.     FROM dbo.AR_CustomerCreditCard with (nolock)
  32.     WHERE    (CustomerNo=@DealerNo )
  33.     Order by PaymentType + ' '+ Last4UnencryptedCreditCardNos + ': ' + ExpirationDateMonth + ' ' +ExpirationDateYear 
  34.  
  35.         Select @Row_Counter=@@ROWCOUNT
  36. --        SET @ResultVar =  @CC + '  '
  37.  
  38.         --    Select @ResultVar = @ResultVar + @CC + '  '
  39.             Select @PriorCC = @CC
  40. --SET @ResultVar =  @ResultVar  + @CC + '  '
  41. --RETURN @ResultVar -- temporary to see if I can get anything back
  42. WHILE @Row_Counter > 0
  43. BEGIN
  44.             SET @ResultVar =  @ResultVar  + @CC + '  '
  45.             SET @PriorCC = @CC
  46.     Select TOP 1 @CC=(PaymentType + ' '+ Last4UnencryptedCreditCardNos + ': ' + ExpirationDateMonth + ' ' +ExpirationDateYear) 
  47.     FROM dbo.AR_CustomerCreditCard with (nolock)
  48.     WHERE     CustomerNo=@DealerNo and (PaymentType + ' '+ Last4UnencryptedCreditCardNos + ': ' + ExpirationDateMonth + ' ' +ExpirationDateYear) >@PriorCC
  49.     Order by PaymentType + ' '+ Last4UnencryptedCreditCardNos + ': ' + ExpirationDateMonth + ' ' +ExpirationDateYear 
  50.     Select @Row_Counter=@@ROWCOUNT
  51. CONTINUE
  52. END
  53.        RETURN @ResultVar 
  54.  
  55. END
  56.  
Jim
Apr 16 '14 #1

✓ answered by Rabbit

If any of those fields are null, the result will be null. You should coalesce the nulls to a blank string before trying to append them.
Expand|Select|Wrap|Line Numbers
  1. '1234' + null = null

3 1659
Rabbit
12,516 Recognized Expert Moderator MVP
If any of those fields are null, the result will be null. You should coalesce the nulls to a blank string before trying to append them.
Expand|Select|Wrap|Line Numbers
  1. '1234' + null = null
Apr 16 '14 #2
jimatqsi
1,271 Recognized Expert Top Contributor
Rabbit,
Bingo! Thank you so much. I had only to add initialization of the return variable and it runs like a champ.

I do so little SQL programming it feels like pulling teeth when I do it. But it feels so good to see it work. Thanks again.

Jim
Apr 16 '14 #3
Rabbit
12,516 Recognized Expert Moderator MVP
No problem, good luck on the rest of your project.
Apr 16 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jeff Adams | last post by:
I am using MSVC .NET to create a C program. I am having trouble creating a window. The createwindow returns NULL however no error is caught. The GetLastError() returns "operation completed...
6
by: Michael Vanhoutte | last post by:
We have an ASP.NET website written in C# that accesses a VB6 COM component. That component accesses the ASP-objects in the following manner: Set objContext = GetObjectContext Set objRequest =...
11
by: MLH | last post by:
I have 2 lines in a procedure that assign MyVariant a value - line #238 and line #491. When line #238 runs, the value is 152. When line #491 runs, the DLookup function returns Null. I would expect...
1
by: js | last post by:
I am using the following C# code and T-SQL to get result object from a SQL Server database. When my application runs, the ExecuteScalar returns "10/24/2006 2:00:00 PM" if inserting a duplicated...
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
2
by: r3ap3r | last post by:
I am using the following recursive function for an assigment string GetString(char numbers, int curNum){ string returnString = ""; int temp; if (numbers != '\0'){ for (int i = 0; i <=2;...
5
by: Travis | last post by:
I am using a function that returns a const char * that is usually a word, etc. How can I check to see if what it returns is empty? I tried if (function() == "") and (function() == NULL) and...
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, is it possible to call a sql server scalar function from c#? thanks, rodchar
6
by: Peter Michaux | last post by:
Suppose I have implemented a language with garbage collection in C. I have wrapped malloc in my own C function. If malloc returns NULL then I can run the garbage collector and then try malloc...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.