Solved

Problem with special characters in SQL Pivot string

Posted on 2014-11-19
2
277 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
Comment Utility
Put it in hard brackets, i.e. [Billy-Boy]
1
 
LVL 1

Author Closing Comment

by:Steynsk
Comment Utility
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

762 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

12 Experts available now in Live!

Get 1:1 Help Now