Solved

Alter this Sql Statement

Posted on 2016-08-11
8
27 Views
Last Modified: 2016-09-05
Here is the sql select statement Im using :
SELECT t1.CaseID,
        ,CASE WHEN [DATERECEIVED]='1900-01-01' then '' ELSE CONVERT(VARCHAR(10),[DATERECEIVED],101)END AS DATERECEIVED  
      ,CASE WHEN [TICKLERDATE]='1900-01-01' then '' ELSE CONVERT(VARCHAR(10),[TICKLERDATE],101)END AS TICKLERDATE
      ,CASE WHEN ISNULL(DATERECEIVED,'') = '' THEN '' ELSE DATEDIFF(DAY,DATERECEIVED,TICKLERDATE)END AS DATEDIFFERENCE
      ,DATEDIFF(DAY,GETDATE(),TICKLERDATE)as DATEDIFFOFDAY
  FROM Table1 t1 inner join Table2  t2 on t1.CaseID=t2.CaseID
  where TICKLERDATE > GETDATE()

and here is the output:
CaseID   DateReceived   TicklerDate    DateDifference   DateDiffOfDay
3          2/16/2016       8/14/2016                      180             3
4          6/13/2016       12/11/2016                    180            122
5          3/1/2016         8/28/2016                      180             17

What I'd like to get is that even though the DateDiffOfDay zero out or becomes negative because 180 days have been reach or over, the records will still display.

What Select statement to use?
0
Comment
Question by:zachvaldez
  • 4
  • 4
8 Comments
 
LVL 8

Expert Comment

by:Randy Peterson
ID: 41752651
All you need to do is remove your where clause to get all the records to display.  But then all the records would always display.  You could add an order by clause to show the ones where it hasn't gone past yet first.
0
 
LVL 8

Expert Comment

by:Randy Peterson
ID: 41752653
Here is the query with an order clause:

SELECT t1.CaseID,
        ,CASE WHEN [DATERECEIVED]='1900-01-01' then '' ELSE CONVERT(VARCHAR(10),[DATERECEIVED],101)END AS DATERECEIVED  
      ,CASE WHEN [TICKLERDATE]='1900-01-01' then '' ELSE CONVERT(VARCHAR(10),[TICKLERDATE],101)END AS TICKLERDATE
      ,CASE WHEN ISNULL(DATERECEIVED,'') = '' THEN '' ELSE DATEDIFF(DAY,DATERECEIVED,TICKLERDATE)END AS DATEDIFFERENCE
      ,DATEDIFF(DAY,GETDATE(),TICKLERDATE)as DATEDIFFOFDAY
  FROM Table1 t1 inner join Table2  t2 on t1.CaseID=t2.CaseID
  ORDER BY DATEDIFFOFDAY desc
1
 

Author Comment

by:zachvaldez
ID: 41752737
Now I get more data. How would I express if DATEDIFFOFDAY has -83 means 83 days over.
I would just like to say 83 days over when it gets over 180 days. Currently the numbers are express
as -7,-78....
 like to say 78 days over, 7 days over.... instead
0
 
LVL 8

Accepted Solution

by:
Randy Peterson earned 500 total points (awarded by participants)
ID: 41756584
This should work for you...

SELECT t1.CaseID,
        ,CASE WHEN [DATERECEIVED]='1900-01-01' then '' ELSE CONVERT(VARCHAR(10),[DATERECEIVED],101)END AS DATERECEIVED  
      ,CASE WHEN [TICKLERDATE]='1900-01-01' then '' ELSE CONVERT(VARCHAR(10),[TICKLERDATE],101)END AS TICKLERDATE
      ,CASE WHEN ISNULL(DATERECEIVED,'') = '' THEN '' ELSE DATEDIFF(DAY,DATERECEIVED,TICKLERDATE)END AS DATEDIFFERENCE
      ,CONVERT(VARCHAR,ABS(DATEDIFF(DAY,GETDATE(),TICKLERDATE))) + ' days over' AS DATEDIFFOFDAY
  FROM Table1 t1 inner join Table2  t2 on t1.CaseID=t2.CaseID
  ORDER BY DATEDIFFOFDAY DESC
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:zachvaldez
ID: 41759663
thanks!!
0
 

Author Comment

by:zachvaldez
ID: 41759668
ok!!
0
 
LVL 8

Expert Comment

by:Randy Peterson
ID: 41784448
Looks like the question was answered
0
 

Author Comment

by:zachvaldez
ID: 41785601
I acknowledge the answer.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

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

19 Experts available now in Live!

Get 1:1 Help Now