Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Selecting records between dynamic dates

Author  Topic 

fralo
Posting Yak Master

161 Posts

Posted - 2011-01-12 : 08:30:31
Hi all,

I have an automated process which runs on the 1st and the 15th on the month.

If it runs on the 1st then it needs to check for the existence of records where a data value is between the 15th of the previous month and the current date. In this case, the 1st.

If it runs on the 15th then it needs to check for the existence of records where a data value is between the 1st of this month and the current date. In this case, the 15th.

I have some pseudocode like this in mind.

If @dayofmonth = '15'

select * from table
where termdate between '1st of month' and 'getdate()'

else if @dayofmonth = '1'

select * from table
where termdate between '15th of previous month' and 'getdate()'

Unfortunately, I don't know how to get all these values. Your help is greatly appreciated.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-12 : 08:42:11
[code]select * from table
where termdate >= dateadd(dd,datediff(dd,0,getdate()),-16)
and termdate < dateadd(dd,datediff(dd,0,getdate()),1)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2011-01-12 : 11:05:00
This is if the day is the 1st, correct?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-12 : 11:17:27
no regardless of date it will bring the data for last 15 days period. may be you could add one more condition


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2011-01-12 : 11:24:51
yeah maybe because depending on what month it is, it will be 14,15, or 16 days
Go to Top of Page
   

- Advertisement -