Solved

sql function date compare

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

705 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

15 Experts available now in Live!

Get 1:1 Help Now