0

database table

I want to select id in the database table where allot field have a specific integer value in the string.

example:- In the allot column I want to search value 26 in Comma(,) separated string, here result should be id=72

3 Answers 3

1

Fix your data structure! You should be using a junction/association table with one row per value and per id. That is the SQL way to represent the data. Why is your structure bad?

  • Data should be stored using the appropriate type. Numbers should be stored as numbers, not strings.
  • Columns should contain one value.
  • Databases have great data structures for storing lists of values. The best known one is tables. Strings are not the appropriate data structures.
  • SQL engines have (relatively) poor string processing capabilities.
  • Operations on strings do not (in almost all cases) take advantage of indexes and other engine optimizations.
  • If these are ids, then foreign key relationships should be properly declared.

Sometimes, we are stuck with other people's really, really bad design decisions. In those cases, you can use like:

SELECT p.id
FROM Prospects p
WHERE ',' || allot || ',' like '%,26,%';
Sign up to request clarification or add additional context in comments.

Comments

0

try the commend 'SELECT id FROM (table_name) WHERE allot LIKE '%,26,%' the '%x%' will look for anything with an x in it in the provided column basically if you find something with x give it to me

1 Comment

if I have string 10,15,26 in a field then the result will not be accurate
0

Using the LIKE operator you should be able to solve your requirement. Considering that your table name is Prospects:

SELECT id FROM Prospects
WHERE allot LIKE '%,26,%'

EDIT-1: You can narrow down the search finer by adding additional commas in the query as mentioned here!

EDIT-2: To additionally handle scenarios, you can have the same query with a UNION like this. This is not something that you should be looking to implement, but implement a stored procedure to check these scenarios and handle it in your logic.

SELECT id FROM Prospects
WHERE allot LIKE '%,26,%'

UNION

SELECT id FROM Prospects
WHERE allot LIKE '%,26%'

UNION

SELECT id FROM Prospects
WHERE allot LIKE '%26,%'

Hope this answers your question!

4 Comments

if I have 26 and 126 both values then it effects on the result accuracy
Updated my answer!
if the string is 26,126,261 then?
@Manojkumar, basis on what is provided - you could do the additional scenarios. If you have additional questions, then you may post newer questions!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.