Solved

How can I alter the result of a SQL Query

Posted on 2013-06-19
5
251 Views
Last Modified: 2013-06-19
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
0
Comment
Question by:GD_GRAY
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:thenerdynerd
ID: 39260367
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
0
 
LVL 9

Expert Comment

by:selva_kongu
ID: 39260429
SELECT REPLACE(SUBSTRING(SUBSTRING(games,3,LEN(games),1,LEN(games)-4),',,','-')

FROM hoby
0
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 39260450
To eliminate beginning and ending ',,' -
select substring(replace(games,',,','-'),2,len(replace(games,',,','-')) - 2)
from hobby;
0
 
LVL 32

Expert Comment

by:awking00
ID: 39260461
Or you can do the substring before the replace -
select replace(substring(games,3,len(games) - 4),',,','-') from hobby;
0
 

Author Closing Comment

by:GD_GRAY
ID: 39260950
Thank you so very much.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question