Solved

Selecting field values from a table record based on today's date

Posted on 2016-11-19
7
45 Views
Last Modified: 2016-11-24
I have a table of payroll calendar dates which includes biweekly records ranging from 1/1/2010 through 12/31/2020.  The fields are:

Beginning Date
EndingDate
PayDate

I need to store the Beginning and Ending dates to @Beg and @End variables when dateadd(day, -1,getdate()) is greater than or equal to the BeginningDate and Less than or equal to the EndingDate in the record.

I am having trouble writing the query when getdate() = the ending date.

Can someone help?

thank you

Glen
0
Comment
Question by:GPSPOW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41894186
can you please post the entire query ?
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41894188
Are you looking for this..

DECLARE @Start AS DATE = dateadd(day, -1,getdate())
DECLARE @End AS DATE = dateadd(day, -1,getdate())

SELECT @Start, @End

O/p
           
---------- ----------
2016-11-18 2016-11-18

(1 row(s) affected)
0
 

Author Comment

by:GPSPOW
ID: 41894196
The statement needs to select the correct beginning and ending date from the table.  For example, if today is 11/19, then the current payroll based on the table started 11/6/2016 and ends 11/19/16, so those dates would be stored in the 2 variables.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 66

Expert Comment

by:Jim Horn
ID: 41894267
>I need to store the Beginning and Ending dates to @Beg and @End variables when dateadd(day, -1,getdate()) is greater than or equal to the BeginningDate and Less than or equal to the EndingDate in the record.

I'm guessing there's a larger requirement then this description, so it might be worth it to step back, collect your thoughts, and then spell out exactly what you are trying to do here.   Preferably with a data mockup.
0
 

Accepted Solution

by:
GPSPOW earned 0 total points
ID: 41894276
I have solved this.

Not elegant but it works.

declare @BD datetime,

@EDate datetime,

@PDate datetime

 

 

set @PDate=cast(GETDATE() as DATE)

 

 

 

if OBJECT_ID(N'tempdb..#PPDate') is not null

            Begin

                  drop table #PPDate

            End

      Create table #PPDate(

     

      Metric varchar (30),

     

      BDate Date,

      EDate date

     

      );

 

 

 

 

insert into #PPDate

SELECT  [payroll] as Metric

     

      ,max([begdate]) as BDate

      ,max([enddate]) as EDate

     

  FROM [livedb].[dbo].[tbl_sp_pp_calendar]

  where begdate <=dateadd(day,-1,cast(@PDate as DATE)) and enddate>=dateadd(day,-1,cast(@PDate as DATE))

  group by payroll

 

 

  set @BD=(select BDate

  from #PPDate)

 

  set @EDate=(select EDate

  from #PPDate)
0
 

Author Closing Comment

by:GPSPOW
ID: 41900317
I was able to figure it out.

Thanks

Glen
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

688 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