Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for ASCII values in sql server table

Tags:

sql

sql-server

I have table that one of the fields needs to be cleaned up. The basic table structure is as follows:

create table testComments(id int, comments text)

In the comments column some of the rows had to many "carriage returns" (char(13)) and "line feed" (char(10)). If there is more then one grouping per line I need to be able to modify it. The basic select statement that I have so far is a follows:

  select count(id)
  from testComments
  where comments like('%' +  char(13) + char(10) + char(13) + char(10) + '%')

This query will find the results

"This is a entry in the testComments crlf
crlf
In the comments field that works"

Although the query will not find the results if the comment is listed as follows:

"This is an entry in the testComments crlf
crlf
crlf
That will not work"

The query will only return a count of 1 entry for the above data. Any idea how I can change the query to return a count of 2?

like image 830
Chris Avatar asked Jul 02 '13 16:07

Chris


People also ask

How do I find ascii characters in SQL?

If you ever need to find the ASCII code for a given character when using SQL Server, the T-SQL ASCII() function is probably what you need. The ASCII() function returns the ASCII code value of the leftmost character of a character expression.

How do I return ASCII value in SQL?

SQL Server ASCII() Function The ASCII() function returns the ASCII value for the specific character.

What is an ASCII value SQL Server?

SQL Server ASCII() function overviewThe ASCII() function accepts a character expression and returns the ASCII code value of the leftmost character of the character expression.


1 Answers

Using the code you gave us, your query should work properly. So the details appear to be different, and I suspect that GolezTrol's comment is on the right track -- some of the CRLF pairs are really just solo CR or LF characters.

Try this:

select count(id)
  from #testComments
  where comments like('%' +  char(13) + char(10) + char(13) + char(10) + '%')
     or comments like('%' +  char(10) + char(10) + '%')
     or comments like('%' +  char(13) + char(13) + '%')
like image 170
John Fisher Avatar answered Nov 02 '22 11:11

John Fisher