learned/learning/to learn RSS 2.0
# Friday, 01 May 2009

This works in Oracle, hope there is something similar to this in Sql Server too! There might be some other good way to do this, if so, please let me know.

SELECT * FROM TABLE1 WHERE COLUMN1 IN (‘ABC’,’XYZ’,’PQR’) 

Say suppose the strings ‘ABC’,’XYZ’ and ’PQR’ need to be grouped as a single string in the frontend and to be passed as a parameter.  How can we do it? We cannot write like this –

SELECT * FROM TABLE1 WHERE COLUMN1 IN (IN_PARAMTER)

There are many ways to do it:

  1. We can write an inline query.
  2. We can split the strings and group it as a table type.
  3. We can write dynamic sql.

There is one more way to do it and found it to be easy, so thought of sharing.

Here it is:

SELECT * FROM TABLE1 WHERE COLUMN1 IN
(          
 SELECT  REGEXP_SUBSTR(In_Parameter ,'[^,]+',1,LEVEL)        
 FROM    DUAL
 CONNECT BY LEVEL <= LENGTH (REGEXP_REPLACE (In_Parameter, '[^,]')) + 1
)
 

It is enough if we pass the parameter as ‘ABC,XYZ,PQR’ instead of “‘ABC’,’XYZ’,’PQR’”.

More about Oracle Regular Expressions - http://download-west.oracle.com/docs/cd/B13789_01/server.101/b10759/functions116.htm

Friday, 01 May 2009 18:33:29 (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
Oracle
Navigation
Archive
<2009 May>
SunMonTueWedThuFriSat
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2017
Gokulnath
Sign In
Statistics
Total Posts: 41
This Year: 0
This Month: 0
This Week: 0
Comments: 47
Themes
Pick a theme:
All Content © 2017, Gokulnath
DasBlog theme 'Business' created by Christoph De Baene (delarou)