How to get all the string before an empty space in postgres? - sql

So basically I need to get a part of the postcode string, all before an empty space
my current query is
select postcode, left(postcode, length(postcode) - strpos(' ', postcode))
from postcodetable
But it just doesn't return the post code correctly, example:
1st column is NW1 1AA, 2nd column should just be NW1 but instead it just repeats the first column

Your arguments to strpos() are in the wrong order. So you can do:
select postcode, left(postcode, length(postcode) - strpos(postcode, ' '))
from (values ('NW1 1AA')) v(postcode);
You can also do this using substring() with regular expressions:
select postcode, substring(postcode from '[^ ]+'::text)

Related

Select the first word on the left of a string in snowflake

I have a column (mycolumn) in my snowflake table (mytable) whose content has this pattern :
JohnDoe - Client Number One
MaryJane - Client Number Two
I would need to extract the first portion on the left of the string (JohnDoe,MaryJane - with no whitespace behind).
I tried to use the following approach, but I got stucked because I could only remove the first two block of words to the right, but not the - (dash) and the white spaces.
select substring(mycolumn,1,length(mycolumn)- CHARINDEX(' ', REVERSE(mycolumn))- CHARINDEX(' ', REVERSE(mycolumn))) from mytable
You can use regexp_substr():
select regexp_substr(mycolumn, '^[^ ]+')
from mytable;

How to remove all hyphens and spaces from a string?

I've got the following code:
INSERT INTO DWCUST (DWCUSTID, DWSOURCEIDBRIS, DWSOURCEIDMELB, FIRSTNAME, SURNAME, GENDER, PHONE, POSTCODE, CITY, STATE, CUSTCATNAME)
SELECT dwcustSeq.nextval, cb.custid, Null, cb.fname, cb.sname, UPPER(cb.gender), cb.phone, cb.postcode, cb.city, cb.state, cc.custcatname
FROM a2custbris cb
NATURAL JOIN a2custcategory cc
WHERE cb.rowid IN (SELECT source_rowid FROM A2ERROREVENT where filterid = 5);
I want to adjust it so that before cb.phone (varchar2) values are added to dwcust, all hyphens and spaces are removed from the strings so that they are just numeric.
For instance I want 04-1234 5254 to become 0412345254
Translate can be useful:
translate(cb.phone, 'X- ', 'X')
For example,
select translate(' 04-1234 5254', 'X- ', 'X')
from dual
gives:
TRANSLATE('04-12345254','X-','X')
---------------------------------
0412345254
1 row selected.
About this usage, Oracle docs says:
You cannot use an empty string for to_string to remove all characters
in from_string from the return value. Oracle Database interprets the
empty string as null, and if this function has a null argument, then
it returns null. To remove all characters in from_string, concatenate
another character to the beginning of from_string and specify this
character as the to_string. For example, TRANSLATE(expr,
'x0123456789', 'x') removes all digits from expr.
I would use regexp_replace()
select regexp_replace('04-1234 5254', '[- ]', '')
from dual;
This would still return something that is not a number if it e.g. contains a /
To make sure or to remove everything that is not a number use the following:
select regexp_replace('04-1234 5254', '[^0-9]', '')
from dual;
Just replace cb.phone with regexp_replace(cb.phone, , '[^0-9]', '') in your SELECT list.
SELECT ...., regexp_replace(cb.phone, , '[^0-9]', ''), ....
FROM a2custbris cb
....

substr Error -- ORA-01722:invaild number separate string

I am trying to separate first and last name . I have a column called 'Fullname' and it has first and last name and a comma all in one column. I've tried the below but I get an error " its not a valid number". When I remove the comma it works, so I am not sure how to incorporate a comma in the formula so it can work.
,substr(Fullname,1,',') as Lastname
,substr(Fullname,',',' ') as Firstname
Column
Fullname
Brown,John N
Green,Julie T
Desired results
Lastname FirstName
Brown John
Green Julie
You can use regexp_substr():
select regexp_substr(name, '[^,]+', 1, 1) as lastname,
regexp_substr(name, '[^, ]+', 1, 2) as firstname
The second argument to SUBSTR() is the position of the substring, the third argument is the length of the substring. It will not automatically search for a delimiter if you use strings there instead of numbers. You can use LOCATE() to find the positions that you want.
SUBSTR(Fullname, 1, LOCATE(Fullname, ',')-1) AS Lastname,
SUBSTR(Fullname, LOCATE(Fullname, ',')+1) AS Firstname
Can be performed in Classical way by using instr inside substr function as the following case :
select substr(fullname,1,instr(fullname,',')-1) Firstname,
substr(fullname,instr(fullname,',')+1,length(fullname)) Lastname
from tab;
SQL Fiddle Demo

Retrieve Second to Last Word in PostgreSQL

I am using PostgreSQL 9.5.1
I have an address field where I am trying to extract the street type (AVE, RD, ST, etc). Some of them are formatted like this: 5th AVE N or PEE DEE RD N
I have seen a few methods in PostgreSQL to count segments from the left based on spaces i.e. split_part(name, ' ', 3), but I can't seem to find any built-in functions or regular expression examples where I can count the characters from the right.
My idea for moving forward is something along these lines:
select case when regexp_replace(name, '^.* ', '') = 'N'
then *grab the second to last group of string values*
end as type;
Leaving aside the issue of robustness of this approach when applied to address data, you can extract the penultimate space-delimited substring in a string like this:
with a as (
select string_to_array('5th AVE N', ' ') as addr
)
select
addr[array_length(addr, 1)-1] as street
from
a;

Changing LastName,FirstName to LastName,FirstInitial

I'm sure this is super easy, but how would I go about converting LastName,FirstName to LastName,FirstInitial?
For example changing Smith,John to Smith,J or Johnson,John to Johnson,J etc.
Thank You!
In case of LastName and FirstName columns:
select LastName,substr(FirstName,1,1)
from mytable
;
In case of a fullname saved in a single column:
select substr(fullname,1,instr(fullname || ',',',')-1) || substr(fullname,instr(fullname || ',',','),2)
from mytable
;
or
select regexp_replace (fullname,'([^,]*,?)(.).*','\1\2')
from mytable
;
Here is one way, using just "standard" instr and substr. Assuming your input is a single string in the format 'Smith,John':
select substr(fullname, 1, instr(fullname, ',')+1) from yourtable;
yourtable is the name of the table, and fullname is the name of the column.
instr(fullname, ',') finds the position of the comma within the input string (it would be 6 in 'Smith,John'); thensubstrtakes the substring that begins at the first position (the1in the function call) and ends at the position calculated byinstr`, PLUS 1 (to get the first initial as well).