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


Urgent help need to verify DateDiff is correct?


Urgent help need to verify DateDiff is correct?

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,

my requirement is-

"SmartLaborII Work Order End Date" is NOT greater than "SmartLaborII Previous Work Order End Date"]-

and the code I'm using is-

//

datediff(day,[WorkForce_WorkOrder].[Work Order End Date_WO]
            ,[WorkForce_WorkOrder].[Previous Work Order End Date_WO] )
            >= '0'
//
Since I'm new to TSQL world, wanted to know if my above code for my requirement is correct ? if not what should be the right code and why please?


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: 24167 Visits: 37929
If they are DATE or DATETIME data types why not just do this?

[WorkForce_WorkOrder].[Work Order End Date_WO] <= [WorkForce_WorkOrder].[Previous Work Order End Date_WO]

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)
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
I would prefer datediff since that way not dependent of type of date format in DB hence need help please:-)
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: 24167 Visits: 37929
dhananjay.nagarkar (3/7/2013)
I would prefer datediff since that way not dependent of type of date format in DB hence need help please:-)


If the columns are defined as one of the date data types, you don't need to use DATEDIFF. You can compare dates just like you can compare integers values.

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)
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 Lynn,

that will not help since in my case one of the dates can remian NULL hence if that's the case then it will not work using > , = etc; hence datediff

thanks
Dhananjay
Michael Valentine Jones
Michael Valentine Jones
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3254 Visits: 11771
dhananjay.nagarkar (3/7/2013)
Hi Lynn,

that will not help since in my case one of the dates can remian NULL hence if that's the case then it will not work using > , = etc; hence datediff

thanks
Dhananjay


If either if the date values is NULL, then it will fail the test either way, with or without DATEDIFF.

You need to explain what you want to happen if one or both of the values is NULL.
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: 24167 Visits: 37929
observe:



declare @TestDate datetime;
select datediff(dd,getdate(),@TestDate)
select datediff(dd,@TestDate,getdate())
set @TestDate = '20130318';
select datediff(dd,getdate(),@TestDate)
select datediff(dd,@TestDate,getdate())






-----------
NULL

(1 row(s) affected)


-----------
NULL

(1 row(s) affected)


-----------
11

(1 row(s) affected)


-----------
-11

(1 row(s) affected)



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)
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
Thanks Lynn you are right
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