Using Case Statement to choose select statement

I want to choose between 2 select statements whether today is the beginning of the month or not.

I am getting an error.

How can I correct the syntax for the following code?

if OBJECT_ID(N'tempdb..#RollTest') is not null
		Begin
			drop table #RollTest
		End
	Create table #RollTest(
	   [AccountNumber] varchar (15)
		,[CAmt] numeric(15,2)
		,[VisitID] varchar (15),
		Typ varchar(5)
		);
		
/*-----------------------------------------------*/
/*                                               */
/*                                               */
/*         Input PriorDay Beginning ATB          */
/*                                               */
/*                                               */
/*-----------------------------------------------*/		
	Declare @BOM as DateTime;
	Declare @CD as DateTime;
	set @BOM = 	dateadd(dd,datediff(dd,0,dateadd(dd,-(DAY(dateadd(mm,1,getdate()))-1),dateadd(mm,0,getdate()))),0)-1
	set @CD = DATEADD(dd,datediff(dd,0,getdate()),0)
	insert into #RollTest
	Case When @BOM = @CD
		
			SELECT  DAPMA.AccountNumber
			,DAPMA.CAmt
			,DAPMA.VisitID
			FROM [livedb].[dbo].[vw_DAILY_ATB_Prior_Month_ATB] DAPMA
		
	else
		
			SELECT  convert(varchar(15),DATB.AccountNumber) as AccountNumber
			  ,convert(numeric(15,2),sum(DATB.CAmt)) as CAmt
			  ,convert(varchar(15),DATB.VisitID) as VisitID
			  ,'A' as Typ
			FROM [livedb].[dbo].[tbl_DAILY_ATB_CMBAL] DATB
			where DATB.VisitID is not null and SUBSTRING(DATB.AccountNumber,1,1)<>'B' and DATB.AccountNumber not in ('A00000221516','A0000222086','A00000272018') 
			group by DATB.AccountNumber,DATB.VisitID
	end
    

Open in new window


thanks

glen
GPSPOWAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try

if OBJECT_ID(N'tempdb..#RollTest') is not null
		Begin
			drop table #RollTest
		End
	Create table #RollTest(
	   [AccountNumber] varchar (15)
		,[CAmt] numeric(15,2)
		,[VisitID] varchar (15),
		Typ varchar(5)
		);
		
/*-----------------------------------------------*/
/*                                               */
/*                                               */
/*         Input PriorDay Beginning ATB          */
/*                                               */
/*                                               */
/*-----------------------------------------------*/		
	Declare @BOM as DateTime;
	Declare @CD as DateTime;
	set @BOM = 	dateadd(dd,datediff(dd,0,dateadd(dd,-(DAY(dateadd(mm,1,getdate()))-1),dateadd(mm,0,getdate()))),0)-1
	set @CD = DATEADD(dd,datediff(dd,0,getdate()),0)


	IF @BOM = @CD

			insert into #RollTest
			SELECT  DAPMA.AccountNumber
			,DAPMA.CAmt
			,DAPMA.VisitID
			FROM [livedb].[dbo].[vw_DAILY_ATB_Prior_Month_ATB] DAPMA
		
	else
			insert into #RollTest
			SELECT  convert(varchar(15),DATB.AccountNumber) as AccountNumber
			  ,convert(numeric(15,2),sum(DATB.CAmt)) as CAmt
			  ,convert(varchar(15),DATB.VisitID) as VisitID
			  ,'A' as Typ
			FROM [livedb].[dbo].[tbl_DAILY_ATB_CMBAL] DATB
			where DATB.VisitID is not null and SUBSTRING(DATB.AccountNumber,1,1)<>'B' and DATB.AccountNumber not in ('A00000221516','A0000222086','A00000272018') 
			group by DATB.AccountNumber,DATB.VisitID
	

Open in new window

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
GPSPOWAuthor Commented:
thank you

Glen
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
Query Syntax

From novice to tech pro — start learning today.