Solved

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

Posted on 2016-11-19
7
43 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 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
find SQL job run average duration 24 54
Need help with a query 3 37
sql query 5 43
Split string into 3 separate fields 5 19
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

756 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