SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


find whether a upper case letter is there in a given string


find whether a upper case letter is there in a given string

Author
Message
from corporate
from corporate
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 43
Hi all,

i want to know whether a upper case letter is there in a given string or not?

if i provide 'Sarat' it needs to give me a flag value like 1
if i provide 'sarat' then it should give 0

else

from a given field in a table i want to retrieve only those rows doesnt contain any upper case letter in any position.

please let me know if you have answer.
Mark Marinovic
Mark Marinovic
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1009 Visits: 355
Here's a simple example that will render the result you want. Please note that this will only answer whether an A-Z character exists in the string (does not do anything for numbers or special characters in the string). There are other options such as using regexp within SQL Server for more customizable output.

DECLARE
@TestString VARCHAR(100)

SET @TestString = 'Sarat'

SELECT CASE WHEN BINARY_CHECKSUM(@TestString) = BINARY_CHECKSUM(LOWER(@TestString)) THEN 0 ELSE 1 END AS DoesContainUpperCase
GO



MJM
Jack Corbett
  Jack Corbett
SSC Guru
SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)

Group: General Forum Members
Points: 70264 Visits: 14946
Cool solution Mark. Not one I would have thought of. I'd have done something like this:


DECLARE @string VARCHAR(10)

SET @string = 'Sarat'

SELECT
CASE
WHEN @string LIKE '%s%' COLLATE Latin1_General_CS_AI THEN 'Lower Case s found'
WHEN @string LIKE '%S%' COLLATE Latin1_General_CS_AI THEN 'Upper Case S found'
ELSE 'No S found'
END



but I like yours better.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Piotr.Rodak
Piotr.Rodak
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4168 Visits: 1761
Actually, this is simpler I think:

DECLARE @string VARCHAR(10)

SET @string = 'Sarat'
SELECT
CASE
WHEN @string = lower(@string) COLLATE Latin1_General_CS_AI
THEN 'No upper case found' ELSE 'Upper Case found'
END



Regards

Piotr

...and your only reply is slàinte mhath
f7_cisco
f7_cisco
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 8
Just a quick clarification:

SQL may consider 'SARAT' , 'Sarat' or 'sarat' as equals; depending on the Collation;

which means that you should use "...BINARY_CHECKSUM(LOWER('SARAT')..." ;

if simply using "...LOWER('SARAT')...", then you should add "...COLLATE Latin1_General_CS_AI...".

"_CS_" stands for Case-Sensitive...; by defaut the Collation is "_CI_" which means Case In-sensitive;

Gr8 answers from all of you!

Cisco.
dwain.c
dwain.c
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28287 Visits: 6431
Another option:


DECLARE @t TABLE (Names VARCHAR(20))

INSERT INTO @t
SELECT 'sarat' UNION ALL SELECT 'Sarat' UNION ALL SELECT 'SaRaT' UNION ALL SELECT 'SARAT'

SELECT Names, CASE WHEN Names2=LOWER(Names2) THEN 1 ELSE 0 END
FROM @t CROSS APPLY (SELECT Names COLLATE Latin1_General_CS_AI) x (Names2)





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
po 35213
po 35213
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 1
A simple regular expression can do the job fine. I had to delete all records from my words table which begun with a capital letter. Here is what I used:

SELECT *
FROM `words`
WHERE `text` REGEXP BINARY '^[A-Z]'

replacing "SELECT" with "DELETE" worked well.

Theodore Pokama
Eugene Elutin
Eugene Elutin
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20718 Visits: 5478
po 35213 (2/27/2013)
A simple regular expression can do the job fine. I had to delete all records from my words table which begun with a capital letter. Here is what I used:

SELECT *
FROM `words`
WHERE `text` REGEXP BINARY '^[A-Z]'

replacing "SELECT" with "DELETE" worked well.

Theodore Pokama


It's not MySql forum...
There are no REGEXP function in T-SQL (as yet ;-))

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
shornick
shornick
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 8
"from a given field in a table i want to retrieve only those rows doesnt contain any upper case letter in any position."

Why not just do something simple like:

select * from myTable
where myField = lower(myField)

That would return the rows that do not have any uppercase letters in the field in question.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)

Group: General Forum Members
Points: 100526 Visits: 18172
shornick (10/29/2013)
"from a given field in a table i want to retrieve only those rows doesnt contain any upper case letter in any position."

Why not just do something simple like:

select * from myTable
where myField = lower(myField)

That would return the rows that do not have any uppercase letters in the field in question.


That actually would depend on your collation. If you are on a case insensitive collation (default) that query ignores upper case. In other words THIS = this.

You would need to modify your query slightly like this.


select * from myTable COLLATE Latin1_General_CS_AI
where myField = lower(myField)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search