Single quotes in variable in T-SQL - sql-server

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+'''''''',',',''''',''''')

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.

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

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

substring or rtrim

i have a field called LastWebpage, in the Field it stores the webpage .asp pagename
I have to write a script where i just need to take the jl_AssessmentDirect.asp name instead of 4000_1_jl_AssessmentDirect.asp. How do i get rid of 4000_1_. Please help.
Thanks.
Here are a couple of ways to do this, I assume the data is not always the same, this is just to show you how the functions work
right or replace is my preferred method
DECLARE #v VARCHAR(100)
SELECT #v = '4000_1_jl_AssessmentDirect.asp'
SELECT REPLACE(#v,'4000_1_','')
or
DECLARE #v VARCHAR(100)
SELECT #v = '4000_1_jl_AssessmentDirect.asp'
DECLARE #r VARCHAR(100)
SELECT #r ='4000_1_'
--if you have spaces RTRIM and LTRIM everything
SELECT RIGHT(#v,LEN(#v) -LEN(#r))
If stuff changes, you can dump it in variables
DECLARE #v VARCHAR(100)
SELECT #v = '4000_1_jl_AssessmentDirect.asp'
DECLARE #r VARCHAR(100)
SELECT #r ='4000_1_'
SELECT REPLACE(#v,#r,'')
Ideally you want to update such columns to only have the URL and nothing else
"It depends"
If always "4000_1_", use REPLACE as per SQLMenace's answer.
If a fixed length of any string, use SUBSTRING. No need to work out LENGTH first
SUBSTRING(MyValue, 8, 8000)
If the right hand side is fixed length and the unwanted part is variable length, use RIGHT
If both sides are variable, you'll need something like
SUBSTRING(MyValue, PATINDEX('%[_]1[_]%', MyValue) + 3, 8000)
Edit:
If you always want "jl"...
SUBSTRING(MyValue, PATINDEX('%jl%', MyValue), 8000)
Besides all the suggestions here already you can use stuff.
declare #s varchar(100)
set #s = '4000_1_jl_AssessmentDirect.asp'
select stuff(#s, 1, 7, '')
Remove characters before jl
declare #s varchar(100)
set #s = '4000_1_jl_AssessmentDirect.asp'
select stuff(#s, 1, charindex('jl', #s)-1, '')

Resources