Solved

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

Posted on 2016-11-19
7
44 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

734 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