SQLite Updating first letter to be upper case - sqlite

I have a field, customer.country
Iam trying to update it so that the first letter of the values in country are upper case. I don't seem to be able to find out a way of doing that.

UPDATE customer
SET country = UPPER(SUBSTR(country, 1, 1)) || SUBSTR(country, 2)

Why don't you use substr() to get the first letter and upper() it?
upper(substr(customer.country, 1, 1))||substr(customer.country, 2)

Related

How to extract specific string until blank space/next line from a text in Oracle?

I am trying to extract the following from the text field using Regrex in Oracle.
For example
"This is example,
and this really a example :h,j,j,j,j,
l //Updated question , as this letter is on the next line
now this is a disease:yes"
I am expecting a result as h,j,j,j,j,l, but if I use
REGEXP_SUBSTR(text_field,'example :[^:]+,') AS Result
I am getting example:h,j,j,j,j
But I am not getting the last letter 'l' like above and I am guessing that's because it's on the next line.Also, if I want the string "disease:yes" only, that will be so helpful as well. Thank you much!
The result you are getting is because your pattern includes the word 'example' and ends with a comma, leaving out the ending 'l'. Try this form instead. Note the example is shown using a Common table Expression (CTE). The WITH statement creates the table called tbl which just sets up test data, kind of like a temp table. This is also a great way to set up data when asking a question. This form of the REGEXP_SUBSTR() function uses a captured group, which is the set of characters after the string 'example:' until the end of that line in the multi-line field. From this you should be able to get the other string you are after. Give it a go.
WITH tbl(text_field) AS (
SELECT 'This is example,
and this really a example :h,j,j,j,j,l
now this is a disease:yes' FROM dual
)
SELECT REGEXP_SUBSTR(text_field,'example :(.*)', 1, 1, NULL, 1) AS Result
FROM tbl;
RESULT
-----------
h,j,j,j,j,l
1 row selected.
Edit based on new info. Since that last letter could be on it's own line, you'll need to allow for the newline. Use the 'n' flag to REGEXP_REPLACE() which allows the newline to match in the usage of the dot (match any character) symbol in regex. We switch to REGEXP_REPLACE as we'll need to return multiple capture groups. Here the WITH sets up 2 rows, one with an embedded newline in the data and one without. The capture groups are (going left to right) 1-the data after "example :" and ending in a comma, 2-the optional newline and 3-the next single character. Then replace the entire data with captured groups 1 and 3 (leaving out the newline).
NOTE this is very specific to the case of only 1 character on the following line.
WITH tbl(ID, text_field) AS (
SELECT 1, 'This is example,
and this really a example :h,j,j,j,j,
l
now this is a disease:yes' FROM dual UNION ALL
SELECT 2, 'This is example,
and this really a example :h,j,j,j,j,l
now this is a disease:yes' FROM dual
)
SELECT ID,
REGEXP_REPLACE(text_field, '.*example :(.*,)('||CHR(10)||')?(.).*', '\1\3', 1, 1, 'n') AS Result
FROM tbl;
ID RESULT
---------- ------------
1 h,j,j,j,j,l
2 h,j,j,j,j,l
2 rows selected.

SQLite replace() specified character and next character

I have a database with a Username column.
There are multiple section signs followed by numbers §# that format the name.
I have to make sure all names are unique, but I want to disregard the formatting character pairs.
I was going to use,
SELECT * FROM Users WHERE replace(lower(Username),'§%','') = 'name';
but I realized that would look for the percent sign and not act as a wildcard. I could really use some help.
Use a combination of INSTR and SUBSTR to isolate the name before comparing it:
SELECT *
FROM Users
WHERE LOWER(SUBSTR(Username, 1, INSTR(Username, '§%') - 1)) = 'name';

regexp_substr get last two words from end of the sentence in Oracle SQL

I have a string: ON P6B 0B8. The output I need is: P6B OB8.
I can use regexp_substr('ON P6B 0B8','[^ ]+$',1) to get the last word from the end of the sentence. But how would I get the word after the space—the second word from the end?
How do I tell regexp_substr to not stop at the first space when looking from behind, and instead move on until it hits the second space?
I had a tough time understanding the metacharacters provided by Oracle regexp.
Here's a regex that will get the last 2 sets of characters from your string. Since it appears you are getting a Canadian postcode though you may want to be a little more careful.
The WITH clause sets up a table with data. Notice the first row is a valid postcode format, but the second row is bad (2 letters in a row). Always use unexpected data for your test cases, you don't want any surprises and the data WILL always contain surprises.
The first regex matches 2 sets of 3 characters separated by a space at the end of the string. At first glance this may seem OK but if the data is bad it will get returned. To tighten it up, use the second regex, which specifically checks for the Canadian postcode format of uppercase_letter-digit-uppercase_letter-space-digit-uppercase_letter-digit and will return NULL if it is not found. Maybe you want to catch this with a NVL() call and return a message instead.
with tbl(str) as (
select 'Windsor ON P6B 0B8' from dual union all
select 'Windsor_bad_postcode ON A3C 9BB' from dual
)
select --regexp_substr(str, '.* (.{3} .{3})$', 1, 1, NULL, 1) postcode_w_bad
regexp_substr(str, '.* ([A-Z]\d[A-Z] \d[A-Z]\d)$', 1, 1, NULL, 1) postcode
from tbl;

cts:near query not working with special character

I have a document with element <word>42 case § 100</word>
I am searching for this using a near query since my requirement is to use it. This does not work when i use the below query,
cts:search(
//word,
cts:near-query(
(
cts:word-query("42", ("case-insensitive","diacritic-insensitive","punctuation-insensitive","lang=en"), 1),
cts:word-query("case", ("case-insensitive","diacritic-insensitive","punctuation-insensitive","lang=en"), 1),
cts:word-query("§", ("case-insensitive","diacritic-insensitive","punctuation-insensitive","lang=en"), 1),
cts:word-query("1*", ("case-insensitive","diacritic-insensitive","punctuation-insensitive","lang=en"), 1)
),
2,
("ordered"),
1
)
)
Same works when I use "§ 1*" together instead of splitting them.
§ is punctuation which is not included in the word index. You can change this by using custom tokenization overrides. See Search Developer's Guide for more details.
I think your distance (3rd param) is too small in this case. The distance is between all subqueries, so when looking for 4 ordered terms, you need a distance of 3.
HTH!

Sort Numbers with a Letter Suffix from an Access Database

I am attempting to retrieve some information from an Access database using an OleDbConnection. I am trying to Order the results By a column that contains a set of numbers in string format.
I wanted the results in a natural order (e.g. 1, 2, 10, 20 versus 1, 10, 2, 20), so I converted the data in the column of interest to integers and sorted the results.
"SELECT Drawing, Sheet FROM TableName ORDER BY CINT(Sheet) ASC"
This works fine, except in some cases when the table data has values with a letter suffix (e.g. 1A, 2B, etc...). The command above obviously fails for theses cases.
I would like the results sorted like so: 1, 2, 2A, 2B, 3, 3A, and so on...
So, how to go about this? I've seen some examples that use REGEXP and some conditional statements, but apparently MS SQL doesn't support REGEXP. So I'm stuck. Ideas would be appreciated.
There is no way to use a regular expression in a query run from outside an Access session. If the query is run inside an Access session, it could use a custom VBA function with RegExp. But a regular expression approach seems like over-kill for this situation anyway. You can get what you need simply with Val().
The Val Function will return a number from the digits in your string. It will stop reading the string when it hits a letter.
Here's an example from the Immediate window.
? Val("2A")
2
Use it in your query like this ...
SELECT Drawing, Sheet
FROM TableName
ORDER BY Val(Sheet), Sheet;

Resources