Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I alter the result of a SQL Query

Posted on 2013-06-19
5
Medium Priority
?
254 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 2000 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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