Using STUFF FUNCTION to Remove Some Characters with T-SQL


 CREATE TABLE table1 (COLOMN1 VARCHAR(50))

INSERT INTO table1
VALUES     ('abcd'),
            ('abcd'),
            ('ab'),
            (NULL),
            ('')

SELECT 
/*

--Msg 537, Level 16, State 2, Line 62
--Invalid length parameter passed to the LEFT or SUBSTRING function.
LEFT(COLOMN1,Len(COLOMN1) - 3)

*/

COALESCE(Stuff(COLOMN1, Len(COLOMN1) - 3, 3, ''), COLOMN1) as COLOMN1

FROM   TABLE1

DROP TABLE TABLE1 
 

 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s