Not able to replace special character in nvarchar string - sql-server

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.

Related

How can I replace two characters in a single word with wildcard if the two characters are included in wild card definition in MSQL

declare #SearchString varchar(150) = 'Banana'
set #SearchString = replace(#SearchString,'a','[abc]');
set #SearchString = replace(#SearchString,'b','[abc]');
select #SearchString
Result will be
#SearchString = [abc][a[abc]c]n[a[abc]c]n[a[abc]c]
what I want is
[abc][[abc]c
How can I do that ?
I found another way to do that be comparing the the replacements on both sides like below
Declare #Keyword varchar(50)='أحمد'
Select name from names where Replace(name,'أ','ا') = replace(#keyword,'ا','أ')
that will get me 'أحمد' and 'احمد'

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

Single quotes in variable in T-SQL

I have a condition where user will pass comma separated values like
0071061386,0071061387
it will be passed to a variable which will feed the values into a dynamic query with two single quotes.
passed by user
declare #s nvarchar(max) = '0071061386,0071061387'
it should be like after converting the user values so that I can pass the values into dynamic query
declare #s nvarchar(max) = '''0071061386'',''0071061387'''
A simple replace with probably do the trick here, but I beleive this to be an XYProblem. Perhaps you better explain the problem leading you to go this path in the first place.
declare #s nvarchar(max) = '0071061386,0071061387'
SELECT '''''' + REPLACE(#s, ',', ''''',''''') + ''''''
Result:
''0071061386'',''0071061387''
Try this
DECLARE #S NVARCHAR(MAX) = '0071061386,0071061387'
SELECT REPLACE(''''''''+#S+'''''''',',',''''',''''')

SQL Server: how to remove leading 'A's from base64 string

I'm working on SQL Server and trying to create a single key combining data from bigint and string columns. To minimize the size of bigint's represented as strings I'm using Base 64 encoding. The problem is that the result includes leading 'A's meaning base64 zeroes and it increases the size of the resulting field. What is the way to remove these leading A using T-SQL or XQuery?
Sample code:
DECLARE #binInput VARBINARY(MAX)
SET #binInput = CAST(123 AS VARBINARY(MAX))
SELECT CAST(N'' AS XML).value('xs:base64Binary(sql:variable("#binInput"))', 'varchar(max)')
I have a resulting AAAAew== where I would prefer to see just ew because of the idea is to make the final string as short as it possible and base64 string should be shorter than base10.
update 1: as were suggested by Richard Boyce I tried to convert bigint to the equal string, but it gives null as a result of base64 conversion
declare #input bigint
declare #varInput nvarchar(max)
set #input = 123
set #varInput = cast(cast(#input as varbinary(max)) as varchar(max))
select CAST(N'' AS xml).value('xs:base64Binary(sql:variable("#varInput"))', 'varchar(max)')
update 2: the current solution is to get a base64binary string and to remove leading 'A's and trailing '='s. It's not perfect, so any suggestions are welcome. Actual code:
declare #input bigint
set #input = 1234567890
declare #output varchar(max)
set #output = (select cast(#input as varbinary(max)) for xml path(''),binary base64)
set #output = replace(ltrim(replace(#output,'A',' ')),' ','A') -- remove leading 'A's
set #output = replace(#output,'=','') -- remove trailing '='s
select #output
Rather than trying to remove leading "A's" from the encoded result look at preventing them in the first place.
You need to convert your number to a string before encoding.
Try this
DECLARE #binInput VARBINARY(MAX)
SET #binInput = CAST(CAST(123 AS VARCHAR(MAX)) AS VARBINARY(MAX))
DECLARE #Result VARCHAR(MAX)
SELECT #Result = CAST(N'' AS XML).value('xs:base64Binary(sql:variable("#binInput"))', 'varchar(max)')
SELECT CAST(CAST(N'' AS XML).value('xs:base64Binary(sql:variable("#Result"))', 'varbinary(max)') AS VARCHAR(MAX))
Note that when encoded "123" becomes "MTIz" not ew==
Not wanting to leave an answer that doesn't answer the question, here's how to remove a number of leading A's from a string (but the answer #Richard gave is better):
DECLARE #VAL NVARCHAR(MAX) = N'AAAA12345ABCD9876=='
SELECT SUBSTRING(#VAL,PATINDEX('%[^A]%',#VAL), LEN(#VAL))
----------------
12345ABCD9876==

Mistake in Sql query while splitting string

What mistake I am making in this sql query
Declare #str Varchar(100) = 'asasa,bsasas,csasa,dsczxvvc'
declare #d varchar(2)=','
SELECT
RIGHT(LEFT(#str,Number-1),
CHARINDEX(#d,REVERSE(LEFT(#d+#str,Number-1))))
FROM
master..spt_values
WHERE
Type = 'P' AND Number BETWEEN 1 AND LEN(#str)
AND SUBSTRING(#str,Number,1) = #d
Expected Result
(No column name)
asasa
bsasas
csasa
dsczxvvc
Actual Result
(No column name)
asasa
bsasas
csasa
Your AND SUBSTRING(#str, Number, 1) = #d is forcing a comma to be at the end of dsczxvvc...there isn't one. asasa,bsasas,csasa,dsczxvvc, works.
add one more comma at the end of string
you can directly add comma in the string like = "asasa,bsasas,csasa,dsczxvvc,"
or
handle this thing in sql side.
Declare #str Varchar(100)
set #str = 'asasa,bsasas,csasa,dsczxvvc'
declare #d varchar(2)
set #d =','
set #str = #str + ','
SELECT
RIGHT(LEFT(#str,Number-1),
CHARINDEX(#d,REVERSE(LEFT(#d+#str,Number-1))))
FROM
master..spt_values
WHERE
Type = 'P' AND Number BETWEEN 1 AND LEN(#str)
AND SUBSTRING(#str,Number,1) = #d
This is because since the last string portion does not have the seperator at the end of the string
If you add the following code just before the SELECT statement, it will work
set #str = #str + #d
There are many split functions on the web, you can use one of them actually.
Split string using CLR function
Split using XML
SQL Server split string function

Resources