Solved

Problem with special characters in SQL Pivot string

Posted on 2014-11-19
2
293 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

806 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