12 Replies - 13655 Views - Last Post: 23 December 2004 - 01:51 PM Rate Topic: -----

#1 philiprakusen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-December 04

Removing Nulls From A Valuelist

Posted 21 December 2004 - 08:12 AM

I have a simple query that returns a list of IDs that are in the GetProducts query:

SELECT company_id 
FROM companies
WHERE company_id IN (#ValueList(GetProducts.company_id)#)



The problem is the list of company ids in the GetProducts query are sometimes NULL and the above query results in the error: Incorrect syntax near ','.

The SQL looks like this:

SELECT company_id 
FROM companies 
WHERE company_id IN (302,307,235,,514,154,,421)



I have tried writing an IF statement to compile the list if the company_id is not NULL but have run into problems with commas – especially if the last record is NULL.

Does anyone know how to get the second query to ignore the NULL records or a sound alternative?

Is This A Good Question/Topic? 0
  • +

Replies To: Removing Nulls From A Valuelist

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,283
  • Joined: 17-March 01

Re: Removing Nulls From A Valuelist

Posted 21 December 2004 - 08:15 AM

Can you use a sub query instead of pulling the value list from another query? This would be more efficient and eliminate your problem...
Was This Post Helpful? 0
  • +
  • -

#3 philiprakusen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-December 04

Re: Removing Nulls From A Valuelist

Posted 21 December 2004 - 08:56 AM

skyhawk133, on Dec 21 2004, 08:15 AM, said:

Can you use a sub query instead of pulling the value list from another query? This would be more efficient and eliminate your problem...

The GetProducts query is HUGE (!) so was hoping not to have to replicate it as a sub query.

Thanks for your suggestion.
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,283
  • Joined: 17-March 01

Re: Removing Nulls From A Valuelist

Posted 21 December 2004 - 10:12 AM

This may defeat the purpose since you said your query is huge, however, I just tried this on a similar situation and it works... here is just the debug output:

<!--- Original Query --->
<cfquery datasource="staff" name="list">
SELECT TOP 25 * FROM staff
</cfquery>

<!--- New query to eliminate null values for the valuelist --->
<cfquery name="newlist" dbtype="query">
SELECT nickname FROM list WHERE nickname IS NOT NULL AND nickname != ''
</cfquery>

<cfdump var="#list#">
<cfdump var="#newlist#">

<cfdump var="#valuelist(list.nickname)#">
<cfdump var="#valuelist(newlist.nickname)#">


In the first list, it outputs about 10 null values and empty strings, in the newlist (after the query-of-query) the value list is stripped of all the null values.

The above method may be just as, if not more, overhead than doing a subquery since you are putting all the work on the coldfusion server. The nice thing about SQL is... it's designed to aggregate data so doing a subquery is it's job whereas using ColdFusion is a secondary process. It's best to let the SQL server do all the work you can before bringing the data back.
Was This Post Helpful? 0
  • +
  • -

#5 philiprakusen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-December 04

Re: Removing Nulls From A Valuelist

Posted 21 December 2004 - 10:47 AM

OK - New idea:

Any idea how to test for a NULL value in a list, e.g. my_list = '1,2,3,,5,6,,8,9'?

<cfif ListFind(my_list, '')>
  ...
</cfif>


Unfortunately, the above doesn't work.
Was This Post Helpful? 0
  • +
  • -

#6 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,283
  • Joined: 17-March 01

Re: Removing Nulls From A Valuelist

Posted 21 December 2004 - 11:11 AM

You could search for the double commas in the string?! You could also loop through the list and only echo if the value is NEQ to "". List find won't work because it only returns the first instance of the search, so if you have 5 nulls it will return the first position of the first null.
Was This Post Helpful? 0
  • +
  • -

#7 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4503
  • View blog
  • Posts: 28,411
  • Joined: 21-March 01

Re: Removing Nulls From A Valuelist

Posted 21 December 2004 - 11:16 AM

loop thru the list assining all non-NULL numbers to a new list? then use that list in your valuelist function for the sql?
Was This Post Helpful? 0
  • +
  • -

#8 SpongeC  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-December 04

Re: Removing Nulls From A Valuelist

Posted 21 December 2004 - 03:15 PM

philiprakusen, on Dec 21 2004, 10:47 AM, said:

OK - New idea:

Any idea how to test for a NULL value in a list, e.g. my_list = '1,2,3,,5,6,,8,9'?

<cfif ListFind(my_list, '')>
  ...
</cfif>


Unfortunately, the above doesn't work.

Well, i am not quite sure on what you are trying to do. Do you want to place the NULL value between the ',,' or remove the empty list elements?

Anyway, whatever you are trying to do you can use the following function:

<cffunction name="ReplaceListBlanks" returntype="string">
	<cfargument name="lInput" required="yes" type="string">
	<cfargument name="bReplaceWithNull" required="no" type="boolean" default="true">
	
	<!--- Change the format of the list from '1,2,3,,5,6,,8,9' to ',1,2,3,,5,6,,8,9,' --->
	<cfset var lOutput = "," & arguments.lInput & ",">
	
	<cfif arguments.bReplaceWithNull>
  <!--- Replace all occurences of ',,' with ',NULL,' --->
  <cfset lOutput=REReplace(lOutput, ",,", ",NULL,", "ALL")>
	<cfelse>
  <!--- Replace all occurences of ',,' with ',' --->
  <cfset lOutput=REReplace(lOutput, ",,", ",", "ALL")>
	</cfif>
	
	<!--- Remove the first and last comma and make the string a valid list again --->
	<cfset lOutput = Mid(lOutput, 2, len(lOutput)-2)>
	
	<cfreturn lOutput>
</cffunction>



Use it as :

<cfdump var="#ReplaceListBlanks('1,2,3,,5,6,,8,9')#"><br>
<cfdump var="#ReplaceListBlanks('1,2,3,,5,6,,8,9', false)#">



... and the result will be:

Quote

1,2,3,NULL,5,6,NULL,8,9
1,2,3,5,6,8,9

Was This Post Helpful? 0
  • +
  • -

#9 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,283
  • Joined: 17-March 01

Re: Removing Nulls From A Valuelist

Posted 21 December 2004 - 03:35 PM

Whoa damn, bustin out with the function, very nice :) Yes, SpongeC's way should work for ANY list no matter if it's in a query or otherwise.
Was This Post Helpful? 0
  • +
  • -

#10 shiroh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-December 04

Re: Removing Nulls From A Valuelist

Posted 23 December 2004 - 06:49 AM

What database are you using? If the db supports subqueries why not use a subquery instead of using the valuelist function.

However, a funky little work around is
#arrayToList(listToArray(valueList(Get_Products.company_id)))#


ColdFusion does not recognize empty strings (nulls) in lists, so if you convert the list to an array it will automatically remove the empty list elements, then all you do is convert the array back to the list. SpongeC's solution was quite nifty though!

HTH
Was This Post Helpful? 0
  • +
  • -

#11 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,283
  • Joined: 17-March 01

Re: Removing Nulls From A Valuelist

Posted 23 December 2004 - 09:50 AM

That's what I was going to do but figured the subquery or even query of query would be faster.

I agree, the subquery would be best though.
Was This Post Helpful? 0
  • +
  • -

#12 shiroh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-December 04

Re: Removing Nulls From A Valuelist

Posted 23 December 2004 - 01:14 PM

I am such an idiot, I need to get more sleep sometimes! The next best thing to a subquery would be using the CFQUERYPARAM tag.

SELECT company_id
FROM companies
WHERE company_id IN (<cfqueryparam cfsqltype="CF_SQL_INTEGER" list="Yes" separator="," value="#ValueList(GetProducts.company_id)#" />)


And that's the end of that, it will solve all your problems! A handy tip is to use the CFQUERYPARAM tag as much as possible since it allows for faster query execution.

HTH
Was This Post Helpful? 0
  • +
  • -

#13 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,283
  • Joined: 17-March 01

Re: Removing Nulls From A Valuelist

Posted 23 December 2004 - 01:51 PM

I did NOT know there was a list parameter within query param, now THAT is cool.

Very nice shiroh! Good call!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1