# in Caché between columns - intersystems-cache

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.

Related

FOR XML PATH in Postgres

Hi everyone I'm trying convert this code so it works in postgres. I figured I will use to_char(dateinquar:: date, 'mm/dd/yyyy') instead of convert(varchar,dateinquar,101). But I'm not sure what can be used instead of FOR XML PATH? I don't need to generate any XML document. How would I get multiple dates in a single row separated by comma for the case below?
Case d.Closing WHEN 0 THEN NULL WHEN 1 THEN (
substring((SELECT convert(varchar,dateinquar,101) + ', ' FROM PDT_tblAReasons sub
WHERE sub.IDnumber = d.IDnumber and dateoutquar IS NULL
GROUP BY IDnumber,dateinquar FOR XML PATH('')),0,len((
SELECT convert(varchar,dateinquar,101) + ', ' FROM PDT_tblAReasons sub
WHERE sub.IDnumber = d.IDnumber and dateoutquar IS NULL
GROUP BY IDnumber,dateinquar FOR XML PATH('')))-0)) end As [Closing Date]
Thank you

Postgres: Retrieve first n words from column

I know that I can do a text search in Postgres with TextSearch and get some result with
select ts_headline('german',content, tq, 'MaxFragments=4, MinWords=5, MaxWords=12,
ShortWord=3, StartSel = <strong>, StopSel = </strong>') as highlight, ...
FROM to_tsquery('german', 'test') tq ...
Is there a similar way to apply to content the same limitations? i.e. to get directly up to 12 words from the column content.
You could use regular expressions:
SELECT (regexp_match(
regexp_replace(content, '[^\w\s]+', ' ', 'g'),
'^\s*((?:\w+\s+){9}\w+)'
))[1] FROM ...
That will first replace everything that is not a space or alphanumerical character with a space and then return the first 10 words.

How to order by a string of numbers separates by dot

I have a query for run over the tree structure, using a CTE way.
The problem is that I use one column called camino for ORDER BY clause. This column is get like that:
---
UNION ALL
SELECT rel.codpadre, rel.codhijo, rel.canpres, depth+1, camino || ''.'' || CAST(rel.posicion AS text) , rel.posicion
FROM ---
This one :
camino || ''.'' || CAST(rel.posicion AS text
gives me a column like that:
0.0.1
0.0.10
0.0.1.0
0.0.1.0.0
0.0.10.0
0.0.1.0.1
0.0.10.1
0.0.2
.......
I need to order by those column, but considering 10 after 9, no after 1.
You can convert the string to an array of integers:
order by string_to_array(camino, '.')::int[]
or
order by string_to_array(concat_ws('.', camino, posicion), '.')::int[]

How to concatenate 2 or more columns in beamSql

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.

How to fetch a part of string upto a chracter?

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.