Special character not showing up in select query in SQL Server - sql-server

I have an question about special characters not showing up in the query
DECLARE #varName1 NVARCHAR(500);
set #varName1 = 'ÜCŞKUçÖ'
select '''' + #varName1 + ''' As Name1'
These are the Turkish characters. When I run the above query I get this character as S but it should come as Ş
In the database I have this column value as ÜCŞKUçÖ
How can I solve the issue?

You're not actually storing it as a Unicode value...
SET #varname1 = N'ÜCŞKUçÖ';
SELECT #varname1;

DECLARE #varName1 NVARCHAR(500);
set #varName1 = N'ÜCŞKUçÖ'
select #varName1 As Name
For more in details http://technet.microsoft.com/en-us/library/ms180059.aspx

Related

Remove a value from a comma separated string in sql stored procedure

I have a field that store the comma separated id's of publications. Such as 123456,2345678,123567,2345890 etc. When I pull the data from the database I put it into a json object and the web page loops the values and displays the data. Works great.
What I would like to do is to send the stored proc one of the numbers and the stored proc will remove it from the string and save it back to the table. Such as the end user worked on publication 123567 and now wants to make it completed, so I want to remove it from the string so they don't see it in the future. I have a split function in the database but I don't know how to wire it up to delete or rebuild the string without the publication ID.
I don't have any code to show because I am at a loss to start. I figure I need to pass the entire string and the ID. Split the string and loop each value to rebuild a new string but check if the ID is there and skip it.
Is this the best way to do this?
Thanks for your help
what I've ended up with as the base to work from is:
ALTER FUNCTION dbo.RemovePMID (
#S VARCHAR(MAX)
,#PMID VARCHAR(15)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE #T VARCHAR(50)
DECLARE #W VARCHAR(50)
SET #T = ''
WHILE len(#S) > 0
BEGIN
SET #W = left(#S, charindex(',', #S + ',') - 1)
IF charindex(#W, + #PMID) = 0
SET #T = #T + ',' + #W
SET #S = stuff(#S, 1, charindex(',', #S + ','), '')
END
RETURN substring(#T, 2, len(#T) - 2)
END
GO
No need for loops (please take a peek at Larnu's suggestion for your parse/split function)
That said, consider the following
Example
Declare #S varchar(max) = '123456,2345678,123567,2345890'
Declare #Zap varchar(50)='123456'
Select reverse(stuff(reverse(stuff(replace(','+#S+',',','+#Zap+',',','),1,1,'')),1,1,''))
Returns
2345678,123567,2345890

Not able to replace special character in nvarchar string

I am trying to split a string which is nvarchar(max)
if I pass the string like
#String_xyz = 'abc#def#ghi$jkl'
Then I am able to replace the special character but my character comes in a unreadable format like ?????
If I send my string like this way
#String_xyz = N'abc#def#ghi$jkl'
Then I am not able to replace any special character
Let say
DECLARE #string nvarchar(max)
SET #string = 'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது'
SET #string = replace(#string,'##',' ') -- This work perfect
if
DECLARE #string nvarchar(max)
SET #string = N'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது'
SET #string = replace(#string,'##',' ') -- This will not work
Please let me know any possible solution
where abc-def-ghi-jkl are multilanguage character
Collation : SQL_Latin1_General_CP1_CI_AS
The point is UNICODE
You have to make sure, that you use unicode in all places
Literal strings must be started with an N everywhere
Try this
--This will come out with question marks
SELECT 'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது';
--And this is the correct output
SELECT N'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது';
--Here I replace one of the characters with a "%"
SELECT REPLACE(N'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது',N'கி',N'%')
This works fine here...
UPDATE
Cannot verify this anymore, but it might be, that the output was wrong at the first try. I tried around with several collations. With this I got the wanted
SELECT N'YourString' COLLATE Indic_General_90_BIN;
After having used this, it was OK. So - but this is just guessing - it might be, that SQL Server had to learn this first...
If your code is like
DECLARE #string nvarchar(max) = N'என்$பெயர்#PIN#ஆகிறது##என்
SET #string = replace(#string,'##',' ')
results as
என்$பெயர்#PIN#ஆகிறது என்$பெயர்#KUL#ஆகிறது
if
DECLARE #string1 nvarchar(max) = 'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது'
SET #string1 = replace(#string1,'##',' ')
then
???$?????#PIN#?????? ???$?????#KUL#??????
if
DECLARE #string2 varchar(max) = 'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது'
SET #string2 = replace(#string2,'##',' ')
then
???$?????#PIN#?????? ???$?????#KUL#??????
if
DECLARE #string3 varchar(max) = N'என்$பெயர்#PIN#ஆகிறது##என்$பெயர்#KUL#ஆகிறது'
SET #string3 = replace(#string3,'##',' ')
then
???$?????#PIN#?????? ???$?????#KUL#??????
if you declares string as nvarchar you have to give N' before the string otherwise unicodes will not work.

Declaring a value and executing it cannot be used in the same stored procedure?

I'm stuck on an error in MS SQL Server.
I'm making a stored procedure in which I declare a value, I set it as a select statement, and execute it. Then I want to use it later in the same stored procedure, but It still sees it as a select statement, and not as a result of the statement.
Declaring:
Declare #functie varchar(255);
Set #functie = 'Select Functie From Medewerker m, Logins l where m.mdwnr = l.mdwnr And gebruikersnaam = ''' + #GebruikersNaam + ''''
EXECUTE(#functie)
Print #functie
Comparing it afterwards (does not work):
If '+ #functie + ' = ''Administrator''
Print ' + #functie + '
The prints are used to debug, and are not necessary.
How can I fix this?
You don't need dynamic sql here at all. Here is your query setting the value of the variable.
Declare #functie varchar(255);
Select #functie = Functie
From Medewerker m
join Logins l on m.mdwnr = l.mdwnr
And gebruikersnaam = #GebruikersNaam
Also, you should get in the habit of using ANSI-92 style joins instead of the out of data comma separated list of tables. The "newer" join syntax is nearly 30 years old now.
Bad habits to kick : using old-style JOINs
You can get data from EXEC only to the table, then you can parse that info depending on your requirements. Here is possible example of that:
DECLARE #sql nvarchar(255) = 'Select ''test'''
DECLARE #result TABLE (functie nvarchar(100))
DECLARE #functie nvarchar(100)
INSERT INTO #result EXECUTE(#sql)
SET #functie = (SELECT TOP(1) functie FROM #result)
PRINT(#functie)
However previous comment is perfect, you don't need Dynamic SQL for this operation, and parametrized query may save your ass in case of SQL Injection (which looks possible in your code).
The prior answer my resolve your issue - I wanted to note also that your comparison code is incorrect - using two single quote characters in a row resolves to one single quote character (rather than being treated as a text delimiter).
If you will be getting single quotes back as part of your result, then that code should look like:
If '' + #functie + '' = '''Administrator'''
Print #functie

How to store non english unicode characters using dynamic query in SQL Server

I want to save non-english unicode characters into a table in SQL Server.
declare #cSql varchar(200)
set #cSql = ''
declare #cCode varchar(10)
set #cCode = '001'
declare #cKName Nvarchar(200)
set #cKName = N'ಒಂದು'
Insert into DepMast (omCode, omKName)
Values(#cCode, #cKName)
On executing the above code, the unicode characters are saved as required.
But, if I use dynamic query, '?????' is stored.
set #cSql = 'Insert into DepMast (omCode, omKName)
Values(''' + #cCode + ''',''' + #cKName + ''')'
exec(#cSql)
Any ideas on storing unicode characters using dynamic query?
Two reasons for this behaviour - firstly, you have declared #cSql as varchar datatype and #cKName as nvarchar datatype. Secondly, you are not prefixing the value with N when inserting. This should fix the issue
declare #cSql nvarchar(200) --changed datatype to nvarchar
set #cSql=''
declare #cCode varchar(10)
set #cCode='001'
declare #cKName nvarchar(200)
set #cKName=N'ಒಂದು'
set #cSql='Insert into DepMast (omCode,omKName)
Values('''+#cCode+''',N'''+#cKName+''')' -- added the "N" prefix
exec(#cSql)
Use Insert Into Select Statement in dynamic query.You need to store your Unicode characters in some reference table with key value.

Saving sql query that contains arabic into a cell

declare #val nvarchar(max),#H_ARABIC nvarchar(max)
select #val = 'select [settings_key] as N''اسم'' from [settings]'
set #H_ARABIC= #val;
print #H_ARABIC
It is showing results as
select [settings_key] as N'???' from [application_setting] but how can I get this result
select [settings_key] as N'اسم' from [application_setting] I have tried many ways by changing the quotation mark but no use. Pls help
Prefix your string literal with N.
declare #val nvarchar(max),#H_ARABIC nvarchar(max)
select #val = N'select [settings_key] as N''اسم'' from [settings]'
set #H_ARABIC= #val;
print #H_ARABIC

Resources