I'm trying to concatenate 2 columns using delimiter as "."
code :
PCollection<BeamRecord> first = apps.apply(BeamSql.query(
"SELECT *,('CatLib' || 'ProdKey') AS CatLibKey from PCOLLECTION"));
How shall I specify delimiter between 2 columns ?
I'd say go for
SELECT
COALESCE(CatLib, '') || '.' || COALESCE(ProdKey, '') AS CatLibKey,
(any other columns here...)
FROM
PCOLLECTION;
but in SQL there is no "Select everything but column X" or "Select everything else" so you'd have to write down every name of the column you want to select.
Thanks #Impulse The Fox.
I have modified my query to :
PCollection<BeamRecord> first = apps.apply(BeamSql.query(
"SELECT Outlet, CatLib, ProdKey, Week, SalesComponent, DuetoValue, PrimaryCausalKey, CausalValue, ModelIteration, Published, (CatLib || '.' || ProdKey) AS CatLibKey from PCOLLECTION"));
and this worked perfectly.
Related
I am working on Data standardization rules, and one of the rules says, "If the last name is part of first name, then remove the last name from first name".
my Query- how do i check if first name column has the last name in it using oracle sql developer?
I tried using :
select fst_nm, lst_nm from emp where fst_nm = fst_nm || lst_nm ;
but this query returns '0' results.
Also, I tried another query:
select fst_nm, lst_nm, regexp_substr(fst_nm, '[^ ]+',1,1) from emp ;
I tried using the below query
select fst_nm, lst_nm from emp where fst_nm = fst_nm || lst_nm ;
but this query returns nothing, I mean '0' results.
Also, I tried another query:
select fst_nm, lst_nm, regexp_substr(fst_nm, '[^ ]+',1,1) from emp ;
expected result is:
fst_nm = john smith ;
lst_nm = smith
Actual result showing up is :
fst_nm = john ;
lst_nm = smith
Please help
You should be able to just do a blanket replace on the entire table:
UPDATE emp
SET fst_nm = REPLACE(fst_nm, lst_nm, '');
The reason this should work is that for those records where the last name does not appear as part of the first name, the replace would have no effect. Otherwise, the last name would be stripped from the first name.
You can use below logic
select length('saurabh rai'),instr('saurabh rai',' '),case when length('saurabh rai') > instr('saurabh rai',' ') then substr('saurabh',1,instr('saurabh rai',' ')-1) else 'saurabh rai' end as a from dual;
Update emp set fst_nm=(Case when length(fst_nm) > instr(fst_nm, ' ') then substr(fst_nm,1,instr(fst_nm,' ')-1) else fst_nm end);
I have a SQL query and I would like to insert a hashtag between one column and another to be able to reference in Excel, using an import option in fields delimited by #. Anyone have an idea how to do it? A query is as follows:
SELECT FC.folha, folha->folhames,folha->folhaano, folha->folhaseq, folha->folhadesc, folha->TipoCod as Tipo_Folha,
folha->FolhaFechFormatado as Folha_Fechada, folha->DataPagamentoFormatada as Data_Pgto,
Servidor->matricula, Servidor->nome, FC.rubrica,
FC.Rubrica->Codigo, FC.Rubrica->Descricao, FC.fator, FC.TipoRubricaFormatado as TipoRubrica,
FC.ValorFormatado,FC.ParcelaAtual, FC.ParcelaTotal
FROM RHFolCalculo FC WHERE folha -> FolhaFech = 1
AND folha->folhaano = 2018
and folha->folhames = 06
and folha->TipoCod->codigo in (1,2,3,4,6,9)
You are generating delimited output from the query, so the first row should be a header row, with all following rows the data rows. You will really only have one column due to concat. So remove the alias from the columns, output the first row like so (using the alias here) . . .
SELECT 'folha#folhames#folhaano#folhaseq#folhadesc#Tipo_Folha#
Folha_Fechada#Data_Pgto#
matricula#nome#rubrica#
Codigo#Descricao#fator#TipoRubrica#
ValorFormatado#ParcelaAtual#ParcelaTotal'
UNION
SELECT FC.folha || '#' || folha->folhames || '#' || folha->folhaano . . .
The UNION will give the remaining rows. Note some conversion may be necessary on the columns data if not all strings.
I have a table with an integer column. It has 12 records numbered 1000 to 1012. Remember, these are ints.
This query returns, as expected, 12 results:
select count(*) from proposals where qd_number::text like '%10%'
as does this:
SELECT COUNT(*) FROM "proposals" WHERE (lower(first_name) LIKE '%10%' OR qd_number::text LIKE '%10%' )
but this query returns 2 records:
SELECT COUNT(*) FROM "proposals" WHERE (lower(first_name) || ' ' || qd_number::text LIKE '%10%' )
which implies using || in concatenated where expressions is not equivalent to using OR. Is that correct or am I missing something else here?
You probably have nulls in first_name. For these records (lower(first_name) || ' ' || qd_number::text results in null, so you don't find the numbers any longer.
using || in concatenated where expressions is not equivalent to using ORIs that correct or am I missing something else here?
That is correct.
|| is the string concatenation operator in SQL, not the OR operator.
I want to fetch the names of employees from a table upto the character ':' but couldn't as substr and ltrim is not working as expected. Below given are given some examples:
ABINERI:REBECCA C
CARRINGTON:JAMES M
But I want them in the way given below:
REBECCA C ABINERI
JAMES M CARRINGTON
I just used the query below in Toad for Oracle:
<pre>
<b>select name from employees</b>
</pre>
Please try below query:
select SUBSTR(name,(INSTR(name,':')+1)) || ' ' || SUBSTR(name,1,(INSTR(name,':'))-1) from employees;
hope above query will resolve your issue.
My current SQL statement is:
SELECT *
FROM names
WHERE UPPER(first_name) LIKE UPPER('John Smith%')
OR UPPER(last_name) LIKE UPPER('John Smith%')
OR UPPER(first_name || ' ' || last_name) LIKE UPPER('John Smith%')
I want to search my table for "John Smith", this SQL statement is okay.
But what if I have an entry with the first name as 'John Kevin' and last name 'Smith', this wouldn't include that entry. What do I need to add? Thanks all! :)
You can use the Similar To operator to cover all possible combinations.
Select * from table names UPPER(first_name || ' ' || last_name)
SIMILAR to '%(UPPER(John)|UPPER(Smith))%';