Solved

Problem with special characters in SQL Pivot string

Posted on 2014-11-19
2
288 Views
Last Modified: 2014-11-19
Hi Expert,


I've got this table:

CREATE TABLE membership(
	[memberID] [int] NOT NULL,
	[username] [nvarchar](50) NULL,
	[groupname] [nvarchar](50) NULL
)

insert into membership (memberID,username,groupname) values(28,'Bill','Domain users')
insert into membership (memberID,username,groupname) values(30,'Bill','Application B')
insert into membership (memberID,username,groupname) values(33,'Judy','Domain users')
insert into membership (memberID,username,groupname) values(34,'Judy','Application A')
insert into membership (memberID,username,groupname) values(36,'John','Domain users')
insert into membership (memberID,username,groupname) values(37,'John','Application B')
insert into membership (memberID,username,groupname) values(38,'John','Printer 45')

Open in new window


And this pivot query:

SELECT * FROM (SELECT memberID, groupname, username FROM ADS_membership) t PIVOT (MIN(memberID) FOR username IN (Bill,Judy,John)) p

Open in new window


All works well until a name is containing a minus sign (-). Like in the example below:

SELECT * FROM (SELECT memberID, groupname, username FROM ADS_membership) t PIVOT (MIN(memberID) FOR username IN (Billy-Boy,Judy,John)) p

Open in new window


Of Course I know that is not the way you should a name like that but I simplified and translated the problem.

How can I deal with this problem?
0
Comment
Question by:Steynsk
2 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40452475
Put it in hard brackets, i.e. [Billy-Boy]
1
 
LVL 1

Author Closing Comment

by:Steynsk
ID: 40452506
Thanks
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Backup & Restore 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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

862 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now