Split words with a capital letter in PostgreSQL - sql

I have sene Split words with a capital letter in sql which is applicable to MS SQL but I was wondering how to achieve the same using PostgreSQL
Basically I'm getting values such as FirstNameValue but I need it to be First Name Value
Unfortunately I don't know where to even start. I got to the following and got stuck straight away
SELECT REGEXP_REPLACE('ThoMasTest', '[^ ][A-Z].', ' ')
The result from a string such as ThoMasTest should be Tho Mas Test
Thanks

This should do the trick:
select regexp_replace('ThoMasTest', '([a-z])([A-Z])', '\1 \2','g');
The expression matches two characters next to eachother, each one in its own group:
[a-z] that matches a lowercase letter.
[A-Z] finds a capital letter
So if one lowercase if immediately followed by a capital letter insert a space between them.
Do that globally 'g'.

Related

First 3 letters in caps and remaining letters in small case in a name -SQL

i have question, In a name (eg. Richard) first 3 letters should be in capital letter and remaining letters should be in lower case.
ANS: RIChard
can you help me to get the query for this?
Microsoft SQL Server has UPPER() and LOWER() functions that could change the characters to upper case and lower case.
for your demand, you need to use UPPER for your first 3 letters:
you can use the left() or substring functions to get the first 3 letters.
and for the remaining letters, you need to use the LOWER function.
for splitting the remaining letters you need to use the right or substring functions plus Len() function to calculate the remained letter counts.
Select UPPER(Left(Name,3)) + LOWER(right(Name,len(Name)-3))
OR
Select UPPER(substring(Name,1,3)) + LOWER(substring(Name,4,len(Name)))
What you could do is the following if you want to update it:
UPDATE employees SET First_name = CONCAT(UPPER(LEFT(First_name,3)), LOWER(SUBSTRING(First_name,4)))
You can test it here
If you want to only have it in a select you can use:
SELECT CONCAT(UPPER(LEFT(First_name,3)), LOWER(SUBSTRING(First_name,4))) as First_name FROM employees;
You can test it here.
What I'm doing in both cases is the following:
Get the first 3 characters, and convert it to uppercase
Get all the other characters, and convert it to lowercase
Concatenate the two string together
// Try this:
SELECT UPPER(LEFT('richard',3))+LOWER(SUBSTRING('richard',4));
You can do like this
SELECT UPPER(LEFT('richard',3))+LOWER(SUBSTRING('richard',4,LEN(richard')));
Explanation: Upper() is to capitalize the letter, as you can see, I use it with LEFT(), Left() will get the 3 letters from the left, 3 means the number of character that you want. The substring() will extract the remaining letters, starting from the forth

Regexp_Like to Validate Uppercase Characters [A-Z] and Numbers [0-9] Only

I would like a query using regexp_like within Oracle's SQL which only validates uppercase characters [A-Z] and numbers [0-9]
SELECT *
FROM dual
WHERE REGEXP_LIKE('AAAA1111', '[A-Z, 0-9]')
List item
The select Statement probalby should look like
SELECT 'Yes' as MATCHING
FROM dual
WHERE REGEXP_LIKE ('AAAA1111', '^[A-Z0-9]+$')
Which means that starting from the very first ^ to the last $ letter every character should be upper case or a number. Important: no comma or space between Z and 0. The + stands for at least one or more characters.
Edit: Based on the answer of Barbaros another way of selecting would be possible
SELECT 'Yes' as MATCHING
FROM DUAL
WHERE regexp_like('AAAA1111','^[[:digit:][:upper:]]+$')
Edit: added a DBFiddle
A quick help may be found here and for oracle regular expressions here.
You can use :
select str as "Result String"
from tab
where not regexp_like(str,'[[:lower:] ]')
and regexp_like(str,'[[:alnum:]]')
where not regexp_like with POSIX [^[:lower:]] pattern stands for eliminating the strings
containing lowercase,
and regexp_like with POSIX [[:alnum:]] pattern stands for accepting the strings
without symbols
( containing only letters and numbers even doesn't contain a space because of the trailing space at the end part of [[:lower:] ] )
Demo

Find phone numbers with unexpected characters using SQL in Oracle?

I need to find rows where the phone number field contains unexpected characters.
Most of the values in this field look like:
123456-7890
This is expected. However, we are also seeing character values in this field such as * and #.
I want to find all rows where these unexpected character values exist.
Expected:
Numbers are expected
Hyphen with numbers is expected (hyphen alone is not)
NULL is expected
Empty is expected
Tried this:
WHERE phone_num is not like ' %[0-9,-,' ' ]%
Still getting rows where phone has numbers.
from https://regexr.com/3c53v address you can edit regex to match your needs.
I am going to use example regex for this purpose
select * from Table1
Where NOT REGEXP_LIKE(PhoneNumberColumn, '^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$')
You can use translate()
...
WHERE translate(Phone_Number,'a1234567890-', 'a') is NOT NULL
This will strip out all valid characters leaving behind the invalid ones. If all the characters are valid, the result would be NULL. This does not validate the format, for that you'd need to use REGEXP_LIKE or something similar.
You can use regexp_like().
...
WHERE regexp_like(phone_num, '[^ 0123456789-]|^-|-$')
[^ 0123456789-] matches any character that is not a space nor a digit nor a hyphen. ^- matches a hyphen at the beginning and -$ on the end of the string. The pipes are "ors" i.e. a|b matches if pattern a matches of if pattern b matches.
Oracle has REGEXP_LIKE for regex compares:
WHERE REGEXP_LIKE(phone_num,'[^0-9''\-]')
If you're unfamiliar with regular expressions, there are plenty of good sites to help you build them. I like this one

Get the FIrst character from the Firstname if more than one firstname is present in oracle

I have a requirement where the persons can have more than one first name and i need to convert the name into first characters from the 2 or more names with capital letters.
Examples:
Srinivas Kalyan ,Sai Kishore
if the above is one having 4 first names then i need to display as below
S K S K
The comma is also be replaced and take all the first characters
2. Srinivas-Kalyan Sai Kishore
For the above name the value should be as
S S K
since Srinivas-Kalyan is considered as one name
Also the name can be in small letters
srinivas kalyan sai kishore
For this
S K S K
This has to be in oracle
Tried the below regex_replace which is working fine in sql developer but in the application it is changing into space
replace(trim(regexp_replace(to_char(regexp_replace(initcap(regexp_replace(regexp_replace
(FIRST_NAME, '[0-9]', ''), '( *[[:punct:]])', '')), '([[:lower:]]| )')), '(.)', '\1 ')),',',null)
The missing letter in your output is caused by this regex for character removal:
( *[[:punct:]])
This will turn Kalyan ,Sai into KalyanSai, which will be treated as one word by the rest of the process, and so you will not have the S of Sai in your output.
I would suggest this shorter expression:
trim(upper(regexp_replace(
regexp_replace(first_name, '([[:alpha:]])(-|[[:alpha:]])+', '\1'),
'.*?([[:alpha:]]|$)', ' \1'
)))
Explanation
([[:alpha:]])(-|[[:alpha:]])+ -> \1
This replaces any sequence of letters (or hyphen) with the first of those. So it reduces words to their initial letter.
.*?([[:alpha:]]|$) -> (space)\1
This replaces anything that precedes the next letter, with a space. As no letter will be skipped (because .*? is non-greedy) this effectively replaces all non-letter sequences with a space. To also replace the non-letters at the very end (which do not precede a letter), the special case $ (end-of-string) is added as alternative.
After these two steps there just remains to:
Upper case everything
Remove blanks at the start and end of the result with trim
I find the advantage of this method is that it does not use any other class than [[:alpha]]. Digits, punctuation, lowercase -- or whatever else -- does not need to be identified explicitly, as it is just the negation of [[:alpha]]. The only exception that has to be made, is for the hyphen.
As some names might include some other non-letters, like a quote, you might want to add such characters as well in the first regular expression.

In Postgres / SQL how can I search for names that doesn't start with a letter?

I want to find all the names that start with numbers, weird chars (.,-#$, etc) and everything else that isn't a letter.
For example, i have 3 names: John, #1 John and 2John. What I want to get is the last 2 names. (and I don't know what weird chars the names can start, so it must be something like ![a-Z])..
I'm using postgresql.
SELECT *
FROM Table
WHERE name ~ '^[^a-zA-Z]'
If accented or non-Latin characters don't fall under your definition of "weird stuff", you may use:
SELECT *
FROM Table
WHERE name ~ '^[^[:alpha:]]'
PostgreSQL Manual: Pattern Matching