convert postgreSQL column values from CAPITAL to Camel Case - postgresql

I have two columns one have values in Camel case 'Costa Rica' which is correct but the another column is all capital 'COSTA RICA' , sometime its make problem for me while querying , Can I convert these Capital into camel case too as first column?

Use initcap() function to get this done.
to convert all values of second column having all capital letters use below mentioned query:
update your_table set second_col=initcap(second_col);
if you just want to compare the columns then use like below:
where first_col=initcap(second_col)
or
where upper(first_col)=upper(second_col)

Related

Google Cloud Dataprep - How to replace a pattern in a string

How can I replace a pattern in a string in one column with a value from another column in Cloud Dataprep?
To be precise, I have a column A with the same pattern in every string of the column, and I want to replace that pattern inside a string with corresponding value (when i say corresponding I mean the value in the same row) from another column.
Any idea?
I think you should be able to do this in two steps:
1.- Splitting the column (using the pattern in order to take it away)
2.- Merging the column (gathering the pieces together into the one desired column)

Postgresql : How can I determine how many characters in a text

a column has type text and its data looks like "{U}{R}" or "{3}{U}{U}{U}".
How can I determine how many "U" contains this column?
I want to select those data who has at least one and at most three {U}.
You can remove the code U and compare the size before/after removing it. This difference is the number of occurrence.
select length('{3}{U}{U}{U}{R}{R}')-length(translate('{3}{U}{U}{U}{R}{R}','U','')) AS U_CNT;
--> 3
or more generaly
select length(colname)-length(translate(colname,'U','')) AS U_CNT;

How to get one letter and one number from REGEX Oracle Sql query

Trying to get just "P1:" from this code but if I add numbers to the one it will output those as well. How do I restrict it to only take numbers 1-9 and have it display "NULL" if it's two digits?
select REGEXP_SUBSTR('P1:EMAIL', '[P]+\d+[:]') as test from dual;
You have included the + operator, which means match one or more of the character or class. If you want to match exactly one then you don't want that. You also don't need the square brackets:
select REGEXP_SUBSTR('P1:EMAIL', 'P\d:') as test from dual;
You might also want to anchor the pattern to the start of the string, but that isn't clear; if so then:
select REGEXP_SUBSTR('P1:EMAIL', '^P\d:') as test from dual;

Pulling a Substring which does not exist in the same position

I am looking to pull a substring from a Oracle database column using PL/SQL.
The column has a large string value which varies in size for different rows. Hence the substring I am referring to will not be in the same position for each row. But the substring is uniquely identifiable. It will be like ",RID!1455,". i.e. It will be preceded by a comma, have RID, followed by a !, followed by a number and then a comma.
I am interested in pulling this number followed by RID. Can you please help me with this. Thank you very much in advance
Consider below query:
Suppose table text1 has column text with string:
dfgggsdRID!3242dfgdfdg
Then below query will give you result '3242':
select substr(text, (select (INSTR(text, 'RID!', 1)+4) FROM text1),4) from text1;

How do I format a number of arbitrary length?

If I have data that includes a numeric column with values into the miillions (eg 63254830038), and I want to format the number as a US Dollar amount (eg. $63,254,830,038), I know I can use:
SELECT numeric_column, to_char(numeric_column, '$999G999G999G999') from table
to format the values, but to do so reliably I either have to include an unnecessarily long text string ('$999G999G999G999') or know the maximum number of possible digits. Is there a way to say, broadly, "group numbers with a comma" instead of explicitly saying "group the hundreds, group the thousands, Oh! and please group the millions"?
You just need cast integer to money type.
E.g.:
tests=> select cast(63254830038 as money);
Or alternative syntax:
tests=> select 6323254830038::money;
And output (I'm from Poland, so money type take my locales and set correct currency symbol):
money
----------------------
63.254.830.038,00 zł
Monetary Types documentation.
You can try something like this (works in sql-server, not sure about postgresql)
select convert(varchar,cast('63254830038' as money),1)
You could do things the hard way using regular expressions: convert the number into a string, reverse it, use regexp_replace to insert commas between pairs of 3 digits, and then reverse it again:
select '$' || reverse(regexp_replace(
reverse(numeric_column::varchar),
E'(\\d\\d\\d)(?=\\d)', '\1,', 'g'))
Explanation
The first argument to regexp_replace is the expression to match, which contains two parts:
(\\d\\d\\d) means 3 digits, which are captured
(?=\\d) is a positive lookahead constraint of a single digit, meaning the match only counts if there is a digit following it. (That is, this digit is checked to exist, but it does not count as part of the match.)
The second argument is what to replace with: the 3 captured digits, plus a comma.
The third argument 'g' is a flag indicating that it should match and replace as many times as possible.
For more information on regular expressions in PostgreSQL, see the documentation.