Link to home
Start Free TrialLog in
Avatar of GD_GRAY
GD_GRAYFlag for United States of America

asked on

How can I alter the result of a SQL Query

If I run a query like this
SELECT games FROM hoby

Open in new window

it will return a value like
 ,,Bingo,,Darts,,Pool,,WordsWithFriends,,
how can I code the select statement to change the ,, into a dash - so the result would be
-Bingo-Darts-Pool-WordsWithFriends-  
it would also be nice to omit leading and trailing ,, where the result is
Bingo-Darts-Pool-WordsWithFriends
Avatar of thenerdynerd
thenerdynerd

Is this just one column with all that data?  

Best solution would be to split the games data into separate columns that would be best practice, then in a statement you can manipulate much easier.  I suspect your data looks like the last line in the before SQL screen shot?

In any case here is the SQL that I think you are after:

SELECT REPLACE(games,',,','-') games FROM Hobbies
WHERE games IS NOT NULL
Screen-Shot-2013-06-19-at-18.36..jpg
Screen-Shot-2013-06-19-at-18.45..jpg
SELECT REPLACE(SUBSTRING(SUBSTRING(games,3,LEN(games),1,LEN(games)-4),',,','-')

FROM hoby
ASKER CERTIFIED SOLUTION
Avatar of awking00
awking00
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or you can do the substring before the replace -
select replace(substring(games,3,len(games) - 4),',,','-') from hobby;
Avatar of GD_GRAY

ASKER

Thank you so very much.