Trim white space from array values - postgresql

My records in the table are as follows:
id column1
1 'Record1'
2 ' Record2'
3 ' Record3a, Record3b'
4 'Record4a , Record4b, Record4c '
column1 type: text
pre-defined array= {record1,record2,record3a}
While I'm checking the values with a pre-defined array using && operator, most of the values are missed because of the delimiter space between those which are unnecessary.
Hence I need to first remove these space that are there in beginning or end (only) and then do string_to_array() so that the result could be compared to my pre-defined array

Use trim() to remove leading a trailing whitespace:
SELECT string_to_array(trim(both ' ' from regexp_replace(column1, '\s*,\s*', ',')), ',')
FROM yourTable

SELECT string_to_array(trim(both ' ' from regexp_replace(column1,
'\s*,\s*', ',')), ',') FROM yourTable
It works, but the 'g' flag should be added to remove all whitespaces:
SELECT string_to_array(
trim(both ' ' from regexp_replace(column1, '\s*,\s*', ',')), ',','g')
FROM yourTable

Related

I have Product Table and which has PRODUCTID Column which is Int I want all ProductID's in a single Row separated by comma

I want all the product ID extracted from product table to single Row separated by a Comma . Each Product ID Separated by comma
PRODUCTID
6578,7657,65836,84947,8464..... SO ON
Assuming you are using MS SQL Server you should be able to achieve your goal with the following code.
SELECT STUFF((
SELECT ', ' + CAST(PRODUCT_ID as VARCHAR(20))
FROM PRODUCT_TABLE
FOR XML PATH('')
), 1, 1, '')
With MSSQL Server 2017 and newer you could also use STRING_AGG().
SELECT STRING_AGG(CAST(PRODUCT_ID as VARCHAR(MAX)), ', ') FROM PRODUCT_TABLE

get till end of string after first space

I need to split a string as follows, when I try with with split_part no luck
select split_part('8 HAMPSHIRE RD',' ',2)
Expected output: HAMPSHIRE RD
A cheaper solution without a regular expression:
SELECT substring (
'8 HAMPSHIRE RD'
FROM position(' ' IN '8 HAMPSHIRE RD') + 1
);
Use regexp_replace():
select regexp_replace('8 HAMPSHIRE RD', '.*?\s', '');
regexp_replace
----------------
HAMPSHIRE RD
(1 row)
An alternative solution using string manipulation functions:
with my_table(str) as (
values ('8 HAMPSHIRE RD')
)
select right(str, -strpos(str, ' '))
from my_table;
If you want to skip the first word if it contains only digits you should use \d (digit) instead of . (any char):
select regexp_replace('8 HAMPSHIRE RD', '\d*?\s', '');

Extract string of dynamic length when the indicator of completion exists in multiple instances. Postgres

So if I have a varchar length string column let's call ID(samples below):
97.128.39.256.1460854333288493
25.365.49.12.13454154815132
346.45.156.354.1523425161233
I want to grab, like a left in excel, everything to the left of the 4th period. How do i create a dynamic string to find the fourth instance of a period?
I know substring is a start but not sure how to write in the dynmic length that exists
This is probably the easiest for someone else to read:
select split_part(i, '.', 1) || '.' ||
split_part(i, '.', 2) || '.' ||
split_part(i, '.', 3) || '.' ||
split_part(i, '.', 4)
from (select '97.128.39.256.1460854333288493' as i) as sub;
Or if you don't like split_part and prefer to use arrays:
select array_to_string((string_to_array(i, '.'))[1:4], '.')
from (select '97.128.39.256.1460854333288493' as i) as sub;
I think the array example is a bit harder to grasp at first glance but both work.
Updated answer based on revised question to also convert the Unix timestamp to a Greenplum timestamp:
select 'epoch'::timestamp + '1 second'::interval *
(split_part(i, '.', 5)::numeric/1000000) as event_time,
array_to_string((string_to_array(i, '.'))[1:4], '.') as ip_address
from (
select '97.128.39.256.1460854333288493' as i
) as sub;
You could also try this:
mydb=> select regexp_replace('97.128.39.256.1460854333288493', E'^((?:\\d+\\.){3}\\d+).+$', E'\\1');
regexp_replace
----------------
97.128.39.256
(1 row)
Time: 0.634 ms
with t (s) as ( values
('97.128.39.256.1460854333288493'),
('25.365.49.12.13454154815132'),
('346.45.156.354.1523425161233')
)
select a[1] || '.' || a[2] || '.' || a[3] || '.' || a[4]
from (
select regexp_split_to_array(s, '\.')
from t
) t (a)
;
?column?
----------------
97.128.39.256
25.365.49.12
346.45.156.354

Padding zeros and remove commas, decimals, and dashes in tsql

I have an amount field and a commission field that I need to remove the comma: , decimal point: . dash: - and the percent sign: %.
I have tried replicate, format, replace and stuff,
right ('000000000')
right('000000000') + rtrim(field), len#)
RTRIM(replicate('0', 9 - len(field)) + REPLACE(REPLACE(REPLACE(cast(field as varchar), ',', ''), '.',''), '-', ''))
RTRIM(replicate('0', 9 - len(t.Commission_Amount)) + REPLACE(REPLACE(REPLACE(cast(t.Commission_Amount as varchar(9)), ',', ''), '.',''), '-', ''))
but I never get the results that I want. When I use replace it replaces the comma, dash, or % but cuts the field short and does not pad to the left with zeros. I know it's probably right in front of my face I just need some clarity please.
00-126.47 comes out as 0012647
0.00 comes out as 00000000
000126.47 comes out as 00012647
Try this:
create sample table
DECLARE #Table as table (
field varchar(15)
)
populate sample table
INSERT INTO #Table VALUES
('00-126.47'),
('0.00'),
('000126.47'),
('00033%2.422')
select
SELECT field As before,
RIGHT(REPLICATE('0', 9) +
REPLACE(
REPLACE(
REPLACE(
REPLACE(field, '-', '')
, '.', '')
, ',', '')
, '%', '')
, 9) As [After]
FROM #Table
results:
before After
--------------- ---------
00-126.47 000012647
0.00 000000000
000126.47 000012647
00033%2.422 000332422
this solved my problem;
RIGHT(REPLICATE('0', 9) + REPLACE(REPLACE(REPLACE(REPLACE(field, '-', ''), '.', ''), ',', ''), '%', ''), 9) As [After]

query to get part of a string

column1
\\abc\tri\eds\rf1\edr\4ed
\\f.d\tri\ef\poe
\\ghi0j\tri\gf\rf\k\hg\ose
'
'
'
i got some rows like that in a column
now i want to get the result set like
\\abc\tri\eds
\\f.d\tri\ef\
\\ghij\tri\gf
simply from first '\' to end of 4th '\'
In this week's episode of Horrible Code I Have Written For StackOverflow...
SELECT
SUBSTRING(column1, 1,
CHARINDEX('\', column1,
CHARINDEX('\', column1,
CHARINDEX('\', column1, 3) + 1) + 1))
FROM
TableName