Solved

sql function date compare

Posted on 2014-10-09
8
121 Views
Last Modified: 2014-10-09
I am dealing with 4 date fields in a function    

@AsOfDate (passed to function)
@PaidDate (variable in function)
table.AppliedDate  
table.PostDate

I want to set the value of the @PaidDate to the table.AppliedDate but if the table.AppliedDate is Null then I want to set the @PaidDate to table.PostDate

then I only want to select those records where the @PaidDate is <= to @AsOfDate.

I don't know how to do this.
0
Comment
Question by:ryann
  • 4
  • 3
8 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40370749
Show us the function and the table schema(s) involved here.
Keep in mind that a prefix of @ indicates a variable.
0
 

Author Comment

by:ryann
ID: 40370771
Hope this helps.

I have not written the function yet. But here is the shell. I don't know how to set the @PaaidDate to the right date or where to put it in the sql

Declare @AsOfDate         (this will be passed to function)
                @PaidDate        (this will be the table.applied date or the table.PostDate

Select *
   From Table
       where
              @PaidDate <= @AsOfDate
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40370780
>this will be the table.applied date or the table.PostDate
Spell out the 'OR' logic involved here.

So far, this will go something like...

SELECT *
FROM Table
WHERE
   ( <or logic goes here that evaluates to TRUE> AND @PaidDate <= @AsOfDate)
0
 

Author Comment

by:ryann
ID: 40370810
Select *
From Table
      If isnull table.appliedDate then
            @PaidDate = table.PostedDate
     else
            @PaidDate=table.AppliedDate

where @PaidDate <= @AsOfDate
0
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.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 40370823
>where @PaidDate <= @AsOfDate
Since these are both variables, that means the entire SELECT query will return all or nothing based on this.  
(Question #1)  Correct?  And what happends if the above is false?

>else @PaidDate=table.AppliedDate
(Question #2)  SQL doesn't allow assignment of values to a variable, and a SELECT query that returns values, in the same SELECT clause, so what are you trying to pull off here?
0
 

Author Comment

by:ryann
ID: 40370844
Yes you are correct. If it is false then I don't want the record.
0
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40370883
Not sure, why you need a function..

DECLARE @AsOfDate DATE;
DECLARE @Table TABLE
    (
      AppliedDate DATE ,
      PostDate DATE
    );

SELECT  T.*
FROM    @Table T
WHERE   COALESCE(T.AppliedDate, T.PostDate) < @AsOfDate;

Open in new window

0
 

Author Closing Comment

by:ryann
ID: 40370919
New to SQL so had to look up Coalesce but it worked. Thanks!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

867 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

23 Experts available now in Live!

Get 1:1 Help Now