Solved

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

Posted on 2016-11-19
7
38 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 28

Expert Comment

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

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

790 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