Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Case-sensitive replace for strings on case-insensitive server.

Total Hit ( 1640)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


A sample script that demonstrates how to perform case-sensitive search & replace on a case-sensitive server. This script demonstrates some of the least known features of the T-SQL string functions.

Click here to copy the following block
DECLARE @SearchStr varchar( 30 ) , @SearchBin varbinary( 30 )
SELECT @SearchStr = 'AdministraciÓón' ,
    @SearchBin = CONVERT( varbinary( 30 ) , @SearchStr )
PRINT 'Original String : ' + @SearchStr
/*
Original String : AdministraciÓón
*/

SE @SearchStr = STUFF( @SearchBin ,
              CHARINDEX( CONVERT( binary(1) , 'Ó' ) , @SearchBin ) ,
              1 , CONVERT( binary(1) , 'O') )
PRINT 'After Replace #1: ' + @SearchStr
/*
After Replace #1: AdministraciOn
*/

SELECT @SearchBin = CONVERT( varbinary( 30 ) , @SearchStr )
SELECT @SearchStr = STUFF( @SearchBin ,
              CHARINDEX( CONVERT( binary(1) , 'ó' ) , @SearchBin ) ,
              1 , CONVERT( binary(1) , 'o') )
PINT 'After Replace #2: ' + @SearchStr
/*
After Replace #2: AdministraciOon
*/



-- Mimic behavior of REPLACE using a LOOP
DECLARE @SearchStr varchar( 30 ) , @SearchBin varbinary( 30 ) ,
    @CharBin binary( 1 )
SELECT @SearchStr = 'AdministraciÓón de Operación'
SELECT @SearchBin = CONVERT( varbinary( 30 ) , @SearchStr )
SELECT @CharBin = CONVERT( binary(1) , 'Ó' )
WHILE CHARINDEX( @CharBin , @SearchBin ) > 0
  SELECT @SearchBin = CONVERT( varbinary( 30 ) ,
           STUFF( @SearchBin ,
             CHARINDEX( @CharBin , @SearchBin ) ,
              1 , CONVERT( binary(1) , 'O') ) )

SELECT @CharBin = CONVERT( binary(1) , 'ó' )
WHILE CHARINDEX( @CharBin , @SearchBin ) > 0
  SELEC @SearchBin = CONVERT( varbinary( 30 ) ,
             STUFF( @SearchBin ,
             CHARINDEX( @CharBin , @SearchBin ) ,
              1 , CONVERT( binary(1) , 'o') ) )
SELECT @SearchStr = CONVERT( varchar( 30 ) , @SearchBin )
PRINT @SearchStr


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.