Solved

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

Posted on 2016-11-19
7
24 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
  • 3
  • 2
7 Comments
 
LVL 18

Expert Comment

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

Expert Comment

by:Pawan Kumar Khowal
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 65

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now