Home » SQL & PL/SQL » SQL & PL/SQL » How to write query for desire output?
How to write query for desire output? [message #611192] Sat, 29 March 2014 04:08 Go to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
I will have to choose only those row who contain single 2 from given column col_id, please write sql qury here:


Col_id
11, 2, 3, 22
5, 6, 12, 22
4, 23, 14, 22
2, 9, 22, 30
8, 18, 19, 2


Output:

Col_id
11, 2, 3, 22
2, 9, 22, 30
8, 18, 19, 2

[Updated on: Sat, 29 March 2014 04:09]

Report message to a moderator

Re: How to write query for desire output? [message #611193 is a reply to message #611192] Sat, 29 March 2014 04:40 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
It seems to me that it is time for you to start reading Oracle® Database SQL Language Reference

In addition why do you keep refusing to provide a working test case?
Re: How to write query for desire output? [message #611195 is a reply to message #611192] Sat, 29 March 2014 06:40 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

It also depends on your Oracle version AND actual possible data.
Have a look at SUBSTR, INSTR, REGEXP_SUSTR, LIKE, REGEXP_LIKE.

Re: How to write query for desire output? [message #611688 is a reply to message #611195] Mon, 07 April 2014 03:51 Go to previous messageGo to next message
gauravgautam135
Messages: 33
Registered: December 2013
Member
Hi Michel,

I think we can do this by simple LIKE statement.
Please see below scenarios and let me know if there is any problem with it.

Assumption:
The input data obtained from column will be in similar fashion as mentioned by OP and is string by type.

Possible Scenarios:
1. '2' coming at start i.e. the column will always start with '2,'.
2. '2' coming in middle i.e. the column will always have ', 2,' in it.
3. '2' coming in end i.e. the column will always ends with ' ,2'.

Since we need to find out those rows which have '2' as a separate value.


SQL> WITH tab1 AS
  2  (SELECT '1, 2, 3, 22' col FROM dual
  3   UNION ALL
  4   SELECT '5, 6, 12, 22' FROM dual
  5   UNION ALL
  6   SELECT '4, 23, 14, 22' FROM dual
  7   UNION ALL
  8   SELECT '2, 9, 22, 30' FROM dual
  9   UNION ALL
 10   SELECT '8, 18, 19, 2' FROM dual)
 11   SELECT * FROM tab1 WHERE col LIKE '%, 2,%' OR col LIKE '2,%' OR col LIKE '%, 2';

COL
-------------
1, 2, 3, 22
2, 9, 22, 30
8, 18, 19, 2

SQL> 


Re: How to write query for desire output? [message #611689 is a reply to message #611688] Mon, 07 April 2014 03:58 Go to previous message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You assume 2 is ALWAYS preceding by a space if it is not the fist number.
As I said:

Quote:
It also depends on your Oracle version AND actual possible data.


Previous Topic: mutually exclusive query
Next Topic: Manipulating XML Clob
Goto Forum:
  


Current Time: Tue May 07 12:12:52 CDT 2024