"Not a Valid identifier"

Hi all,

Please find the attached code,

When am trying to run the code with my live data I have this below.

Error Message
The name 'Select [Mobile no], isnull([01-01-2011],'-') [01-1-2011], isnull([01-02-2011],'-')[01-02-2011], isnull([01-03-2011],'-') ... is not a valid identifier


Please help out
Cheers
declare @cols1 nvarchar(max)
declare @cols2 nvarchar(max)
 select  @cols1 = stuff(( select distinct top 100 percent ',isnull([' + convert(varchar,t2.Calldate,105) + '],''-'') [' + convert(varchar,t2.Calldate,105) + ']'
                           from    [Source table] as t2
                          order by 1
                            for xml path('')), 1, 1, '') + ''

 select  @cols2 = stuff(( select distinct top 100 percent ',[' + convert(varchar,t2.Calldate,105) + ']'
                           from    [Source table] as t2
                          order by 1
                            for xml path('')), 1, 1, '') + ''
declare @query nvarchar(max)
set @query = 'select [Mobile no],' + @cols1 + ' 
from (select [Mobile no],convert(varchar,CallDate,105) CallDate, Case When Disposition is NULL then ''-'' Else Disposition End as Disposition  from [Source table]) t1
               pivot (max(Disposition) for Calldate in (' + @cols2 + ')) as p'
print @query
exec (@query)

Open in new window

parpaaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rajkumar GsSoftware EngineerCommented:
I tried this query by creating the table and one data. It's working.
Please post some data. will try

--DROP TABLE	[Source table]

CREATE TABLE [Source table]
(
	Calldate DATETIME,
	 [Mobile no] VARCHAR(20),
	 Disposition VARCHAR(10)
)

INSERT INTO [source table]
SELECT GETDATE(), '3234234234', '234234'

declare @cols1 nvarchar(max)
declare @cols2 nvarchar(max)
 select  @cols1 = stuff(( select distinct top 100 percent ',isnull([' + convert(varchar,t2.Calldate,105) + '],''-'') [' + convert(varchar,t2.Calldate,105) + ']'
                           from    [Source table] as t2
                          order by 1
                            for xml path('')), 1, 1, '') + ''

 select  @cols2 = stuff(( select distinct top 100 percent ',[' + convert(varchar,t2.Calldate,105) + ']'
                           from    [Source table] as t2
                          order by 1
                            for xml path('')), 1, 1, '') + ''
declare @query nvarchar(max)
set @query = 'select [Mobile no],' + @cols1 + ' 
from (select [Mobile no],convert(varchar,CallDate,105) CallDate, Case When Disposition is NULL then ''-'' Else Disposition End as Disposition  from [Source table]) t1
               pivot (max(Disposition) for Calldate in (' + @cols2 + ')) as p'
print @query
exec (@query)

DROP TABLE [Source table]

Open in new window

0
parpaaAuthor Commented:
Please find the test data attached.

I have problems with the live data. It s working fine with my test data.

thanks again
Source-Table.xlsx
0
SharathData EngineerCommented:
I have executed your query with the sample data provided and did not get any error.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

parpaaAuthor Commented:
Hi Sharath,
The thing is that it was working fine with the test data. when I try with the live data..having this error NOT A VALID IDENTIFIER
0
SharathData EngineerCommented:
You have to check your live data where exactly its failing. If possible, provide few records from the live data.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
parpaaAuthor Commented:
Hi Sharath,

Apologies for the delay respnse, i tried my best to get fw records .. frm the live env.. unfortunately failed.
I see no prb with the code you prvded. May be I have to look into the DT.

and  Thnk you everyone for your efforts
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.