Enter key issue in SQL - sql

I have a column that is defined as a nvarchar(100).
Sometimes office users press enter key wrongly and it causes problems in some applications. When I check the value it seems like double space character in SQL Server. Now my mission is finding the records that has this situation. I tried the code below to find the problematic records:
DECLARE #NewLineChar AS CHAR(2) = CHAR(10) + CHAR(13);
SELECT *
FROM TBLCASABIT
WHERE CARI_ISIM LIKE '%'+CHAR(10) + CHAR(13)+'%'
but it doesn't return the records I need.
If I use double space character I can see the test record I put and some other records has double space character. So I can't be sure if other records have the same situation or not.
How can I handle this situation? Any help will be appreciated.

I believe you are getting the order of CHAR(10) and CHAR(13) backwards. Here is what each of these characters is:
CHAR(13) - carriage return, or \r
CHAR(10) - line feed, or \n
In Windows, \r\n is a line ending, while in Unix \n by itself is interpreted as a line ending. So you should be searching for CHAR(13) followed immediately by CHAR(10), and not the other way around. Try the following query:
SELECT *
FROM TBLCASABIT
WHERE CARI_ISIM LIKE '%' + CHAR(13) + CHAR(10) + '%'
References:
MSDN Social
DOS vs. Unix Line Endings

Related

Identify Hidden Characters

In my SQL tables I have text which has hidden characters which is only visible when I copy and paste it in notepad++.
How to find those rows which has hidden characters using SQL Server queries?
I have tried comparing the lengths using datalength and len
it did not work.
DATALENGTH(name) AS BinaryLength != LEN(name)
I want the row which has hidden characters.
On the assumption that this is being caused by control characters. Some of which are invisible. But also include tabs, newlines and spaces. An example to illustrate and how to get them to appear.
--DROP TABLE #SillyTemp
DECLARE #InvisibleChar1 NCHAR(1) = NCHAR(28), #InvisibleChar2 NCHAR(1) = NCHAR(30), #NonControlChar NCHAR(1) = NCHAR(33);
DECLARE #InputString NVARCHAR(500) = N'Some |' + #InvisibleChar1 +'| random string |' + #InvisibleChar2 + '|' + '; Thank god Finally a normal character |' + #NonControlChar + '|';
SELECT #InputString AS OhNoInvisibleCharacters
DECLARE #ControlCharRange NVARCHAR(50) = N'%[' + NCHAR(1) + '-' + NCHAR(31) + ']%';
CREATE TABLE #SillyTemp
(
input nvarchar(500)
)
INSERT INTO #SillyTemp(input)
VALUES (#InputString),(N'A normal string')
SELECT #ControlCharRange;
SELECT input FROM #SillyTemp AS #SI WHERE input LIKE #ControlCharRange;
This produces 3 results. A string with invisiblechars within them like such:
Some || random string ||; Thank god Finally a normal character |!|
Note, the are actually invisible inside SQL. But stackoverflow shows them as such. The output in SQL Server is simply.
Some || random string ||; Thank god Finally a normal character |!|
But these characters still have a corresponding (N)CHAR(X) value. (N)CHAR(0) is a NULL character and is highly unlikely to be in a string, in my setup to detect them it also provides some problems in building a range. (N)CHAR(32) is the ' ' space character.
The way the [X-Y] string operator works is also based on the (N)CHAR numbers. Therefore we can make a range of [NCHAR(1)-NCHAR(31)]
The last select goes through the temporary table, one which has invisible characters. Since we're looking for any NCHARS between 1 and 31, only those with invisible characters (and often invalid characters or tabs/newlines) satisfy the where condition. Thus only they get returned. In this case only the 'faulty' string gets returned in my select statement.

How to hard code spaces at end of result?

I need to add spaces to the end of a column I have called NPI (nvarchar(20)). NPI is always 10 digits, but the requirements for the report want the first 10 digits to be the NPI followed by 10 spaces (text file formatting issues I assume). I have tried the following:
cast([NPI] as nvarchar(20)) + ' '
cast([NPI] as nvarchar(20)) + Space(10)
However, the result set does not change. It just shows the 10 digit NPI and spaces aren't included.
It sounds like you are actually using SQL Server instead of MySQL. VARCHAR() is for variable length strings and it will trim end whitespace. Cast instead to char/nchar for the desired output. It won't look like it in SSMS, so check datalength to confirm nchar(20) = 20 bytes * 2 for unicode.
SELECT CAST([NPI] AS NCHAR(20)) AS formattedNPI,
DATALENGTH(CAST([NPI] AS NCHAR(20))) AS confirmation
FROM your_table
This seems to work. The '*' is added to show that spaces are present..
print cast([NPI] as nchar(20)) + '*'
Here are a couple of other cheesy ways to add padding...
print substring([NPI] + ' ',1,20) + '*'
print [NPI] + space(20 - len([NPI])) + '*'
Add the space inside the cast
cast([NPI] + ' ' as nchar(20))
You're right #dfundako, I was fooled by my editor.
This works but I am using MariaDb (MySql) so it's maybe not relevant now.
select concat([NPI] , ' ')

Search for ASCII values in sql server table

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?
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) + '%')
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?
I think you're misunderstanding something basic.
The COUNT-Function returns the count of all returned rows. In your example it is still returning one row.
So no matter if you have this:
"This is an entry in the testComments crlf
crlf
That will not work"
Or this:
"This is an entry in the testComments crlf
crlf
crlf
crlf
crlf
That will not work"
COUNT will still return 1! (If you have one record that has this string)
EDIT: If you literally want to count the characters for each row, here you go:
SELECT LEN(REPLACE(myColumn, 'yourCharacter', '')) FROM ...
Try using a virtual table. I added up the differences in length of the comments field and the comments field after replacing the characters that you are looking for with empty strings.
SELECT SUM(DIF) FROM (
select
(
LEN(comments)
- LEN( REPLACE( REPLACE ( comments, char(13), ''),char(10),'') )
)
AS DIF
from #testComments
) as VT;

New line in TSQL

I am getting records from database as comma separated. I am getting contact
titles as:
greg w.workcerf, ashir ali, asdddfgjk
This is comma separated has been defined in SQL function getCommaListTitle()
What i want is to get these record on new lines as
greg w.workcerf,
ashir ali,
asdddfgjk
Any idea about what should i use in sql function instead of ','
Append after the comma in getCommaListTitle, CHAR(13) + CHAR(10) for a
new line
CHAR(13) is a new line char and CHAR(10) is a line feed.
See http://msdn.microsoft.com/en-us/library/ms187323.aspx
You should do this in your front end, like on data access layer or may be at presentation layer because your application could be any one a web app or a window app and in both there's different in new line syntax like in web we use <br/> tag whereas in window we use /n.
use the replace function
replace(field, ',', ',' + char(13)+char(10)
...however DO NOT do this in your database, database is about DATA and of course it 'should' be presented in some form... but starting with a line break, and finally you'll end with something like:
SELECT #s = '<tr><td>' + firstname + '</td><td>' + substr(lastname, 1, 30) + '</td></tr>'
FROM ....
RETURN '<table>' + #s + '</TABLE>'
and that is not to route to choose grasshopper

How to save and Print new line character using SQL server 2008

I am trying to save data to the database with Multiline textbox.
and another process is to read from the database and prints to a text file.
My problem is:
1) when I am trying to save the data in the database as new line character its taking unusally number of spaces between two words.
Please can any body help me out how to insert new line character in to the database.
thanks
Char(13) is a carriage return and Char(10) is a line feed.
so...
UPDATE tblEXAMPLE
SET Contents=#FIRSTLINE + chAr(13) + chAr(10)
+ #secondline + chAr(10) + chAr(13)
+ #lastline
WHERE KEY=#PRIMARYKEy