Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

How to check if all characters of a string is same?

Jan 31 2011 1:35AM by Madhivanan   

Someone in the forums asked for finding out a string in which each character is same. Consider the following example

declare @t table(data varchar(20))
insert into @t
select '222222222222' as data union all
select '666666663466' union all
select 'aaaaaaaa'+CHAR(32) union all
select 'jjkjkhdg' union all
select '0----------' union all
select '9999999999999' union all
select ']##########' union all
select ']    ' union all
select ']]]]]]]]]]]]]'

As you see only three values have all same characters. You can use many methods to find out this but the simplest method is

select data from @t
where PATINDEX('%[^'+left(replace(data,']',char(0)),1)+']%',replace(data,']',char(0)))=0

The result is

data
--------------------
222222222222
9999999999999
##########

The logic is to see if there is a character which is different than the first character. If there is different character the patindex function will return a value greater than 0 otherwise all characters are same and it will return 0

Edit : As per the comment by Ramesh Saive, the code does not work well if the first character is ] (a closing sqaure bracket). So I have modified the code to handle that too

Tags: t-sql, sql_server, sqlserver, tsql, BRH, #TSQL, function, #SQLServer, patindex,


Madhivanan
3 · 39% · 12440
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

4  Comments  

  • Just to note that this approach fails when the first character is "]" (i.e. closing square bracket).

    The workaround is to replace such character with something like char(0) and then do the pattern search.

    commented on Feb 23 2011 7:34AM
    Ramesh Saive
    117 · 1% · 434
  • Thanks Ramesh Saive. Thanks for the testing. I have modified the code accordingly

    commented on Feb 24 2011 2:07AM
    Madhivanan
    3 · 39% · 12440
  • Replacing "]" with empty string will still have an issue for following set of data. And also you are not replacing the text in PATINDEX's data parameter.

    Sample data:

    declare @t table(data varchar(20)) insert into @t select '9999999999999' union all select '] ' union all select ']]]]]]]]]]]]]]'

    select data from @t where PATINDEX('%[^'+left(replace(data,']',''),1)+']%',data)=0

    Resolution: Try replacing it with char(0) or any other non printable character.

    select data from @t where PATINDEX('%[^'+left(replace(data,']',char(0)),1)+']%',replace(data,']',char(0)))=0

    commented on Feb 24 2011 4:40AM
    Ramesh Saive
    117 · 1% · 434
  • Thanks Ramesh Saive. I have modified the code.

    commented on Feb 24 2011 6:56AM
    Madhivanan
    3 · 39% · 12440

Your Comment


Sign Up or Login to post a comment.

"How to check if all characters of a string is same?" rated 5 out of 5 by 1 readers
How to check if all characters of a string is same? , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]