Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DateDiff help to verify my code is correct or not?


DateDiff help to verify my code is correct or not?

Author
Message
dhananjay.nagarkar
dhananjay.nagarkar
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 185
Hi Friends,

I'm using DateDiff function -

my requirement is -

1) "SmartLaborII Job Posting End Date"" is NOT < ""SmartLaborII Work Order End Date"

and
my code is -
--------
not(datediff(day,[WorkForce_JobPosting].[Job Posting End Date_JP]
,[WorkForce_WorkOrder].[Work Order End Date_WO])
> 0)
--------

2) "SmartLaborII Work Order End Date"" is NOT greater than ""SmartLaborII Previous Work Order End Date"

my code is -

///

datediff(day,[WorkForce_WorkOrder].[Work Order End Date_WO]
,[WorkForce_WorkOrder].[Previous Work Order End Date_WO] )
>= 0
//

Need help from experts to verify if my code for the 2 requirements is correct ?

Kind Regards
Dhananjay
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24185 Visits: 37953
This is the fourth or fifth thread you have started for the same question. Creating new threads every day or so isn't going to get you more help. Keep the same question in one place. it keeps all the responses you do get together instead of fragmenting them across multiple threads.

We have tried to answer this for you before but I don't recall you ever answering the question about how you want to handle null values in either or both of the columns you are comparing. Without that informationi e really can't help you.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16564 Visits: 17014
I haven't seen all the other threads but from what you posted I can't figure out why you are using DATEDIFF at all. It doesn't meet your requirements and it will make your query nonSARGable.

I think that something like this should meet your requirements. Of course as Lynn stated this doesn't accomodate NULL values in any of those columns.


[WorkForce_JobPosting].[Job Posting End Date_JP] < [WorkForce_WorkOrder].[Work Order End Date_WO]
AND
[WorkForce_WorkOrder].[Previous Work Order End Date_WO] <= [WorkForce_WorkOrder].[Work Order End Date_WO]



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24185 Visits: 37953
Sean Lange (3/14/2013)
I haven't seen all the other threads but from what you posted I can't figure out why you are using DATEDIFF at all. It doesn't meet your requirements and it will make your query nonSARGable.

I think that something like this should meet your requirements. Of course as Lynn stated this doesn't accomodate NULL values in any of those columns.


[WorkForce_JobPosting].[Job Posting End Date_JP] < [WorkForce_WorkOrder].[Work Order End Date_WO]
AND
[WorkForce_WorkOrder].[Previous Work Order End Date_WO] <= [WorkForce_WorkOrder].[Work Order End Date_WO]



I gave him basically the same answer in another thread. He didn't like the answer because one or the other of the columns can be null. With that said, DATEDIFF suffers the same problem if a column is null.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16564 Visits: 17014
Lynn Pettis (3/14/2013)
Sean Lange (3/14/2013)
I haven't seen all the other threads but from what you posted I can't figure out why you are using DATEDIFF at all. It doesn't meet your requirements and it will make your query nonSARGable.

I think that something like this should meet your requirements. Of course as Lynn stated this doesn't accomodate NULL values in any of those columns.


[WorkForce_JobPosting].[Job Posting End Date_JP] < [WorkForce_WorkOrder].[Work Order End Date_WO]
AND
[WorkForce_WorkOrder].[Previous Work Order End Date_WO] <= [WorkForce_WorkOrder].[Work Order End Date_WO]



I gave him basically the same answer in another thread. He didn't like the answer because one or the other of the columns can be null. With that said, DATEDIFF suffers the same problem if a column is null.


Having seen your post I went and found the other thread(s). I was laughed that we both posted code that was so nearly identical you can't tell the difference.

Not only does the DATEDIFF version not handle NULLS and render indexes useless, the most important flaw is that it doesn't actually meet the requirements if the dates are on the same day. In other words using datediff we could have [Job Posting End Date_JP] being greater than [Work Order End Date_WO] but the datediff logic would say it is ok.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
dhananjay.nagarkar
dhananjay.nagarkar
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 185
Hi Sean,

When you say-

"
I haven't seen all the other threads but from what you posted I can't figure out why you are using DATEDIFF at all. It doesn't meet your requirements and it will make your query nonSARGable"

Can you please help me understand why it does not meet my requirement?
Also Why and under what scenarios / situtations DateDiff function needs to be used and need not be used?
The solution you and Lynnhave provided that I already had beven before I joined this site, but using Predicates comparison for dates is not allowed in my team I have been asked to use DateDiff hence would be much obliged if you can justiy your reason and help me learn please so that I can convince my manager Smile

Kind Regards
Dhananjay
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24185 Visits: 37953
dhananjay.nagarkar (3/14/2013)
Hi Sean,

When you say-

"
I haven't seen all the other threads but from what you posted I can't figure out why you are using DATEDIFF at all. It doesn't meet your requirements and it will make your query nonSARGable"

Can you please help me understand why it does not meet my requirement?
Also Why and under what scenarios / situtations DateDiff function needs to be used and need not be used?
The solution you and Lynnhave provided that I already had beven before I joined this site, but using Predicates comparison for dates is not allowed in my team I have been asked to use DateDiff hence would be much obliged if you can justiy your reason and help me learn please so that I can convince my manager Smile

Kind Regards
Dhananjay


SARGable, meaning SQL Server will use an index on the table if one exists that benefits the query. Using DATEDIFF the way you are using it will force SQL Server to use a table scan and process every row of the table(s) to determine if the criteria is met for the records to be included in the result set.

As for what DATEDIFF will do, run the following:



declare @Date1 datetime, @Date2 datetime;
set @date1 = '20130314 19:23:30.000';
set @date2 = '20130314 19:23:45.000';
-- By observation @date1 is less than @date2

select
@date1,
@date2,
case when @date1 < @date2 then '@date1 < @date2' else '@date2 <= @date1' end, -- will display @date1 < @date2: true
case when datediff(day,@date1,@date2) > 0 then '@date1 < @date2' else '@date2 <= @date1' end -- will display @date2 <= @date1: not true




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24185 Visits: 37953
By the way, you still haven't told us how you want to handle situations where one or both of the dates are null. If date1 is null and date2 is not null, date1 is not null and date2 is null, both date1 and date2 are null. We need this information to provide a complete answer.


What would really help, is the DDL (CREATE TABLE statement(s)) for the table(s) involved, sample data for the table(s) as a series of INSERT INTO statements, expected results based on the sample data, and the code you have written so far to solve your problem.

If you need help with all this, take the time to read the first article I reference below in my signature block regarding asking for help. It will walk you through what you need to post and how to do it.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16564 Visits: 17014
Dhananjay - did the example and explanation Lynn posted help you understand? The problem with datediff is calculates differences on a boundary. The results is how many of those boundaries are crossed between the two dates. In the example Lynn posted the datediff(DAY...) will return 0 but clearly @date2 > @date1.


using Predicates comparison for dates is not allowed in my team


This is obviously based on the fact that your manager does not understand how this type of thing works. You are forbidden to use best way for data comparison. w00t

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search