SQL - How to use a LIKE Matching pattern with a Replace ? - sql

I'm trying to make a replace with an sql request, but I also need to use a matching pattern in the replace request.
Let's suppose I have a database with one column movie and three fields : 'Batman Begins', 'The dark knight', 'the dark knight rises'.
And my request would look something like that :
SELECT replace(movie, 'kn%t','') as movie FROM db_movie;
or something like that :
SELECT replace(movie, movie like ('kn%t'),'') as movie FROM db_movie;
And I should get the following output :
('Batman Begins', 'the dark','the dark rises')
But I doesn't work, so do you have an idea how I could do that ?
And I need for my true request to use the '%' matching pattern, not just directly write the word "knight" that could be enough for that example.
Thank you very much for your answers :)

In SQL SERVER, there's a function CHARINDEX() that will help you.
Select movie from db_Movie where CHARINDEX('dark',movie) <> 0

Try the following query-:
select case when movie like '%knight%' then
replace(movie,'knight','') else movie end as movie
FROM db_movie
SQL Server

Try the following query-:
SELECT CASE WHEN MOVIE LIKE '%KNIGHT%' THEN
REPLACE(MOVIE,'KNIGHT','') ELSE MOVIE END AS MOVIE
FROM DB_MOVIE
SQL SERVER

Related

Returning values from a database with a Substring

Let's say I've got 3 product description fields with the values AC-120 XXX, AC-120,CCC and AC-120 BBB.
How would I get that information from a table using only AC-120 as my search argument?
I've tried using the subStr function but that won't return any values either
SELECT TbArtikel.Artikel_Merk, TbArtikel.Artikel_Groep, TbArtikel.Artikel_Categorie_ID, TussenMAATenARTKEL.VoorraadNummer, TbArtikel.Artikel_ID, TussenMAATenARTKEL.ArtikelDetail_ID, TbArtikel.Artikel_Prijs_Advies, TbArtikel.Artikel_Prijs_Bees, TbArtikel.Artikel_Omschrijving
FROM TbArtikel INNER JOIN TussenMAATenARTKEL ON TbArtikel.Artikel_ID = TussenMAATenARTKEL.Artikel_ID
WHERE (((TbArtikel.Artikel_Merk)="Yonex") AND ((TbArtikel.Artikel_Omschrijving)="%AC-102%"));
LIKE is very appropriate for this comparison. However, MS Access uses different wildcards from standard SQL. So you want:
TbArtikel.Artikel_Omschrijving) LIKE "*AC-102*"
In standard SQL, this would be:
TbArtikel.Artikel_Omschrijving) LIKE "%AC-102%"
Try '((TbArtikel.Artikel_Omschrijving) LIKE "%AC-102%"' instead of '((TbArtikel.Artikel_Omschrijving)="%AC-102%"'
Use MID() function as
MID(TbArtikel.Artikel_Omschrijving, 1, 6) = 'AC-120'
Instead of
TbArtikel.Artikel_Omschrijving ="%AC-102%"

Like Clause over an 'Element' - ORACLE APEX

I encounter some problems that i don't understand with APEX.... Well, let's be specific.
I ve got a select element retrieving a top 50 of most liked url (P11_URL). This is populate by a table view, TOp_Domains.
I create an element called "Context" that have to print all text containing the URL selected by the user from the element select. Those Texts come from another table, let's say "twitter_post".
I create a dynamic action (show only) with this sql/statement:
Select TXT, NB_RT, RANK
from myschema.twitter_post
where TXT like '%:P11_URL%'
group by TXT, NB_RT, RANK
.... and it doesn't work... I think APEX don't like like clause... But i don't know how to do. Let's keep in min an url could have been shared by multiple Tweets, that's why this element "context" is important for me.
I tried to bypass the problem by building a State (in french Statique) and a dynamic action that will refresh the state but it doesn't work neither... bouhououououou
TriX
Right click on the 'P11_URL' and create DA. Event :change, Item:P11_URL. As the true action of the DA, select 'Set Value'. Write your query in the sql stmt area. In the page items to submit, select 'P11_URL' . In the 'Affected Items': select 'Context'.
Query should be :
Select TXT, NB_RT, RANK
from myschema.twitter_post
where TXT like '%' || :P11_URL || '%'
group by TXT, NB_RT, RANK
So
Thanks to #Madona... Their example made me realised my mistake. I wrote the answer here for futher help if somebody encouter the same porblem.
A list select element get as arguments a display value (the one you want to be shown in your screen.... if you want so....^^ ) and a return value (in order, I think to linked dynamic actions). So to solved my problem i had to shape my sql statement as:
select hashtags d, hastags r
from my table
order by 1
[let s say that now in Apex it s an object called P1_HASHTAGS]
First step problem solving.
In fact, the ranking as second value, as i put into my sql statement was making some mitsakens into my 'Where like' clause search... well... Newbie am i!
Second step was to correctly formate the sql statement receiving the datas from my select lov (P1_HASHTAGS) into my interactive report. As shown here:
Select Id, hashtags
from my table
where txt like '%'||:P1_HASHTAGS||'%'
And it works!
Thank you Madona your example helped me figure my mistakes!

Multiple Likes in SQL

Please review the following code and help me in fixing the error.
When i'm trying to execute it i'm getting only the records which were starting with the ENTBI-Q.But the two conditions which were written below(task%,INC%) were not getting executed.
I want the records which starts with task,incident and entbi-q.
Please note that the task and incident are the field values of one column and the entbi-q is the field values of the another coloumn.
SELECT
S1."NAME" AS "NAME",
S1."SYS_ID" AS "SYSID",
T2."ASSIGNMENT_GROUP" AS "ASSIGNMENTGROUP",
T2."NUMBER_" AS "NUMBER",
T2."CLOSED_AT" AS "CLOSEDAT",
T2."OPENED_AT" AS "OPENEDAT"
FROM
"IOD"."SYS_USER_GROUP" S1,
"IOD"."TASK" T2
WHERE
(S1."SYS_ID"=T2."ASSIGNMENT_GROUP")
AND S1."NAME" LIKE 'ENTBI-Q%'
AND T2."NUMBER" LIKE 'TASK%'
AND T2."NUMBER" LIKE 'INC%'
AND T2."NUMBER" LIKE 'TASK%'
AND T2."NUMBER" LIKE 'INC%'
This can never be true. If a string starts with TASK it cannot start with INC. You probably wanted to OR the two conditions:
AND (T2."NUMBER" LIKE 'TASK%'
OR T2."NUMBER" LIKE 'INC%')

looking for db2 text function or method I can do a text contain rather than like

I'm looking for a db2 function that does a text contain search. At present I am running the following query against the data below....
SELECT distinct
s.search_id,
s.search_heading,
s.search_url
FROM repman.search s, repman.search_tags st
WHERE s.search_id = st.search_id
AND ( UPPER(s.search_heading) LIKE (cast('%REPORT%' AS VARGRAPHIC(32)))
OR (UPPER(st.search_tag) LIKE cast('%REPORT%' AS VARGRAPHIC(32)))
)
ORDER BY s.search_heading;
Which returns...
But if I change the search text to %REPORTS% rather than %REPORT% (which I need to do) the like search does not work and I get zero results.
I read a link that used a function named CONTAINS like below but when trying to use the function I get an error.
SELECT distinct
s.search_id,
s.search_heading,
s.search_url
FROM repman.search s, repman.search_tags st
WHERE s.search_id = st.search_id
AND CONTAINS(s.search_heading, 'REPORTS') = 1
Has anynoe got any suggestions? I'm on db2 version DB2/LINUXPPC 9.1.6.
Thanks
In order to look for a pattern in a string, you can use Regular Expressions. They are built-in DB2 with xQuery since DB2 v9. There are also other ways to do that. I wrote an article in my blog (in Spanish that you can translate) about Regular Expressions in DB2.
xmlcast(xmlquery('fn:matches(\$TEXT,''^[A-Za-z 0-9]*$'')')

Why is this query not working for number 1?

this query returns 5 results as expected
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '0'
but this query returns nothing
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1'
the USERFIELD1 is a varchar 80 field
Here is all the data that is in the DB and as you can see there are two records with USERFIELD1 = '1' but there seems to be a line break after them...maybe that is causing the issue...is there anyway to grab those two records
LEADID,CREATEUSER,CREATEDATE,MODIFYUSER,MODIFYDATE,ACCOUNTMANAGERID,ASSIGNDATE,BUSINESSDESCRIPTION,COMPANY,COMPANY_UC,CREDITRATING,DATAQUALITY,DESCRIPTION,DIVISION,DONOTSOLICIT,EMAIL,EMPLOYEES,FAX,FIRSTNAME,HOMEPHONE,IMPORTID,IMPORTSOURCE,INDUSTRY,INTERESTS,ISPRIMARY,LASTCALLDATE,LASTNAME,LASTNAME_UC,LEADSOURCEID,MIDDLENAME,MOBILE,NEXTCALLDATE,NOTES,PREFERRED_CONTACT,PREFIX,PRIORITY,QUALIFICATION_CATEGORYID,REVENUE,SECCODEID,SICCODE,STATUS,SUFFIX,TICKER,TITLE,TOLLFREE,TYPE,USERFIELD1,USERFIELD2,USERFIELD3,USERFIELD4,USERFIELD5,USERFIELD6,USERFIELD7,USERFIELD8,USERFIELD9,USERFIELD10,WEBADDRESS,WORKPHONE,LEAD_ADDRESSID,DONOTEMAIL,DONOTFAX,DONOTMAIL,DONOTPHONE
Q134915558 ,U6UJ9A00000S,2011-09-20 17:36:10.053,U6UJ9A00000S,2011-09-20 17:36:10.053,NULL,2011-09-20 17:36:10.053,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,test#gmail.com,NULL,NULL,Harry,NULL,NULL,NULL,NULL,Restaurant Pro Express demo download,T,NULL,Scott,SCOTT, ,NULL,NULL,NULL,this is from the site,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,1
,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4075559999,QQ134915558 ,NULL,NULL,NULL,NULL
Q39769667 ,U6UJ9A00000S,2011-09-20 17:46:18.103,U6UJ9A00000S,2011-09-20 17:46:18.103,NULL,2011-09-20 17:46:18.103,NULL,scaoo,SCAOO,NULL,NULL,NULL,NULL,0,harry333#harry.com,NULL,NULL,upper2,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is a download,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ39769667 ,NULL,NULL,NULL,NULL
Q488888476 ,U6UJ9A00000S,2011-09-20 17:49:28.963,U6UJ9A00000S,2011-09-20 17:49:28.963,NULL,2011-09-20 17:49:28.963,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,markus#gmail.com,NULL,NULL,upper,NULL,NULL,NULL,sales,posnation.com online demo request,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is from upper,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ488888476 ,NULL,NULL,NULL,NULL
Q504845720 ,U6UJ9A00000S,2011-09-20 17:06:10.053,U6UJ9A00000S,2011-09-20 17:06:10.053,U6UJ9A00000G,2011-09-20 17:06:10.053,NULL,Rafner ext.,RAFNER EXT.,NULL,NULL,NULL,NULL,0,raf#raf.com,NULL,NULL,James,4075615519,NULL,NULL,sales,NULL,NULL,NULL,Rafner,RAFNER, ,NULL,NULL,NULL,Raf associates is asking a question,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,QQ504845720 ,NULL,NULL,NULL,NULL
Q539171226 ,U6UJ9A00000S,2011-09-20 17:49:28.963,U6UJ9A00000S,2011-09-20 17:49:28.963,NULL,2011-09-20 17:49:28.963,NULL,scaoo,SCAOO,NULL,NULL,NULL,NULL,0,harry333#harry.com,NULL,NULL,upper3,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is a download,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ539171226 ,NULL,NULL,NULL,NULL
Q547088411 ,U6UJ9A00000S,2011-09-20 17:46:18.103,U6UJ9A00000S,2011-09-20 17:46:18.103,NULL,2011-09-20 17:46:18.103,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,markus#gmail.com,NULL,NULL,upper,NULL,NULL,NULL,sales,posnation.com online demo request,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is from upper,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ547088411 ,NULL,NULL,NULL,NULL
Q913526837 ,U6UJ9A00000S,2011-09-20 17:36:10.053,U6UJ9A00000S,2011-09-20 17:36:10.053,NULL,2011-09-20 17:36:10.053,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,test#gmail.com,NULL,NULL,Parry,NULL,NULL,NULL,NULL,Restaurant Pro Express demo download,T,NULL,Scott,SCOTT, ,NULL,NULL,NULL,this is from the site agan,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,1
,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4075559999,QQ913526837 ,NULL,NULL,NULL,NULL
Q925684753 ,U6UJ9A00000S,2011-09-21 09:36:10.420,U6UJ9A00000S,2011-09-21 09:36:10.420,NULL,2011-09-21 09:36:10.420,NULL,POSfasion,POSFASION,NULL,NULL,NULL,NULL,0,sdfa#ss.com,NULL,NULL,Maria,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,becker4,BECKER4,L6UJ9A000004,NULL,NULL,NULL,this is another lead from the live site,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,7778889999,QQ925684753 ,NULL,NULL,NULL,NULL
If there are characters after the 1 in the userField1 then try using a wildcard, as shown below.
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1%'
Also the statement you wrote
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1'
Is essentially equivalent to
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 = '1'
You may want to read up on using the LIKE clause
http://msdn.microsoft.com/en-us/library/ms179859.aspx
Use
where USERFIELD1 = '1
'
You don't need LIKE as you aren't using any wild cards.
You're using LIKE wrong. Put a wildcard or a pattern in there or use an = operator.
Your data seems malformed.
Try a like with the value surrounded by a wildcard
ie USERFIELD1 like '1%'
Should sort out whatever problem you're having if it is related to the line break field