I have a column called 'uri' which has values as follows:
file://c:\file1.txt
file://\\1.1.1.1\folder1\folder1a\file2.txt
file://d:\sub1\sub2\sub3\file3.txt
I'm trying to remove the filename from the uri to leave
file://c:\
file://\\1.1.1.1\folder1\folder1a\
file://d:\sub1\sub2\sub3\
You can use regexp_replace:
with t (col) as (
select 'file://c:\file1.txt' union all
select 'file://\\1.1.1.1\folder1\folder1a\file2.txt' union all
select 'file://d:\sub1\sub2\sub3\file3.txt'
)
select regexp_replace(col, '(^.*?)[^\\]*$', '\1') as output from t;
Produces:
output
---------------------------------
file://c:\
file://\\1.1.1.1\folder1\folder1a\
file://d:\sub1\sub2\sub3\
Demo
Related
I have a case driven query . Below is the simplest form
select Column 1 from mytable
Results :
Column 1
latinnametest
LatinManual
LatinAuto
Is it possible to show the aggregated data of column 1 data of all the resulted rows in another Column say column 5 in front of each row with comma separated ?
Expected :
Column 1 Column 2
latinnametest latinnametest,LatinManual,LatinAuto
LatinManual latinnametest,LatinManual,LatinAuto
LatinAuto latinnametest,LatinManual,LatinAuto
I have used array_agg and concat() but it aggregates the same row data in column 2 but not as expected to add all rows column data comma separated . Any help please.
Edit :
I have tried the solution mentioned below but I am getting repetitive data in the column . see the screenshot. I have hover the mouse over that last column and see the repetitive data . Any solution to this ?
[![enter image description here][1]][1]
You can use string_agg() as a window function:
select column_1,
string_agg(column_1, ',') over () as all_values
from the_table;
Edit, after the scope was changed:
If you need distinct values, use a derived table:
select column_1,
string_agg(column_1, ',') over () as all_values
from (
select distinct column_1
from the_table
) t;
Alternatively with a common table expression:
with vals as (
select string_agg(distinct column_1, ',') as all_values
from the_table
)
select t.column_1, v.all_values
from the_table t
cross join vals v
Below have the xml framing fields,
**Xml_value=
<docs><doc_id>1234</doc_id>
<reci><reci_code>ss</reci_code>
</reci></docs>**
Select doc.value('doc_id[1]',varchar(8)')doc_id,
Reci.value('reci_code'[1],char(8)')reci_code
From xml_value.nodes('docs/doc')docs(doc)
cross apply docs.nodes('reci/reci')reci'(reci')
Output:
doc_id reci_code
1234 ss
The above mentioned query is for SQL server.
need to extract all XML fields into a separate columns as like above output statement in db2.
How to achieve this in db 2.
can you help on this.
Try this:
/*
WITH MYTABLE AS
(
SELECT T.*
FROM
(
VALUES XMLPARSE
(
DOCUMENT '
<docs>
<doc_id>1234</doc_id>
<reci>
<reci_code>ss</reci_code>
</reci>
</docs>
'
)
) T(X)
)
*/
SELECT V.DOC_ID, V.RECI_CODE
FROM
MYTABLE T
, XMLTABLE
(
'$D/docs/reci' PASSING T.X AS "D"
COLUMNS
DOC_ID INT PATH '../doc_id'
, RECI_CODE VARCHAR(10) PATH 'reci_code'
) V;
|DOC_ID |RECI_CODE |
|----------|----------|
|1234 |ss |
For example, where the element is 'hi', and where N is 3, I need a PostgreSQL snippet I can use in a SELECT query that returns the following array:
['hi', 'hi', 'hi']
Postgres provides array_fill for this purpose, e.g.:
SELECT array_fill('hi'::text, '{3}');
SELECT array_fill('hi'::text, array[3]);
The two examples are equivalent but the 2nd form is more convenient if you wish to replace the dimension 3 with a variable.
See also: https://www.postgresql.org/docs/current/functions-array.html
You may use array_agg with generate_series
select array_agg(s) from ( values('hi')) as t(s) cross join generate_series(1,3)
Generic
select array_agg(s) from ( values(:elem)) as t(s) cross join generate_series(1,:n)
DEMO
sql demo
with cte as (
select 'hi' as rep_word, generate_series(1, 3) as value
) -- ^^^ n = 3
select array(SELECT rep_word::text from cte);
I'm trying to split up an arrayed column that was created using an array_agg. the following is severely cut down, original query is something like 210 lines
select
distinct on (visitor.id)
id,
array_agg(distinct item.code::text)
from
8xfullouter joins
where
exists(
select
distinct on(visitor.id)
item.code::text
from
3 full outer join that all appear in the first query
group by
visit.id,
item.code
order by
visit.id
)
I need to break the array_agg(distinct item.code::text) into multiple columns. I've tried split_part(array_agg(distinct item.code::text), ',', 1) but received the following
> [Err] ERROR: function split_part(character varying[], unknown,
> integer) does not exist LINE 159: split_part (array_agg(distinct
> "public".procedure_group_cpt_...
thanks!!!
Use an index of the array, e.g.:
select (array_agg(code::text))[1]
from (values ('code1'), ('code2')) as codes(code)
array_agg
-----------
code1
(1 row)
Alternatively, you can use string_agg() instead of array_agg(), e.g.:
select split_part(string_agg(code::text, ','), ',', 1)
from (values ('code1'), ('code2')) as codes(code)
split_part
------------
code1
(1 row)
I have a data in the field as " Date: 03-21-13 12/13/14/15 Date:04-21-13 39/12/34/14 Date:04-19-13 19/45/65/12 ".How to sort this data inside the field based on the recent Date.
It should Look like
Date:04-21-13 39/12/34/14
Date:04-19-13 19/45/65/12
Date: 03-21-13 12/13/14/15
Because you are storing it as text, you cannot correctly sort directly on the column (as you appear to have discovered). You will need to split the column, and then sort on that. Something like:
Declare #tvTable Table (
TextColumn varchar(max)
)
Insert #tvTable
Select '04-19-13 19/45/65/12'
Union All
Select '04-21-13 39/12/34/14'
Union All
Select '03-21-13 12/13/14/15'
Union All
Select '03-25-13 17/18/19/20'
Union All
Select '05-01-13 99/88/77/66'
Union All
Select '02-01-13 11/22/33/44'
Select t.TextColumn
From #tvTable t
Cross Apply dbo.fncDelimitedSplit8k(TextColumn, ' ') split
Where split.ItemNumber = 1
Order By Cast(split.Item As DateTime) Desc
The split function taken from Jeff Moden Tally OH!