How to select rows where column contained a string - sql-server-2005

I want to find rows where a column contains a particular string.
I have a table that contains a column which contains values separated by commas (,).
How can I find the rows that contain a certain value?

This should give you what you want:
SELECT * FROM yourtable
WHERE yourcolumn LIKE 'value,%'
OR yourcolumn LIKE '%,value,%'
OR yourcolumn LIKE '%,value'

Related

PostgreSQL: return row where any column value like variable

I am trying to have the user search for a value in a SQL table, and the user is returned with any row that contains that value. At the moment, I can make it work such that the code is:
SELECT * FROM table WHERE lower('foo') in (lower('col1'),lower('col2'),etc)
However, I would like it to be able to search every column and return any row LIKE 'foo'. For instance,
SELECT * FROM table WHERE (lower('col1'), lower('col2'), etc) like lower('%foo%')
But that doesn't work.
Any suggestions?
I believe you need to use multiple WHERE clauses instead of grouping them all into one statement. Try this:
SELECT * FROM table
WHERE lower(col1) like lower('%foo%')
OR lower(col2) like lower('%foo%')
OR etc like lower('%foo%')
You can convert the whole row to a string and then use LIKE on the result of that:
select *
from the_table
where lower(the_table::text) like '%foo%';
the_table::text returns all columns of each row as a comma separated list enclosed with parentheses, e.g. (42,Arthur,Dent). So the above is not 100 identical to a LIKE condition applied on each column - but probably does what you want.

Extract alphanumeric value from varchar column

I have a table which contains a column having alphanumeric values which is stored as a string. I have multiple values in that column having values such as F4737, 00Y778, PP0098, XXYYYZ etc.
I want to extract values starting with a series of F and must have numeric values in that row.
Alphanumeric column is the unique column having unique values but the rest of the columns contain duplicate values in my table.
Futhermore, once these values are extracted I would like to pick up the max value from the duplicate row,for eg:
Suppose I have F4737 and F4700 as a unique Alphanumeric row, then F4737 must be extracted from it.
I have written a query like this but the numeric values are not getting extracted from this query:
select max(Alplanumeric)
from Customers
where Alplanumeric '%[F0-9]%
or
select max(Alplanumeric)
from Customers
where Alplanumeric like '%[0-9]%'
and Alplanumeric like 'F%'**
I run the above query but I am only getting the F series if I remove the numeric part from the above query. How do I extract both, the F starting series as well as the numeric values included in that row?
Going out on a limb, you might be looking for a query like this:
SELECT *, substring(alphanumeric, '^F(\d+)')::int AS nr
FROM customers
WHERE alphanumeric ~ '^F\d+'
ORDER BY nr DESC NULLS LAST
, alphanumeric
LIMIT 1;
The WHERE conditions is a regular expression match, the expression is anchored to the start, so it can use an index. Ideally:
CREATE INDEX customers_alphanumeric_pattern_ops_idx ON customers
(alphanumeric text_pattern_ops);
This returns the one row with the highest (extracted) numeric value in alphanumeric among rows starting with 'F' followed by one ore more digits.
About the index:
PostgreSQL LIKE query performance variations
About pattern matching:
Pattern matching with LIKE, SIMILAR TO or regular expressions in PostgreSQL
Ideally, you should store the leading text and the following numeric value in separate columns to make this more efficient. You don't necessarily need more tables like has been suggested.

How to check whether column in table contains any of a set of values?

If I have a set of strings, what SQL statement would determine which of those strings are found in one column of a table?
I would want it to return which strings are found, or Null if none are found.
SELECT DISTINCT Column1 FROM Table1 WHERE Column1 IN ("String1","String2","String3")

Matching the beginning characters in a LIKE clause

Suppose in my like clause for my query, i would like to match all records that begins with the characters 'Harris' in my specified column. But their can also be names such as 'Harrison' that I would also like to match. How would I go about finding this
select *
from MyTable
where somecol LIKE 'Harris%'

Select number of comma delimited list items

I have a column that has comma seperated values and I need to select all rows that have 13 commas. They seperate numbers so I don't need to worry about any strings that contain commas. How would I do this?
alternative to like (I do not like the like, and the above will fail if contains 14 commas or more)
select * from table
where length(replace(your_column, ',', ''))=length(your_column)-13;
for better utilize the index, you should seek to normalize your table
If you're using PostgreSQL, you could also use regular expressions.
However, a better question might be why you have a single column with comma-separated values instead of multiple columns.
If you count a string with 14 commas as having 13 commas, then this will work:
SELECT * FROM table WHERE column LIKE '%,%,%,%,%,%,%,%,%,%,%,%,%,%'
% matches any string (including zero length).
In PostgreSQL you can do:
select col from table where length(regexp_replace(col, '[^,]', '', 'g')) = 13;