I've got to filter data in a string column and thus need to compare the column using NOT LIKE
with a bunch of strings.
I'm using SQL Server and my code looks like this:
SELECT history.date,
history.new_value,
res.resource_name
FROM item_history history,
owned_resource res
WHERE (history.attr_name = 'resource_contact' OR history.attr_name = 'Kontakt-UUID')
AND res.inactive = 0
AND history.com_par_id = res.own_resource_uuid
AND lower(history.new_value) NOT LIKE '%tp für%'
AND lower(history.new_value) NOT LIKE '%rücklieferung%'
AND lower(history.new_value) NOT LIKE '%rückläufer%'
AND lower(history.new_value) NOT LIKE '%stoerreserve%'
AND lower(history.new_value) NOT LIKE '%zentrallager%'
AND lower(history.new_value) NOT LIKE '%bhs-pool%'
AND lower(history.new_value) NOT LIKE '%lager halle%'
AND lower(history.new_value) NOT LIKE '%lager logistik%'
AND lower(history.new_value) NOT LIKE 'reserve %'
AND lower(history.new_value) NOT LIKE '%igeko%bhs%'
AND lower(history.new_value) NOT LIKE '%service%ecg%'
AND lower(history.new_value) NOT LIKE '%multifunktionsdrucker%'
AND lower(history.new_value) NOT LIKE 'nn%gisa%raum%'
AND lower(history.new_value) NOT LIKE '%citrix%admins%'
AND lower(history.new_value) NOT LIKE '%personalwesen%'
AND lower(history.new_value) NOT LIKE '%etagendrucker%'
AND lower(history.new_value) NOT LIKE '%schulungsraum%'
AND lower(history.new_value) NOT LIKE '%team%raum%'
AND lower(history.new_value) NOT LIKE '%beratungsraum%'
AND lower(history.new_value) != 'reserve'
)
I guess the performance is not the best calling "lower()" over and over. Also, as a programmer, my nails are rolling up seeing so much redundant code. Unfortunately I did not find a nice way to use a variable or something. (I want to add that I cannot simply add a new computed column, which would be a good way here, I'm only authorized to read data.)
Any advise to make the code smarter would be appreciable?
lower()
and the other about repetition of code. Even though both have to do with the same query, IMHO it would be best to address them separately. This is a Q&A site with the goal to make the questions and answers re-usable, so it's best to make the questions (and, consequently, the answers) as focused as possible, and separating different issues into different questions is one way to do that. – Andriy M Nov 14 '19 at 8:23