SQL select for month date range
20 pts.
0
Q:
SQL select for month date range
Hello,

I am attempting to obtain a count of visits each patient comes in in one month, so for example of they were a new patient in January on the 4th, I would want from the 4th of January to the 4th of February.  The code I am attempting to use is below, but every time I use it, no records are returned.  How can I fix this problem?

Thanks,

Craig

select [last name], [first name], [new patient date], count (distinct [service date 1]) as 'Visit Count'
from [chiropractic healing center].dbo.patient
inner join [chiropractic healing center].dbo.[Billing detail] on [chiropractic healing center].dbo.patient.[chart number] = [chiropractic healing center].dbo.[Billing detail].chart
where [new patient date] between '2009-05-01' and '2009-05-31'
and [service date 1] between [new patient date] and datepart(m,[new patient date])+1
and ([transaction code] = '98940' or [transaction code] = '98941' or [transaction code] = 'A9170' or [transaction code] = 'A9270')
group by [last name], [first name], [new patient date]
order by [new patient date]


Software/Hardware used:
SQL Server 2005
ASKED: Nov 9 2009  10:03 PM GMT
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
0
10290 pts.
0
A:
 RATE THIS ANSWER
+1
Click to Vote:
  •   1
  •  0
  • AddThis Social Bookmark Button
Carlosdl 29360 pts. | Nov 9 2009 11:08PM GMT

I’m wondering how is that running without errors.

AFAIK, the DATEPART function retunrs an integer, so, here:

[service date 1] between [new patient date] and datepart(m,[new patient date])+1


You are mixing two different data types.

I think you should try something like this:

[service date 1] between [new patient date] and DATEADD(m,1,[new patient date])
Last Answered: Nov 10 2009  0:24 AM GMT by ITKE   10290 pts.
0
0
Discuss This Answer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _



_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Carlosdl   29710 pts.  |   Nov 9 2009  11:08PM GMT

I’m wondering how is that running without errors.

AFAIK, the DATEPART function retunrs an integer, so, here:

[service date 1] between [new patient date] and datepart(m,[new patient date])+1

You are mixing two different data types.

I think you should try something like this:

[service date 1] between [new patient date] and DATEADD(m,1,[new patient date])

 

CraigN   20 pts.  |   Nov 9 2009  11:44PM GMT

that works perfectly, thanks for your help!

 
0