SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert comma to single quotes


Convert comma to single quotes

Author
Message
sqlfriends@sql.com
sqlfriends@sql.com
SSC Veteran
SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)

Group: General Forum Members
Points: 250 Visits: 280
Hi Experts:

This is my table


Condition Columnname
akraft,crunckel TN
AL,AZ State
Atlanta,Austin-San Marcos MAC
aney,aventura Area
Alexandria, VA,Arlington, TX Market
Alpharetta,Alexandria City
001,002 StoreS
SameStore,HeitmanI Store Type
InLast6Months,CS-CC Space Types



I want to convert the column like as

Condition Columnname
'akraft','crunckel' TN
'AL','AZ' State
'Atlanta','Austin-San Marcos' MAC
'aney','aventura' Area
'Alexandria', 'VA','Arlington', 'TX' Market
'Alpharetta','Alexandria' City
'001','002' Stores
'SameStore','HeitmanI' StoreType
'InLast6Months','CS-CC' SpaceTypes



To add the single quotation to start and End of the position .. any one help to achieve this goal

Thanks
FAJ
SomewhereSomehow
SomewhereSomehow
Old Hand
Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)

Group: General Forum Members
Points: 388 Visits: 469
Try using replace function like this:
declare @a varchar(100) = 'akraft,crunckel';
set @a = ''''+replace(@a,',',''',''')+''''
select @a




I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
Blog: http://somewheresomehow.ru
Twitter: @SomewereSomehow
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12564 Visits: 5478
SomewhereSomehow (8/17/2012)
Try using replace function like this:
declare @a varchar(100) = 'akraft,crunckel';
set @a = ''''+replace(@a,',',''',''')+''''
select @a



The above will not produce exact expected results for some of the cases:

Alexandria, VA,Arlington, TX



will be converted to

'Alexandria',' VA','Arlington',' TX'



and if I'm right the OP wouldn't want trailing spaces in ' VA' and ' TX'...

so, to make it closer to the expected one he can do:



declare @a varchar(500)
set @a = 'Alexandria, VA,Arlington, TX'
set @a = replace(''''+replace(@a,',',''',''')+'''',''','' ',''',''')

select @a




_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
SomewhereSomehow
SomewhereSomehow
Old Hand
Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)

Group: General Forum Members
Points: 388 Visits: 469
The above will not produce exact expected results for some of the cases:

Yes, but in general your solution won't too. Imagine a few more spaces at the end or begining.
declare @a varchar(500)
set @a = ' Alexandria , VA , Arlington , TX '
set @a = replace(''''+replace(@a,',',''',''')+'''',''','' ',''',''')
select @a


If we are talking about timming spaces or doing smth else with the elements, it would be better to split string, do manipulations and concat it back.


I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
Blog: http://somewheresomehow.ru
Twitter: @SomewereSomehow
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12564 Visits: 5478

...
If we are talking about timming spaces or doing smth else with the elements, it would be better to split string, do manipulations and concat it back.


or use well known way of removing duplicate spaces (http://www.sqlservercentral.com/articles/T-SQL/68378/) with one more check...


declare @a varchar(500)
set @a = ' Alexandria , VA , Arlington , TX '
set @a = REPLACE(''''+REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(@a)),' ',' '+CHAR(7)),CHAR(7)+' ',''),CHAR(7),''),' , ', ','),',',''',''')+'''',''','' ',''',''')
select @a




_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
SomewhereSomehow
SomewhereSomehow
Old Hand
Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)Old Hand (388 reputation)

Group: General Forum Members
Points: 388 Visits: 469
Good point!


I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
Blog: http://somewheresomehow.ru
Twitter: @SomewereSomehow
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41819 Visits: 20007
sqlfriends@sql.com (8/17/2012)
Hi Experts:

This is my table


Condition Columnname
akraft,crunckel TN
AL,AZ State
Atlanta,Austin-San Marcos MAC
aney,aventura Area
Alexandria, VA,Arlington, TX Market
Alpharetta,Alexandria City
001,002 StoreS
SameStore,HeitmanI Store Type
InLast6Months,CS-CC Space Types



I want to convert the column like as

Condition Columnname
'akraft','crunckel' TN
'AL','AZ' State
'Atlanta','Austin-San Marcos' MAC
'aney','aventura' Area
'Alexandria', 'VA','Arlington', 'TX' Market
'Alpharetta','Alexandria' City
'001','002' Stores
'SameStore','HeitmanI' StoreType
'InLast6Months','CS-CC' SpaceTypes



To add the single quotation to start and End of the position .. any one help to achieve this goal

Thanks
FAJ



Do you have any control over the design of this table? Your request suggests that you do...

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
sqlfriends@sql.com
sqlfriends@sql.com
SSC Veteran
SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)

Group: General Forum Members
Points: 250 Visits: 280
Hi Experts;


Good Thanks for all support

ChrisM : we don't have using any control to design this table.

Thanks
FAJ
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41819 Visits: 20007
sqlfriends@sql.com (8/17/2012)
Hi Experts;


Good Thanks for all support

ChrisM : we don't have using any control to design this table.

Thanks
FAJ


What you have there is an EAV (entity-attribute-value) table. Simple-talk (site currently down) have an excellent discussion of the pitfalls of this design.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Dheeraj Gadave
Dheeraj Gadave
SSC-Enthusiastic
SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)

Group: General Forum Members
Points: 161 Visits: 152
Tnx It worked for me perfectly.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search