Wednesday, November 26, 2008

Replace NewLine in TSQL

It's quite frustrating how tiny task become so time-consuming and worrisome. Never would have imagined this hard to do. 

Had a situation where records gathered from an application will serve as input to a large application. The data first had to be cleaned (glad I got Sql server tools). This was done in seconds. On export to CSV, it was discovered that one of the fields (address field) had newline inputed in it causing wraping of the record as new line in the exported CSV.

Of course I was quick to type

update xxxxx set CustAddress = Replace(CustAddress, char(13), '')

Alas, no change. Kept getting same reponse. Even len(CustAddress ) = Replace(CustAddress, char(13), ''). 

Search for this issue on the net and could not find a direct linking article of blog reporting this scenerio. 

I finally got the answer from an article on Simple-talk by Robyn Page and Phil Factor

update xxxx set custaddress = REPLACE(custaddress, COALESCE(char(13) + char(10),'[^-a-z'']'), '')

Quite some other stunt in the article too.

No comments: