简体   繁体   中英

find upper case strings in SQL record or in Python

I need to find, in SQL or Python, the words inside a string that are written in upper case latters, if they are exist.

For example, for the string: 'My name is NOA' it would return NOA.

The long way I thought about is to use the ascii value: if 64 < chr(i) < 91 if 64 < chr(i+1) < 91 ....

You could use string_split for that.

Suppose you have this table:

create table phrases (
    phrase varchar(200)
);

insert into phrases values ('My name is NOA');

Then you can write:

select      phrase, value as uppercase_word
from        phrases
cross apply string_split(phrase, ' ')
where       upper(value) = value

With the given sample data it will return:

phrase         | uppercase_word
---------------+---------------
My name is NOA | NOA

Make sure to take note of this information in the documentation:

The STRING_SPLIT function is available only under compatibility level 130. If your database compatibility level is lower than 130, SQL Server will not be able to find and execute STRING_SPLIT function. You can change a compatibility level of database using the following command: ALTER DATABASE DatabaseName SET COMPATIBILITY_LEVEL = 130

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM