REGEXP_SUBSTR for comma delimited list with null values
Hi,
I have a column which stores a comma delimited list of values. Some of these values in the list may be null. I'm having some issues trying to extract the values using the REGEXP_SUBSTR function when null values are present. Here are two things that I've tried:
SELECT
REGEXP_SUBSTR (val, '[^,]*', 1, 1) pos1
,REGEXP_SUBSTR (val, '[^,]*', 1, 2) pos2
,REGEXP_SUBSTR (val, '[^,]*', 1, 3) pos3
,REGEXP_SUBSTR (val, '[^,]*', 1, 4) pos4
,REGEXP_SUBSTR (val, '[^,]*', 1, 5) pos5
FROM (SELECT 'AAA,BBB,,DDD,,FFF' val FROM dual);
POS P POS P P
AAA BBB
SELECT
REGEXP_SUBSTR (val, '[^,]+', 1, 1) pos1
,REGEXP_SUBSTR (val, '[^,]+', 1, 2) pos2
,REGEXP_SUBSTR (val, '[^,]+', 1, 3) pos3
,REGEXP_SUBSTR (val, '[^,]+', 1, 4) pos4
,REGEXP_SUBSTR (val, '[^,]+', 1, 5) pos5
FROM (SELECT 'AAA,BBB,,DDD,,FFF' val FROM dual);
POS POS POS POS P
AAA BBB DDD FFFAs you can see neither of the calls works correctly. Does anyone know how to modify the regular expression pattern to handle null values? I've tried various other patterns but was unable to get anyone to work for all cases.
Thanks,
Martin
http://www.ClariFit.com
http://www.TalkApex.com
Hi, Martin,
This does what you want:
SELECT
RTRIM (REGEXP_SUBSTR (val, '[^,]*,', 1, 1), ',') pos1
,RTRIM (REGEXP_SUBSTR (val, '[^,]*,', 1, 2), ',') pos2
,RTRIM (REGEXP_SUBSTR (val, '[^,]*,', 1, 3), ',') pos3
,RTRIM (REGEXP_SUBSTR (val, '[^,]*,', 1, 4), ',') pos4
,RTRIM (REGEXP_SUBSTR (val || ','
, '[^,]*,', 1, 5), ',') pos5
FROM (SELECT 'AAA,BBB,,DDD,,FFF' val FROM dual);The query above works in Oracle 10 or 11, but in Oracle 11, you can also do it with just REGEXP_SUBSTR, without using RTRIM:
SELECT
REGEXP_SUBSTR (val, '([^,]*),|$', 1, 1, NULL, 1) pos1
,REGEXP_SUBSTR (val, '([^,]*),|$', 1, 2, NULL, 1) pos2
,REGEXP_SUBSTR (val, '([^,]*),|$', 1, 3, NULL, 1) pos3
,REGEXP_SUBSTR (val, '([^,]*),|$', 1, 4, NULL, 1) pos4
,REGEXP_SUBSTR (val, '([^,]*),|$', 1, 5, NULL, 1) pos5
FROM (SELECT 'AAA,BBB,,DDD,,FFF' val FROM dual);The problem with your first query was that it was looking for sub-strings of 0 or more non-commas. There was such as sub-string. consisting of 3 characters, starting at position 1, so it returned 'AAA', as expected. Then there was another sub-string, of 0 characters, starting at position 4, so it returned NULL. Then there was a sub-string of 3 characters starting at position 5, so it returned 'BBB'.
The problem with your 2nd query was that it was looking for 1 or more non-commas. 'DDD' is the 3rd such sub-string.
Edited by: Frank Kulash on Feb 16, 2012 11:36 AM
Added Oracle 11 example
Similar Messages
-
Is it possible to convert a table of values into a comma-delimited-list?
Hi,
I'd like to turn the following dataset:
Parent | Child
Charles | William
Charles | Harry
Anne | Peter
Anne | Zara
Andrew | Beatrice
Andrew | Eugenie
into this:
Parent | Children
Charles | Diana,Camilla
Anne | Peter,Zara
Andrew | Beatrice,Eugenie
In other words, I'd like to take a list of values pertaining to some key and produce them as a comma-delimited-list.
I know its is possible in T-SQL although the method is a bit of a nasty hack. Is it possible in PL-SQL?
Thaks in advance
JamieHi,
With model clause (10g)
with t as(
select 'Charles' parent, 'William' child from dual union
select 'Charles', 'Harry' from dual union
select 'Anne', 'Peter' from dual union
select 'Anne', 'Zara' from dual union
select 'Andrew', 'Beatrice' from dual union
select 'Andrew', 'Eugenie' from dual
select parent,substr(res,2) res
from t
model
return updated rows
partition by ( parent)
dimension by ( row_number()over(partition by parent order by child) rn)
measures(child, cast( null as varchar2(4000)) as res)
rules
iterate(100000)
until(presentv(res[iteration_number+2],1,0)=0)
( res[0]=res[0]||','||child[iteration_number+1]);
PARENT RES
Anne Peter,Zara
Andrew Beatrice,Eugenie
Charles Harry,William
3 rows selected. -
Hi,
One my procedure's parameters is a PL/SQL table of id's (NUMBER) that the GUI sends it, based on the user's selections.
Now, I want to use the table's values in my select's where clause to return only the correct records.
However,
select ......
from ....
where id in (i_array_ids)
doesn't work of course, and my attempts of transfering the ids to a comma delimited list of numbers (as opposed to a lenghty varchar2 string) that could work between the ( ) have all failed.
ThanksAnd here's an example I gave with some more up-to-date syntax than in that old AskTom thread:
Re: DYNAMIC WHERE CLAUSE in PROCEDURE -
Validating comma-delimited list for numeric entries
Hi!
I need to validate a comma-delimited list to make sure all the list items are numeric. I could do a cfloop to loop through the list, then an IsValid() to check each entry, but that seems cumbersome, so I was wondering if there was a better way.
Thanks!BreakawayPaul wrote:
I had the idea to just do a replace() to get rid of all the commas and treat the value as one big number. It seems to work.
True. That is a creative test. But what if the list begins with 0 or contains negative or decimal numbers?
If you must include those such eventualities, then you could extend the test to something like
<cftry>
<cfset myList="0,1,2,x">
<cfset maxNo=arrayMax(listToArray(myList))>
<!--- The rest of the business code goes here --->
<cfcatch type="expression">
<cfoutput>#cfcatch.Detail#</cfoutput>
</cfcatch>
</cftry> -
Revision: 14016
Revision: 14016
Author: [email protected]
Date: 2010-02-05 16:12:41 -0800 (Fri, 05 Feb 2010)
Log Message:
Fix for List RTE when dropping into list with null dataProvider. If the dataProvider is null, we create an ArrayCollection to hold items being dropped in the List.
QE notes: No
Doc notes: No
Bugs: SDK-25218
Reviewer: Jason
Tests run: checkintests
Is noteworthy for integration: No
Ticket Links:
http://bugs.adobe.com/jira/browse/SDK-25218
Modified Paths:
flex/sdk/trunk/frameworks/projects/spark/src/spark/components/List.asRevision: 14016
Revision: 14016
Author: [email protected]
Date: 2010-02-05 16:12:41 -0800 (Fri, 05 Feb 2010)
Log Message:
Fix for List RTE when dropping into list with null dataProvider. If the dataProvider is null, we create an ArrayCollection to hold items being dropped in the List.
QE notes: No
Doc notes: No
Bugs: SDK-25218
Reviewer: Jason
Tests run: checkintests
Is noteworthy for integration: No
Ticket Links:
http://bugs.adobe.com/jira/browse/SDK-25218
Modified Paths:
flex/sdk/trunk/frameworks/projects/spark/src/spark/components/List.as -
Pro*C & SQLDA with NULL value for predicate column
Hi: I am using a C program to update a table via a dynamic sql (method 4) and SQLDA. In the update statement predicate, I have place holders (as in TBLCOL=:C000). One of the columns in the predicate contains null values, so I set L[n] = 0, V[n] = pData (which pData[0] = '\0'), *(I[n]) = -1, and T[n] = 5 (for text). I cannot find the row that I know is there.
I cannot change my statement to contain TBLCOL IS NULL, since I don't know ahead of time if I'm looking for rows with null values for this column. The Pro*C manual says that by setting the appropriate *(I[n]) = -1, it indicates to Oracle to simulate the "IS NULL" clause, and update the appropriate rows. In my case, I receive 1403 as SQLCODE when I use TBLCOL=:C000 vs TBLCOL IS NULL. What am I doing wrong? Thank you for your help.You should include these columns as well;
ChangeType (see mxi_changetype)
ValOwner (repository)
UserID ("jobid=<>", usermskey, GUI (mmc), DG (dyngrp), reconcile)
IdAudit (This is the event task (add and del member for assignments)
ParentAuditId (AuditID of parent which last updated the attribute, not consistent)
ChangedBy (Holds the MSKEY of the user which last changed the attribute)
ExpiryTime
to make sure you get a fuller picture of the audit record.
Your selection does not cover all events and descriptions
br,
Chris -
Suppliers is a field containing a comma delimited list of
Supplier ID's.
When a supplier logs in they should be able to view all the
auctions that they have been registered for
i.e if their supplierID is in the suppliers field.
have tried this and get an error:
<CFQUERY NAME="GetAuctions"
DATASOURCE="#Application.Datasource#">
SELECT * FROM Auctions
WHERE '#Session.SupplierID#' IN 'Auctions.Suppliers'
</CFQUERY>
have tried this and recordcount is 0 when it should be 3:
<CFQUERY NAME="GetAuctions"
DATASOURCE="#Application.Datasource#">
SELECT * FROM Auctions
WHERE '#Session.SupplierID#' LIKE 'Auctions.Suppliers'
</CFQUERY>You should avoid having a list value in a field and normalise
your table. But if you want to stick with your style(which is not
advisable), maybe you can do this. I believe your supplier id is a
string so the code below may cause slowness in your system:
<CFQUERY NAME="GetAuctions1"
DATASOURCE="#Application.Datasource#">
SELECT Suppliers FROM Auctions
</CFQUERY>
<cfoutput query="GetAuctions1">
<CFQUERY NAME="GetAuctions2"
DATASOURCE="#Application.Datasource#">
SELECT * FROM Auctions
WHERE '#Session.SupplierID#' IN(<cfqueryparam
values="#Suppliers#" cfsqltype="CF_SQL_VARCHAR" list="Yes">)
</CFQUERY>
</cfoutput>
But if your supplier id is a numeric value. then you can do
this:
<CFQUERY NAME="GetAuctions"
DATASOURCE="#Application.Datasource#">
SELECT A1.* FROM Auctions A1
WHERE #Session.SupplierID# IN(SELECT A2.Suppliers FROM
Auctions A2 WHERE A2.your_primary_key_for_table_Auctions =
A1.your_primary_key_for_table_Auctions)
</CFQUERY> -
Too many commas in my comma delimited list
I'm trying to merge several pdf files into one by using the cfpdf tag (action="merge"). In the source attribute, you can enter a comma delimited list of file paths to merge the pdf files together. I'm thinking that Adobe could have picked a better delimiter though because it breaks if there is a comma any one of the file names. I've tried using replace() to replace the commas in my filenames with chr(44) before passing it to the cfpdf tag, but it still breaks. Any ideas on how to accommodate this? I'm trying to prevent having to copy hundreds of files to a temp directory, then use the directory attribute instead, then delete the temp directory. That just seems like such a waste of resources...
Thanks!cfpdfparam is a WIN! Thanks for the heads up, i had no idea that you could use the tag like that.
<cfpdf
action = "merge"
destination = "C:\Inetpub\mydir\secure\test\output_merge.pdf"
overwrite = "yes">
<cfpdfparam source = "C:\Inetpub\mydir\secure\test\0003. A-001 - Restaurant, Floor Plan.pdf">
<cfpdfparam source = "#expandPath('/secure/test/0001. G-001 - General.pdf')#">
<cfpdfparam source = "#expandPath('/secure/test/0002. G-101 - General Information.pdf')#"></cfpdf>
Works Great, Thanks! -
Need help with NULL values in Crosstables
Hello everybody,
I need some help with NULL values and crosstables. My issue is the following:
I have a query (BW - MDX-Query) that gives me turnover measures for each month. In Crystal Reports I choose crosstable to display this whereby I put all month in columns and all turnover-measures in rows. Each month that has a value (measures are not empty) is chown in the crosstables rows. So far so good. The problem occures when there are month that actually have no values (measures are empty). In that case these months are not chown in columns. But I need CR to display these columns and show the value 0. How can I do that?Hi Frank,
Cross tab shows the data based on your column and these column fields are grouped and based on the group it will show your summaries.
If there is no data for any of your group it will not display that group. In this case you will have to create a standard report which should look like cross tab and to get zero values you need to write formulas .
Example if you want to display Moth wise sales :
if Month() = 01 Then
sum() else 0
Now this formula will check if your month is Jan, then it will sum up the values else it will display zero value.
This is possible only through standard report not with Cross Tab.
Thanks,
Sastry -
How to create a dimension object in SAP BW Unv with Null value
Hello,
for query syncronisation task, I need a dimension object in my BW universe with NULL value. Does someone know, how to do it or if its possible?
Regards,
ThiloHi Thilo,
so the only common item is Material then - correct ?
Query 1 Query 2
Formulation (grouping of materials) (No counterpart)
Material Material
dummy key figure (why?) Company Code
10 Keyfigure
Is this something you regular have to do ? what about a MultiProvider ?
did you try "merged dimensions" in the Web Intelligence Report ?
Ingo -
Sql query slowness due to rank and columns with null values:
Sql query slowness due to rank and columns with null values:
I have the following table in database with around 10 millions records:
Declaration:
create table PropertyOwners (
[Key] int not null primary key,
PropertyKey int not null,
BoughtDate DateTime,
OwnerKey int null,
GroupKey int null
go
[Key] is primary key and combination of PropertyKey, BoughtDate, OwnerKey and GroupKey is unique.
With the following index:
CREATE NONCLUSTERED INDEX [IX_PropertyOwners] ON [dbo].[PropertyOwners]
[PropertyKey] ASC,
[BoughtDate] DESC,
[OwnerKey] DESC,
[GroupKey] DESC
go
Description of the case:
For single BoughtDate one property can belong to multiple owners or single group, for single record there can either be OwnerKey or GroupKey but not both so one of them will be null for each record. I am trying to retrieve the data from the table using
following query for the OwnerKey. If there are same property rows for owners and group at the same time than the rows having OwnerKey with be preferred, that is why I am using "OwnerKey desc" in Rank function.
declare @ownerKey int = 40000
select PropertyKey, BoughtDate, OwnerKey, GroupKey
from (
select PropertyKey, BoughtDate, OwnerKey, GroupKey,
RANK() over (partition by PropertyKey order by BoughtDate desc, OwnerKey desc, GroupKey desc) as [Rank]
from PropertyOwners
) as result
where result.[Rank]=1 and result.[OwnerKey]=@ownerKey
It is taking 2-3 seconds to get the records which is too slow, similar time it is taking as I try to get the records using the GroupKey. But when I tried to get the records for the PropertyKey with the same query, it is executing in 10 milliseconds.
May be the slowness is due to as OwnerKey/GroupKey in the table can be null and sql server in unable to index it. I have also tried to use the Indexed view to pre ranked them but I can't use it in my query as Rank function is not supported in indexed
view.
Please note this table is updated once a day and using Sql Server 2008 R2. Any help will be greatly appreciated.create table #result (PropertyKey int not null, BoughtDate datetime, OwnerKey int null, GroupKey int null, [Rank] int not null)Create index idx ON #result(OwnerKey ,rnk)
insert into #result(PropertyKey, BoughtDate, OwnerKey, GroupKey, [Rank])
select PropertyKey, BoughtDate, OwnerKey, GroupKey,
RANK() over (partition by PropertyKey order by BoughtDate desc, OwnerKey desc, GroupKey desc) as [Rank]
from PropertyOwners
go
declare @ownerKey int = 1
select PropertyKey, BoughtDate, OwnerKey, GroupKey
from #result as result
where result.[Rank]=1
and result.[OwnerKey]=@ownerKey
go
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
JDBC Receiver Adapter with Null Value
HI,
I have configured ID for JDBC Receiver. In my communication channel, I already check Integration of Empty String Values = Null Value. However, when I check the result from audit log, it still shows that SAP XI sends the null value to database. In my understanding, it should not send the field which has null value (It shouldn't be included in sql statement).
I check this with other scenario. With the same check at Integration of Empty String Values = Null Value, it doesn't send null value to database. It happens only with my first scenario.
Have anyone ever been through this before? Any suggestion please?
Thanks,
PavinHi,
1. The occurrence is 0...1
2. This is the first result with null value (Please see field Error)
UPDATE EXPCRM_T_CustomerProfile SET RequestID=455, RecordNo=1, SAPCustomerCode=0001000344, Error=NULL, Status=2, UpdateDateTime=12/03/2008 13:45:03 WHERE (RequestID=455 AND RecordNo=1)
Then, I change the option from Null Value to Empty string. This is the result.
UPDATE EXPCRM_T_CustomerProfile SET RequestID=455, RecordNo=1, SAPCustomerCode=0001000344, Error=', Status=2, UpdateDateTime=12/03/2008 13:46:12 WHERE (RequestID=455 AND RecordNo=1)
Field Error Change from NULL to '
The expected result from me is that field Error should not exist at all. Please help.
Thanks,
Pavin -
bc4juix:StyledText doesn't work with null values?
Hi , anyone knows why the <bc4juix:StyledText> tag doesn't work with null values?
For example when datasource does not give back data...
Thanks
GuillermoWhich version of Jdev are you using? What are the results you are getting?
-
Range interval partitioning with null value
Hello,
I have question, how can I create table with partition and after that I will insert null value in the partition key like this kind of
range interval partitioning with null value.
I have Oracle 11g.
Thanks
Mohammeduser13777053 wrote:
Thank you, but my question is about range interval not for range?http://docs.oracle.com/cd/E11882_01/server.112/e25523/partition.htm#VLDBG00220
A MAXVALUE literal can be defined for the highest partition. MAXVALUE represents a virtual infinite value that sorts higher than any other possible value for the partitioning key, *including the NULL value.*
So since you're trying to partition by interval (which doesn't support a MAXVALUE clause) you're out of luck.
There's probably fancy hacks and what not you could try to implement, but i'm not about to recommend any of those here :)
Cheers, -
To overcome column with null value-urgent
hai all,
when i query i get column with null value.
how to solve it?
thank in advance.
rcs
SQL> DESC SCOTT.CB1;
Name Null? Type
ID NUMBER
SUPCODE NUMBER
SUPLNAME VARCHAR2(100)
NAME VARCHAR2(100)
ITEMCODE VARCHAR2(10)
RECDOC NUMBER
RECDATE VARCHAR2(10)
TOTVALUE NUMBER
QTY NUMBER
CB_IPNO NUMBER
CB_VNNO NUMBER
CB_VDT VARCHAR2(10)
CB_AMT NUMBER
RECDOC_GR VARCHAR2(30)
RECDATE_GR DATE
SUPCODE_GR VARCHAR2(10)
TABLE LOOK LIKE THIS (NOT ALL DATA IN SAME ROW, BECUSE I INSERTED LAST 3 COLUMN VALUES):
ID SUPCODE SUPLNAME NAME ITEMCODE RECDOC RECDATE TOTVALUE QTY CB_IPNO CB_VNNO CB_VDT CB_AMT RECDOC_GR RECDATE_GR SUPCODE_GR
2015 AAAA 04117 9083 10545.6 78
2016 BBBB 04609 9087 25200 3600
2017 GGGG 04609 9088 28175 4025
2018 36591371.64 2565017.27
00001/07-08 02/04/2007 14020362
00002/07-08 02/04/2007 14020362
00003/07-08 02/04/2007 14010254
00004/07-08 02/04/2007 14010254
00005/07-08 02/04/2007 14021458
SQL> SELECT DISTINCT ID, SUPCODE_GR, NAME, ITEMCODE, RECDOC, RECDATE_GR, TOTVALUE, QTY FROM SCOTT.CB
1;
ID SUPCODE_GR
NAME
ITEMCODE RECDOC RECDATE_G TOTVALUE QTY
1
PRO.AT.ALU.POWDER UNCOATED
04609 15 51975 7425
2
PEN, GEL PEN
07969 17 154 11
ID SUPCODE_GR
I NEED RESULT AS FOLLOWS (ALL RESPECTIVE DDATA IN ONE LINE NOW NOT LIKE THAT):
ID SUPCODE SUPLNAME NAME ITEMCODE RECDOC RECDATE TOTVALUE QTY CB_IPNO CB_VNNO CB_VDT CB_AMT RECDOC_GR RECDATE_GR SUPCODE_GR
2015 AAAA 04117 9083 10545.6 78 00001/07-08 02/04/2007 14020362
============Even accounting for the formatting, I'm not sure I even understand the question. It could be any number of different problems or non-problems.
Maybe you are looking for
-
Order of starting services in Hyperion System 11.1.1 release
Hi All, How important is the order of starting services in 11.1.1 ? I mean does this needs to be done manually every time or does Hyperion takes care of this automatically (including dependencies), if I set this to "automatic" in the services panel?
-
Attach a File to a cell in the BW report...
Hi all, When I run a bw report on the web it is possible attach a text comment to a cell in the result table as a "document". This is an standard function but we need attach also a file in the same way. Please con you help me with this issue? Enrique
-
Hi, I have recently downloaded DW CS3 and as the trial was running out I decided to go for the CS4 beta version. A few days later it's already asking me for a valid serial number and I'm a little puzzled by this. (I guess it's to do with the Cs3 vers
-
ORA-01161: database name
DB 10.2.0.4 AIX 5.2 I am trying to make clone.Even I made clone, the database was up but when I saw in the alert log file it was giving continue below error(1) and the size of alert log file was getting increase so I decided to restore missed file an
-
Hello All, I am facing the problem in COPA customisation, I have to create the characteristic for billing document line item through table VBRP but when i select transfer from SAP table to create then the table is not available in that Table List . h