August 17, 2012 at 4:15 am
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
August 17, 2012 at 4:29 am
Try using replace function like this:
declare @a varchar(100) = 'akraft,crunckel';
set @a = ''''+replace(@a,',',''',''')+''''
select @a
August 17, 2012 at 4:56 am
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
August 17, 2012 at 5:32 am
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.
August 17, 2012 at 6:04 am
...
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
August 17, 2012 at 6:25 am
Good point!
August 17, 2012 at 6:38 am
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...
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
August 17, 2012 at 8:20 am
Hi Experts;
Good Thanks for all support
ChrisM : we don't have using any control to design this table.
Thanks
FAJ
August 17, 2012 at 8:34 am
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.
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
October 9, 2013 at 4:24 am
Tnx It worked for me perfectly.
October 9, 2013 at 6:59 pm
Here's another way that doesn't worry much about how many spaces are included.
WITH SampleData (Condition, Columnname) AS
(
SELECT 'akraft,crunckel','TN'
UNION ALL SELECT 'AL,AZ','State'
UNION ALL SELECT 'Atlanta,Austin-San Marcos','MAC'
UNION ALL SELECT 'aney,aventura','Area'
UNION ALL SELECT 'Alexandria, VA,Arlington, TX','Market'
UNION ALL SELECT 'Alpharetta,Alexandria','City'
UNION ALL SELECT '001,002','StoreS'
UNION ALL SELECT 'SameStore,HeitmanI','Store Type'
UNION ALL SELECT 'InLast6Months,CS-CC','Space Types'
),
SplitStrings AS
(
SELECT *
FROM SampleData
CROSS APPLY dbo.PatternSplitCM(Condition, '[, ]')
)
SELECT Condition, ColumnName
,NewCondition=
(
SELECT CASE [Matched] WHEN 0 THEN ''''+Item+'''' ELSE ',' END
FROM SplitStrings b
WHERE a.Condition = b.Condition AND a.ColumnName = b.ColumnName
ORDER BY ItemNumber
FOR XML PATH('')
)
FROM SplitStrings a
GROUP BY Condition, ColumnName;
PatternSplitCM can be found and is explained in the 4th article in my signature links.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 9, 2013 at 8:28 pm
sqlfriends@sql.com (8/17/2012)
I want to convert the column like as
Sometimes a better solution can be made if we know the reason why you would want to do such a thing. What do you intend to do with the data once it's in the format you want?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply