Using WHERE for partial field match - How - sql

Hello this is my first post and I am fairly new to SQL. Really what I am trying to find is an equivilent to MID$ where I can make updates based on part of a field.
UPDATE OurTable
SET UpdateField='Text-2008-001-old'
WHERE '2008' like ('%' || UpdateField) AND KeyField='1'
In other words I wand to update to 'Text-2008-001-old' where the field might currently be 'Line-2008-000-000'
The position of the 2008 does not change within the existing data so really I just need to update and fields that contain 2008 where the KeyField is '1'
If there is a good online resource for SQL syntax that I have not found yet please feel free to point me there.
Hope I have explained this OK and thanks in advance for all suggestions.

I think you just need to use LIKE:
UPDATE OurTable
SET UpdateField='Text-2008-001-old'
WHERE UpdateField LIKE '%2008%' AND KeyField='1'
Or if there are always dashes around it, perhaps LIKE '%-2008-%' would be better.
Good luck.

UPDATE OurTable
SET UpdateField='Text-2008-001-old'
WHERE UpdateField like 'Line-2008%' AND KeyField='1'

There are a number of wildcards to use with the LIKE operator (%,_,[],[^], etc.) -- explained here: http://msdn.microsoft.com/en-us/library/ms179859.aspx.
There is also the substring() function -- explained here: http://msdn.microsoft.com/en-us/library/ms187748.aspx, that is kind of like a MID function in other languages. (WHERE...LIKE SUBSTRING(...))

Related

SQL DB2 - How to SELECT or compare columns based on their name?

Thank you for checking my question out!
I'm trying to write a query for a very specific problem we're having at my workplace and I can't seem to get my head around it.
Short version: I need to be able to target columns by their name, and more specifically by a part of their name that will be consistent throughout all the columns I need to combine or compare.
More details:
We have (for example), 5 different surveys. They have many questions each, but SOME of the questions are part of the same metric, and we need to create a generic field that keeps it. There's more background to the "why" of that, but it's pretty important for us at this point.
We were able to kind of solve this with either COALESCE() or CASE statements but the challenge is that, as more surveys/survey versions continue to grow, our vendor inevitably generates new columns for each survey and its questions.
Take this example, which is what we do currently and works well enough:
CASE
WHEN SURVEY_NAME = 'Service1' THEN SERV1_REC
WHEN SURVEY_NAME = 'Notice1' THEN FNOL1_REC
WHEN SURVEY_NAME = 'Status1' THEN STAT1_REC
WHEN SURVEY_NAME = 'Sales1' THEN SALE1_REC
WHEN SURVEY_NAME = 'Transfer1' THEN Null
ELSE Null
END REC
And also this alternative which works well:
COALESCE(SERV1_REC, FNOL1_REC, STAT1_REC, SALE1_REC) as REC
But as I mentioned, eventually we will have a "SALE2_REC" for example, and we'll need them BOTH on this same statement. I want to create something where having to come into the SQL and make changes isn't needed. Given that the columns will ALWAYS be named "something#_REC" for this specific metric, is there any way to achieve something like:
COALESCE(all columns named LIKE '%_REC') as REC
Bonus! Related, might be another way around this same problem:
Would there also be a way to achieve this?
SELECT (columns named LIKE '%_REC') FROM ...
Thank you very much in advance for all your time and attention.
-Kendall
Table and column information in Db2 are managed in the system catalog. The relevant views are SYSCAT.TABLES and SYSCAT.COLUMNS. You could write:
select colname, tabname from syscat.tables
where colname like some_expression
and syscat.tabname='MYTABLE
Note that the LIKE predicate supports expressions based on a variable or the result of a scalar function. So you could match it against some dynamic input.
Have you considered storing the more complicated properties in JSON or XML values? Db2 supports both and you can query those values with regular SQL statements.

DB2 complex like

I have to write a select statement following the following pattern:
[A-Z][0-9][0-9][0-9][0-9][A-Z][0-9][0-9][0-9][0-9][0-9]
The only thing I'm sure of is that the first A-Z WILL be there. All the rest is optional and the optional part is the problem. I don't really know how I could do that.
Some example data:
B/0765/E 3
B/0765/E3
B/0764/A /02
B/0749/K
B/0768/
B/0784//02
B/0807/
My guess is that I best remove al the white spaces and the / in the data and then execute the select statement. But I'm having some problems writing the like pattern actually.. Anyone that could help me out?
The underlying reason for this is that I'm migrating a database. In the old database the values are just in 1 field but in the new one they are splitted into several fields but I first have to write a "control script" to know what records in the old database are not correct.
Even the following isn't working:
where someColumn LIKE '[a-zA-Z]%';
You can use Regular Expression via xQuery to define this pattern. There are many question in StackOverFlow that talk about patterns in DB2, and they have been solved with Regular Expressions.
DB2: find field value where first character is a lower case letter
Emulate REGEXP like behaviour in SQL

Faceted Search in Coldfusion and SQL?

I'm working on a faceted search in Coldfusion and SQL. I've tried creating a query like this:
SELECT * FROM Turbos
WHERE
PartNumber LIKE '%#trim(SearchCriteria)#%'
OR PartDescription LIKE '%#trim(SearchCriteria)#%'
AND (PumpingSpeed BETWEEN #minimum# AND #URL.speed#)
AND InletFlange LIKE '#URL.inlet#'
AND Bearing LIKE '#URL.bearing#'
AND Condition LIKE '#URL.condition#'
The problem is the server is returning rows that don't contain EVERY piece of data I'm supplying. How can I select ONLY those rows which contain all the criteria?
just wrap the OR bit in brackets:
(PartNumber LIKE '%#trim(SearchCriteria)#%' OR PartDescription LIKE '%#trim(SearchCriteria)#%') AND...
at the moment you have A or B and C which is being read as A or (B and C). You want (A or B) and C.
And make sure you use cfqueryparam as suggested above.
Just a guess really, but change
...'%#trim(SearchCriteria)#%' OR
PartDescription ...
to
...'%#trim(SearchCriteria)#%' AND
PartDescription ...
edit
or are you saying some have null values?
post comment edit
Imagine for example that InletFlange is empty for your part, and the user didn't put it in their search.
Then InletFlange LIKE '#URL.inlet#' will compare "" LIKE "", which is of course true, so the product shows up.
There are many ways to solve this. For example
Default the search criteria to "N/A" or something
Default the column in the database in a similar fashion.
something along the lines of AND NOT ISNULL(InletFlange,'')=='' for each criteria
Okay, I found the answers:
Leigh: +1 to clarifying the question. (It is
unlikely this has any bearing on the
results, but what is the point of
using LIKE without wildcards "%"? If
it is an equality comparison you are
after, just use equals ie ... AND
Condition = '#URL.condition#' )
You were quite right, I was missing the percent symbols in my LIKE comparisons (DUH!). I removed them earlier thinking they were the problem, but they weren't, this was:
Aiden: just wrap the OR bit in brackets:
(PartNumber LIKE '%#trim(SearchCriteria)#%' OR PartDescription LIKE '%#trim(SearchCriteria)#%') AND...
Thank you Aiden!
While this isnt an answer to your question - you might want to consider using a dedicated search solution such as Solr (which is included with CF9). Its a more powerful fulltext solution than just raw SQL and includes facetting

SQL Query with Multiple Like Clauses

I would like to create a SQL query, which does the following..
- I have a few parameters, for instance like "John","Smith"
- Now I have a articles tables with a content column, which I would like to be searched
- Now, How can I find out the rows in the articles table, which has the any one of those values("John","Smith")
I cannot use content LIKE "%john% or content LIKE "%smith%", as there could be any number of incoming parameters.
Can you guys please tell me a way to do this....
Thanks
Have you considered full-text search?
While HLGEM's solution is ideal, if full-text search is not possible, you could construct a regular expression that you could test only once per row. How exactly you do that depends on the DBMS you're using.
This depends a lot on the DBMS you're using. Generally - if you don't want to use full-text search - you can almost always use regular expressions to achive this goal. For MySQL see this manual page - they even have example answering your question.
If full text search is overkill, consider putting the parameters in a table and use LIKE in theJOIN` condition e.g.
SELECT * -- column list in production code
FROM Entities AS E1
INNER JOIN Params AS P1
ON E1.entity_name LIKE '%' + P1.param + '%';

How to update an SQLite database with a search and replace query?

My SQL knowledge is very limited, specially about SQLite, although I believe this is will be some sort of generic query... Or maybe not because of the search and replace...
I have this music database in SQLite that has various fields of course but the important ones here the "media_item_id" and "content_url".
Here's an example of a "content_url":
file:///c:/users/nazgulled/music/band%20albums/devildriver/%5b2003%5d%20devildriver/08%20-%20what%20does%20it%20take%20(to%20be%20a%20man).mp3
I'm looking for a query that will search for entries like those, where "content_url" follows that pattern and replace it (the "content_url") with something else.
For instance, a generic "content_url" can be this:
file:///c:/users/nazgulled/music/band%20albums/BAND_NAME/ALBUM_NAME/SONG_NAME.mp3
And I want to replace all these entries with:
file:///c:/users/nazgulled/music/bands/studio%20albums/BAND_NAME/ALBUM_NAME/SONG_NAME.mp3
How can I do it in one query?
P.S: I'm using Firefox SQLite Manager (couldn't find a better and free alternative for Windows).
You are probably looking for the replace function.
For example,
update table_name set
content_url = replace(content_url, 'band%20albums', 'bands/studio%20albums')
where
content_url like '%nazgulled/music/band_20albums/%';
More documentation at http://sqlite.org/lang_corefunc.html