July 3, 2007 at 12:45 am
Hi guys,
I have this output (abc,xyz,kty).
How can i add quotes around every value?
Thank you
July 3, 2007 at 1:00 am
Have a look at the QUOTENAME function. Something like this
Select QUOTENAME ('Markus','"')
Markus
[font="Verdana"]Markus Bohse[/font]
July 3, 2007 at 1:17 am
Thank you, but in my case i don't have a string as the original values.
I tried and got Invalid column name error
July 3, 2007 at 2:14 am
you have to place the values within quotes i hope you have omitted the quotes inthe quotename command.
Cheers,
Sugeshkumar Rajendran
SQL Server MVP
http://sugeshkr.blogspot.com
July 3, 2007 at 3:36 am
Select quotename(ContactName,'"') from customers
Select quotename(Orderid,'"') from orders
[font="Verdana"]Markus Bohse[/font]
July 3, 2007 at 5:31 am
Just a suggestion...
When someone gives you an example... don't just take it at 100% face value... it's just an example. When someone suggests something new to you, like QUOTENAME, take a little time to look it up in Books Online and see what it really does. Learning something new does takes a little effort on your part...
--Jeff Moden
Change is inevitable... Change for the better is not.
July 3, 2007 at 8:19 am
If you think i don't research then you are mistaken.I don't take anything for granted.
It doesn't work in my case because the values are not coming from the table , they are passing as a variable in the stored procedure. But anyway thanks for your help, no more questions.
July 3, 2007 at 11:55 am
I'd like to see your final solution if you don't mind sharing it with the world. It may help someone else in the near future who is in the same situation as you are (were).
July 3, 2007 at 5:15 pm
Sure, this is what i used:
declare @List VARCHAR(100)
set @List = 'abc,xyz,kty'
set @List = '''' + REPLACE(REPLACE(@List, '''', ''''''), ',', ''',''') + ''''
print @List
July 3, 2007 at 7:10 pm
> I have this output (abc,xyz,kty).
Can you tell from where?
You know, it's too late to apply quotation when the string is built.
If any value has a comma in it you gonna split it and lose it if you do quotation the way you did it.
_____________
Code for TallyGenerator
July 4, 2007 at 11:45 am
Serqiy is correct... is the stuff you putting into @List available in a table anywhere? If so, we can resolve the whole table instead of using RBAR methods...
--Jeff Moden
Change is inevitable... Change for the better is not.
July 5, 2007 at 8:56 am
Those values passed as a string (user types those values comma separated inside the text box) from application and then i suppose to search the database for the records passed.
July 5, 2007 at 9:16 am
So, why you need quotes?
_____________
Code for TallyGenerator
July 5, 2007 at 9:42 am
So why don't you convert the list of values from your application into a table and then use this through an inner join?
See http://databases.aspfaq.com/database/how-do-i-simulate-an-array-inside-a-stored-procedure.html for details - my preference would be to use a function to create the table of values from the list:
CREATE FUNCTION dbo.FAQ_CommaSeparatedListToSingleColumn
(
@cslist VARCHAR(8000)
)
RETURNS @t TABLE
(
Item VARCHAR(64)
)
BEGIN
DECLARE @spot SMALLINT, @STR VARCHAR(8000), @sql VARCHAR(8000)
WHILE @cslist <> ''
BEGIN
SET @spot = CHARINDEX(',', @cslist)
IF @spot>0
BEGIN
SET @STR = LEFT(@cslist, @spot-1)
SET @cslist = RIGHT(@cslist, LEN(@cslist)-@spot)
END
ELSE
BEGIN
SET @STR = @cslist
SET @cslist = ''
END
INSERT @t SELECT @STR
END
RETURN
END
GO
J
July 5, 2007 at 7:56 pm
Would have been a bit helpful to know that up front.
First, whatever you do... do NOT use dynamic SQL for this because you are allowing the user to type whatever they want and you will surely be made to suffer from an SQL Injection attack.
If you'd like, post the code you ended up with in your stored procedure and let us take a look both from a security aspect and a performance aspect.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply