1

I have code numbers as the following sequential:

1
1.1
1.2
1.3
.
.
.
1.1.1
1.1.2
.
.
.
2

And so..

I want a method to that parses the children based on some input. For example, if the input is 1 then it will retrieve all it's children, and if the input is 1.1 it will retrieve all of it's children, etc.

How can I do something like that?

Notes:

  • This data is stored in one table in Informix database.
  • The code numbers are strings.

4 Answers 4

5

Code

var list = new List<string>();
var children = list.Where(s => s.StartsWith(inputString));

DB

DECLARE @p1 varchar(100)
SET @p1 = '1.1%'
SELECT * FROM tbl WHERE tbl.col LIKE @p1
3
  • Code is supposed to be self explanatory.
    – Ilia G
    Oct 4, 2011 at 17:25
  • ok that's good but what about the database: i wanna some thing general ,if i wanna the childs of 1, or 1.1.1 you talk specially about 1.1! Oct 4, 2011 at 18:03
  • Just set the @p1 variable to whatever you are looking for, '1%' or '1.1.1%'
    – Magnus
    Oct 4, 2011 at 18:05
2
SELECT ...
FROM yourtable
WHERE codestring LIKE '1.1%';

should do the trick.

2
  • i wanna some thing general ,if i wanna the childs of 1, or 1.1.1? Oct 4, 2011 at 18:01
  • 1
    Then insert whatever parent string you're looking for in place of the 1.1. But if you're going to need more complex queries, you'd probably be better off redoing this table as a parent/child relationship.
    – Marc B
    Oct 4, 2011 at 18:02
1

Just to be different - using SUBSTR etc instead of LIKE:

SELECT *
  FROM AnonymousTable
 WHERE SUBSTR(CodeNumbers, 1, LENGTH(<xxx>)) = <xxx>;

Where the <xxx> notation indicates where you place the argument. There are various notations for that, depending on exactly how the SQL is being handled. The one downside is that it requires two references to the same parameter value.

1

Will not a simple sql query solve it??

something like

SELECT * from the_table where column matches "input*"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.