SQL Server CONVERT function doesn't convert '&' - sql-server

I have a sql table with a field varbinary(max) that contains string in it. I'm using varbinary because input in that field could potentially be larger than maximum char for nvarchar.
I'm trying to display the content of that field as a string by using
CONVERT(nvarchar(max), Folders)
The problem that I have is, if content has '&' it in it doesn't get converted properly.
It displays like that "Test & test".
Is there any other way to convert?

if the content could potentially be larger than the maximum of nvarchar, why do you want to convert it to nvarchar?
Try using
Replace(Convert(nvarchar(max), Folders), '&','').

Related

cast to NVARCHAR(MAX) causes "chinese"/UTF encoded characters

I am using code like this in my SELECT statement:
CAST(HASHBYTES(N'SHA1', Bla) AS NVARCHAR(MAX)) AS hashed_bla
and end-up with "chinese"/UTF encoded characters in the ssms grid but also in upstream apps. Is there a way to change this? Does this have to do with the collation? Thanks!
What you have is working as expected. Take the following example:
SELECT HASHBYTES('SHA1','B8187F0D-5DBA-4D43-95FC-CD5A009DB98C');
This returns the varbinary value 0xA04B9CB18A2DC4BC08B83FCCE48A0AF1A1390756. You are then converting that value to an nvarchar, so get a result like N'䮠놜ⶊ별레찿諤㦡嘇' (on my collation). For an varbinary each 4 characters represents a single character. So, for the above A04B is the first character (which is N'䮠').
It appears what you are after is an varchar representing a varbinary value (you don't need an nvarchar here, as there will be no unicode characters). To do so, you need to use CONVERT and a style code. For the example I gave above that would be:
SELECT CONVERT(varchar(100),HASHBYTES('SHA1','B8187F0D-5DBA-4D43-95FC-CD5A009DB98C'),1);
Which returns the varchar value '0xA04B9CB18A2DC4BC08B83FCCE48A0AF1A1390756'. If you don't want the '0x' at the start, use style code 2, rather than 1.

SQL server 2012: Make string fit Column Name limitations

In my stored procedure I use string entered by the user in the application to create column names in a table. The string has no limitations in the application. Is there a way (function maybe) to convert the string so that it fits the Column Name limitations? I know that I can do REPLACE to remove HTML and special chars but I am looking for a solution that it will somehow guarantees that the converted string can be placed as Column Name.
I dont want to limit the user in the UI because the entered string can be used in other parts of the application where for example HTML chars are important
Example: String edited by the user: Cat's <b>name</b> . I wont to be converted to a valid column name string.

Unicode issue in sql server, some characters cannot be saved in a varchar field

I'm dealing with unicode stuff in my DB. I have a data field defined as varchar(max),
and I'm preventing user to save unknown characters in this field, like "≤" for example (all unicode above U+00FF).
While doing so, I found that some characters if sent to be saved in this field would be displayed as "?", so I thought that all unicode characters above "U+00FF" will all be displayed like this, but then I found that "U+201B" which is "‛" is displayed "?" but the next character "U+201C" which is "“" is displayed as "“".
Can someone please explain to me why is that?
Update: Sorry if I was not clear, but I do not want to convert to nvarchar, I want to keep my field as varchar.
What I need to understand is why a character like "‛" is displayed as "?" in a "varchar" field while the next unicode character "“" is displayed properly?
If you want to store Unicode characters, you should use an nvarchar type, not varchar
You need to change your data type to nvarchar which will hold any unicode character where varchar is restricted to 8bit codepage.
For more information, read the accepted answer in this link below.
Difference between varchar and nvarchar

How to tell a field is nvarchar in CakePHP

I want to make sure that the strings I save to the database are properly formatted in UTF-8. I have converted some of the fields into nvarchar. To save the strings properly I need to prefix all strings with N. e.g. N'my string'.
However not all fields are necessarily nvarchar. In the DboSqlsrv class that I am using that extends DboSource, there is just the one string parameter that you can either specify as 'varchar' or 'nvarchar'.
Does anybody know if there is a 'cake' way to interrogate the database and check if a field is varchar or nvarchar?
this? http://book.cakephp.org/view/1066/_schema

blank spaces in sql server data

HI ALL,
I am using sql server express to store some data but it also store spaces with data. for example if a have a nchar(20) column in a table and i store "computer" (8 characters) to this column, then remaining character (20-8=12) is filled with blank spaces. Is there any way to over come this problem. Because when I shows this data to flow document (center alignment), then it produces alignment error.
Thanks for help
You can use the NVARCHAR data type instead. The NVARCHAR type is a variable length data type and will only store the actual data.
If you don't have control over the data types then you'll need to trim off any extra characters manually. In T-SQL you can do this with the RTRIM command.

Resources