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
Solved

sql function date compare

Posted on 2014-10-09
8
124 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
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

 

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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

839 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