I have codes in a text column, for example, e12312312 E123123123 need to write sql query who make every value in a column with capital E my database is postgresql
You can use Postgres string function initcap() to turn the first character of a string to upper case and the rest of the string to lower case : since the rest of your strings is made of digits only, this should work:
select initcap('e12312312')
If your string may contain other upper case letters than you do not want to lower, then you can use left(), right() and upper():
select upper(left('e12312312ABC', 1)) || right('e12312312ABC', -1);
Related
I am trying to remove template text like &#x; or &#xx; or &#xxx; from long string
Note: x / xx / xxx - is number, The length of the number is unknown, The cell type is CLOB
for example:
SELECT 'H'ello wor±ld' FROM dual
A desirable result:
Hello world
I know that regexp_replace should be used, But how do you use this function to remove this text?
You can use
SELECT REGEXP_REPLACE(col,'&&#\d+;')
FROM t
where
& is put twice to provide escaping for the substitution character
\d represents digits and the following + provides the multiple occurrences of them
ending the pattern with ;
or just use a single ampersand ('&#\d+;') for the pattern as in the case of Demo , since an ampersand has a special meaning for Oracle, a usage is a bit problematic.
In case you wanted to remove the entities because you don't know how to replace them by their character values, here is a solution:
UTL_I18N.UNESCAPE_REFERENCE( xmlquery( 'the_double_quoted_original_string' RETURNING content).getStringVal() )
In other words, the original 'H'ello wor±ld' should be passed to XMLQUERY as '"H'ello wor±ld"'.
And the result will be 'H'ello wo±ld'
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
Suppose I have a varchar variable.Can I insert different symbols after evry 2 character of the string untill end of the string .i.e untill length (string).
For example:
Input: '12345678'
And we don't know what is input and length of input and the output we want is :
Output: '12_34&56#78' (special character can be anything )
Please let me know if it possible doing dynamically using loop or something.
This can be done using a simple REGEXP_REPLACE.
The first argument will be your original string.
The second argument will be your regex pattern you want to put a a character after. In this case it is . (any character) occurring 2 times. It is surrounded by parentheses which will be needed for the next argument to specify a capture group.
The third argument is using \1 to specify the first capture group, then you can put anything you'd like to appear after the capture group. In the example below I used a !.
SELECT REGEXP_REPLACE ('12345678', '(.{2})', '\1!') as str FROM DUAL;
STR
_______________
12!34!56!78!
If you do not want the character at the end of your string, you can TRIM it from the right side or SUBSTR if the special character may appear at the end of the original string.
You can use an hierarchical query during the split and padding a special character through use of DBMS_RANDOM.VALUE in order to produce some special characters except for the last piece, and then LISTAGG() function to combine the pieces back again such as
SELECT id,LISTAGG( SUBSTR(col,1+(level-1)*2,2)||
CASE WHEN level < CEIL(LENGTH(col)/2)
THEN
CHR(TRUNC(DBMS_RANDOM.VALUE(33,48)))
END)
WITHIN GROUP (ORDER BY level)
AS result
FROM t
GROUP BY id
CONNECT BY level <= CEIL(LENGTH(col)/2)
AND PRIOR SYS_GUID() IS NOT NULL
AND PRIOR ID = ID
Demo
I have a field that is a string but should be mostly numbers. I need to be able to find if a letter is in this string. The letter can be in any spot in the string.
You can use:
select t.*
from t
where regexp_like(field, '[^0-9]');
That is, return any row where field has a non-digit.
I am trying to figure out some commands/code in SQL.
I have database with names, addresses IDs etc, but I have to convert firstname values ending in “jnr” to “(Jnr)” and those ending in “snr” to “(Snr)”.
How do I do this?
update table TABLE_NAME set NAMES = '*xyz*Jnr' where NAMES like '%jnr'
Update or select:
PASTE(column, CHAR_LENGTH(column)-3, 1, UPPER(SUBSTRING(column FROM CHAR_LENGTH(column)-3 FOR 1)
WHERE column LIKE '%jnr' OR column LIKE '%snr'
PASTE is used to put in one character at position 3 from end,
CHAR_LENGTH to get length of column value,
UPPER converts character to upper case,
SUBSTRING is used to pick one character here (j or s),
LIKE is used to find values ending with jnr, or snr.
All ANSI SQL (no dbms specified!)