Solved

sql function date compare

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

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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 insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

832 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