question

aRookieBIdev avatar image
aRookieBIdev asked

check if all characters in string are same

How to check if all the characters in a string are the same ? Thanks Kannan
sql-server-2008string-function
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered
Search for a pattern where any other character is not like the first one? declare @string varchar(50) set @string = 'aaaabaaaa' select @string, case when patindex('%[^'+left(@string,1)+']%',@string) = 0 then 'All same' else 'Not all same' end
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image
Grant Fritchey answered
The simplest way to do that is using the equals comparison. You'll know right away if two strings are different. That won't work well for large text objects.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Cyborg avatar image
Cyborg answered
Another method

declare @string varchar(50)
set @string = 'aaaaaaaa'

SELECT CASE WHEN LEN(REPLACE(@STRING, LEFT(@String,1),'')) = 0 
THEN 'All Same' ELSE 'NOT ALL SAME' END
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.