Remove / backslash from query to get valid data in oracle - sql

With my latest query, I get the data as ABDD_1037/. but I don't want the record which has / in it.
I tried with below query but it's not working
SELECT TO_CHAR(RJ_INTRACITY_LINK_ID) AS SPAN_ID,
TO_CHAR(RJ_MAINTENANCE_ZONE_CODE) AS MAINT_ZONE_CODE
FROM NE.MV_SPAN#DB_LINK_NE_VIEWER
-- FROM APP_FTTX.span#SAT
WHERE LENGTH(RJ_INTRACITY_LINK_ID) > 8
AND LENGTH(RJ_INTRACITY_LINK_ID) < 21
AND INVENTORY_STATUS_CODE = 'IPL'
AND RJ_MAINTENANCE_ZONE_CODE = 'INUEABDD01'
AND NOT REGEXP_LIKE (RJ_INTRACITY_LINK_ID,'_(9|31|4|7|_____|U|[\/]$)','i')
MINUS
SELECT TO_CHAR(LINK_ID) AS SPAN_ID,
TO_CHAR(MAINTENANCEZONECODE) AS MAINT_ZONE_CODE
FROM TBL_FIBER_INV_JOBS
WHERE SPAN_TYPE = 'INTRACITY'
AND MAINTENANCEZONECODE = 'INUEABDD01'
ORDER BY 1;
UPDATE
IF I comment the whole regex line, I would get all the unwanted data like below
`ABDD_0102_U`, `ABDD_1037/`,`ABDD_3102`, `ABDD_4003`, `STHU_9032`,

At the moment your pattern:
'_(9|31|4|7|_____|U|[\/]$'
will match any value which contains _9, _31, _4, _7, ______ (six underscores), _U or _u anywhere; or which ends with _/ (or _<backslash>, which I don't think you intended).
If you want it to match values ending with _<someting>/ then you need to allow for the <something> part, e.g.:
'_(9|31|4|7|_____|U|.*/$)'
If you want it to match values with a / in it anywhere, not just at the end and following an underscore, then you need to separate that from the underscore prefix and remove the $ anchor, e.g.:
'(_(9|31|4|7|_____|U)|/)'
db<>fiddle

Related

AGE question: WITH clause doesn't work with MATCH/WITH

If I have the following query
select * from cypher('agload_test_graph', $$ match (n) with n where n.name='A' return n $$) as (sp agtype)
then n.name='A' doesn't work.
but if I remove with clause, then it works.
select * from cypher('agload_test_graph', $$ match (n) where n.name='A' return n $$) as (sp agtype)
I tried the example query in age document.
SELECT *
FROM cypher('graph_name', $$
MATCH (david {name: 'David'})-[]-(otherPerson)-[]->()
WITH otherPerson, count(*) AS foaf
WHERE foaf > 1RETURN otherPerson.name
RETURN otherPerson.name
$$) as (name agtype);
The problem with your first query is that the WHERE clause is not applied to the n variable because the WHERE clause is used before the WITH clause.  By WITHclause, a new variable that is a subset of the previous variable is created in Cypher. Any filters added after the the WITH clause will be applied to this new variable rather than the original variable. By deleting the WITH clause from your second query, the where clause is being applied directly to the n variable, which is what you want.
The example query in the age document you provided is a legitimate Cypher query. It uses MATCH to find a node with the name "David", then it uses  WITH clause to create a new variable otherPerson, which is the set of nodes nodes connected to the "David" node, and then uses  count(*) function to count the number of nodes connected to the "David" node. After this it uses the WHERE clause to filter otherPerson nodes where the count of their connections is greater than 1 and return their names.
The where clause after with is ignored. This is one of the issues in Apache Age which is opened on github. Issue Page

Trying to search on the last 4 of SSN (SQL)

So I have found multiple resources that let me know how to retrieve the last 4 of a SSN. But I am trying to search the database for matches of the last 4 that I am provided. I am working with Oracle btw. I've tried a few different things but below is basically what I mean:
SELECT gm.plan_name, tpam.third_prty_admin_name, pm.partcpnt_first_name,
pm.partcpnt_last_name, pm.partcpnt_id, gm.grp_id,
tpam.third_prty_admin_id,
pm.partcpnt_birth_dt,pm.partcpnt_setup_dt
FROM gva_s01.partcpnt_mstr pm,
gva_s01.grp_mstr gm,
gva_s01.third_prty_admin_mstr tpam
WHERE gm.cntrct_id = 'CNTRCTID0000'
AND gm.third_prty_admin_id = 0
AND gm.third_prty_admin_id = tpam.third_prty_admin_id
AND gm.cntrct_id = pm.cntrct_id
--This is where I can't figure it out
AND SUBSTRING(pm.partcpnt_id, -4) = '1234'
In Oracle, you want SUBSTR( string, position ) and a negative position will count from the end of the string. So:
AND SUBSTR(pm.partcpnt_id, -4) = '1234'
Or you can use LIKE:
AND pm.partcpnt_id LIKE '%1234'

Oracle SQL Insert external values

I'm having problems to add some values to a table. The query that i'm using is this one:
INSERT INTO ACTUAR
(CAPITULO,TEMPORADA,COD_GRUPO,COD_LOC)
SELECT NUM_CAP,NUM_TEMPO,CODIGO_GRUPO,CODIGO_LOC
FROM COMENTAR
MINUS
SELECT CAPITULO,TEMPORADA,COD_GRUPO, COD_LOC
FROM ACTUAR;
The ACTUAR table has 2 more values, which I want to add from a value on COMENTAR like this:
NVL(SUBSTR(COMENTARIO,0,10),'SIN PROBLEMA'),
NVL(SUBSTR(COMENTARIO,LENGTH(COMENTARIO)-4),'SIN SOLUCION')
Actuar table
CAPITULO,TEMPORADA,COD_GRUPO,COD_LOC,PROBLEMA,SOLUCION
Comentar table
NUM_CAP,NUM_TEMPO,CODIGO_GRUPO,CODIGO_LOC,COMENTARIO
I want to put the first 10 chars of COMENTARIO on ACTUAR'S PROBLEMA and the last 4 on ACTUAR'S SOLUCION or the text in the nvl if they are null.
I want to insert the result of the minus operation on ACTUAR plus the 10 first chars of COMENTARIO from COMENTAR into ACTUAR'S PROBLEMA and the last 4 on ACTUAR'S SOLUCION
The desired result should be something like
ACTUAR
CAPITULO,TEMPORADA,COD_GRUPO, and COD_LOC the values from the minus operation.
PROBLEMA - first 10 chars of COMENTAR COMENTARIO
SOLUCION - last 4
This solution worked for me. I used merge statement, without update clause.
merge into actuar a
using (
select num_cap, num_tempo, codigo_grupo, codigo_loc,
nvl(substr(comentario,1,10),'SIN PROBLEMA') prob,
nvl(substr(comentario, greatest(length(comentario)-4, 1)),'SIN SOLUCION') sol
from comentar) c
on (a.capitulo = c.num_cap and a.temporada = c.num_tempo
and a.cod_grupo = c.codigo_grupo and a.cod_loc = c.codigo_loc)
when not matched then insert (a.capitulo, a.temporada, a.cod_grupo,
a.cod_loc, a.problema, a.solucion)
values (c.num_cap, c.num_tempo, c.codigo_grupo, c.codigo_loc, c.prob, c.sol)

Issue with union query - select list not compatible

Been working on this query for some time now... Keep getting the error "Corrosponding select-list expressions are not compatible. I am selecting the same # of columns in each select statement.
create volatile table dt as (
SELECT
gcv.I_SYS_IDV,
gcv.i_pln,
gcv.c_typ_cov,
gcv.d_eff,
gcv.d_eff_pln,
gcv.c_sta,
gcv.d_sta,
gcv.c_mde_bft_fst,
gcv.a_bft_fst,
gcv.c_mde_bft_sec,
gcv.a_bft_sec,
gcv.c_mde_bft_trd,
gcv.a_bft_trd,
gcv.p_cre_hom,
gcv.c_cl_rsk,
gpv.c_val,
gpv.i_val,
gcv.c_pol,
gpv.i_prv
FROM Pearl_P.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
WHERE gcv.i_pln > 0
AND gcv.i_pln = gpv.i_pln
and gpv.i_prv = '36'
and gcv.c_pol between 'lac100001' and 'lac100004'
UNION
SELECT
gcv.I_SYS_IDV,
gcv.i_pln,
gcv.c_typ_cov,
gcv.d_eff,
gcv.d_eff_pln,
gcv.c_sta,
gcv.d_sta,
gcv.c_mde_bft_fst,
gcv.a_bft_fst,
gcv.c_mde_bft_sec,
gcv.a_bft_sec,
gcv.c_mde_bft_trd,
gcv.a_bft_trd,
gcv.p_cre_hom,
gcv.c_cl_rsk,
gcv.c_pol,
gpv.i_val,
gpv.i_pln,
gpv.i_prv
FROM Pearl_P.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
where NOT EXISTS(
SELECT 1
FROM pearl_p.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
WHERE gcv.i_pln > 0
AND gcv.i_pln = gpv.i_pln
and gpv.i_prv = '36'
)
) with data
PRIMARY INDEX (i_sys_idv)
on commit preserve rows;
You should check the data types of each column. The data types must be compatible between each SELECT statement.
The last 4 values of your second select statement don't match the ones in your first statement. Try naming(using aliases) those columns the same thing(pairing them). To union you need to have the same columns between the sets.
Check out: http://msdn.microsoft.com/en-us/library/ms180026.aspx
The following are basic rules for combining the result sets of two
queries by using UNION:
The number and the order of the columns must be the same in all
queries.
The data types must be compatible.
Are the data types of each column the same in both portions of the query?
If the first character of the column name indicates the data type (i = integer, c = character, etc.) I'm guessing that the problem is with the second to last column in the select list. The first query is selecting gcv.c_pol, the second query is selecting gpv.i_pln.
Start commenting-out lines until it works. I.e., comment out all of the fields in each select list, and then one-by-one, un-comment the first one out, then the second, etc. You'll find it eventually.

Using SQLDF to select specific values from a column

SQLDF newbie here.
I have a data frame which has about 15,000 rows and 1 column.
The data looks like:
cars
autocar
carsinfo
whatisthat
donnadrive
car
telephone
...
I wanted to use the package sqldf to loop through the column and
pick all values which contain "car" anywhere in their value.
However, the following code generates an error.
> sqldf("SELECT Keyword FROM dat WHERE Keyword="car")
Error: unexpected symbol in "sqldf("SELECT Keyword FROM dat WHERE Keyword="car"
There is no unexpected symbol, so I'm not sure whats wrong.
so first, I want to know all the values which contain 'car'.
then I want to know only those values which contain just 'car' by itself.
Can anyone help.
EDIT:
allright, there was an unexpected symbol, but it only gives me just car and not every
row which contains 'car'.
> sqldf("SELECT Keyword FROM dat WHERE Keyword='car'")
Keyword
1 car
Using = will only return exact matches.
You should probably use the like operator combined with the wildcards % or _. The % wildcard will match multiple characters, while _ matches a single character.
Something like the following will find all instances of car, e.g. "cars", "motorcar", etc:
sqldf("SELECT Keyword FROM dat WHERE Keyword like '%car%'")
And the following will match "car" or "cars":
sqldf("SELECT Keyword FROM dat WHERE Keyword like 'car_'")
This has nothing to do with sqldf; your SQL statement is the problem. You need:
dat <- data.frame(Keyword=c("cars","autocar","carsinfo",
"whatisthat","donnadrive","car","telephone"))
sqldf("SELECT Keyword FROM dat WHERE Keyword like '%car%'")
# Keyword
# 1 cars
# 2 autocar
# 3 carsinfo
# 4 car
You can also use regular expressions to do this sort of filtering. grepl returns a logical vector (TRUE / FALSE) stating whether or not there was a match or not. You can get very sophisticated to match specific items, but a basic query will work in this case:
#Using #Joshua's dat data.frame
subset(dat, grepl("car", Keyword, ignore.case = TRUE))
Keyword
1 cars
2 autocar
3 carsinfo
6 car
Very similar to the solution provided by #Chase. Because we do not use subset we do not need a logical vector and can use both grep or grepl:
df <- data.frame(keyword = c("cars", "autocar", "carsinfo", "whatisthat", "donnadrive", "car", "telephone"))
df[grep("car", df$keyword), , drop = FALSE] # or
df[grepl("car", df$keyword), , drop = FALSE]
keyword
1 cars
2 autocar
3 carsinfo
6 car
I took the idea from Selecting rows where a column has a string like 'hsa..' (partial string match)