Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 select 21/(datediff(dd,getdate(),getdate()) Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, November 26, 2008 3:09 AM
 Forum Newbie Group: General Forum Members Last Login: Wednesday, November 26, 2008 7:04 AM Points: 3, Visits: 4
 select 21/(datediff(dd,getdate(),getdate())Hi How I can get an out put of 21............
Post #608918
 Posted Wednesday, November 26, 2008 3:23 AM
 SSC Veteran Group: General Forum Members Last Login: Monday, November 28, 2016 6:17 AM Points: 280, Visits: 423
 What do you mean? Output of 21?In your code there's a ")" missing. Like this:select 21/(datediff(dd,getdate(),getdate()))But it's no good anyway.This code will give you 21 as output but I'm quite sure this is not what you mean:select (datediff(dd,getdate(),getdate()) + 21)
Post #608925
 Posted Wednesday, November 26, 2008 3:38 AM
 Forum Newbie Group: General Forum Members Last Login: Wednesday, November 26, 2008 7:04 AM Points: 3, Visits: 4
 Hi Thanks for your reply..my requirement is,select 21/datediff(dd,getdate(),getdate())if I execuite above i will get an error as Msg 8134, Level 16, State 1, Line 1Divide by zero error encountered.but I want top value( ie A/datediff(dd,getdate(),getdate()), I want ans as A)the date diff is 0, but we have to make it as 1, so that I will get top value as 21.
Post #608933
 Posted Wednesday, November 26, 2008 3:53 AM
 SSCommitted Group: General Forum Members Last Login: Wednesday, February 24, 2010 4:10 AM Points: 1,550, Visits: 2,232
 Is this a real life example.Cause if you always doing a date diff with GETDATE vs GETDATE then why don't you just do this.SELECT 21I think something is missing here.Do you have sample data with other examples ----------------------------------------------Try to learn something about everything and everything about something. - Thomas Henry Huxley Posting Best PracticesNumbers / Tally TablesSQL-4-Life
Post #608938
 Posted Wednesday, November 26, 2008 3:55 AM
 SSC Veteran Group: General Forum Members Last Login: Monday, November 28, 2016 6:17 AM Points: 280, Visits: 423
 Hmm...I still don't understand what you want?This code is what I believe you're asking for:select 21 / (datediff(dd,getdate(),getdate()) + 1)But you could just as easily replace (datediff(dd,getdate(),getdate()) + 1) by just 1.select 21 / 1Above code is the same.Why do you want to subtract today from today ( GETDATE() - GETDATE() )? What's the purpose of doing that?
Post #608940
 Posted Wednesday, November 26, 2008 3:56 AM
 SSCommitted Group: General Forum Members Last Login: Wednesday, February 24, 2010 4:10 AM Points: 1,550, Visits: 2,232
 Or is the situation something like thisDECLARE @TABLE TABLE(Number INT,Date1 DATETIME,Date2 DATETIME)INSERT INTO @TABLESELECT 21,GETDATE(),GETDATE() UNION ALLSELECT 21,GETDATE(),DATEADD(dd,2,GETDATE()) SELECT Number/CASE WHEN (datediff(dd,Date1,Date2)) = 0 THEN 1 ELSE (datediff(dd,Date1,Date2)) ENDFROM @TABLEWhich case I used a case statement to check for 0 before dividingyou could right it differently but a case statement would work for you.ThanksChris ----------------------------------------------Try to learn something about everything and everything about something. - Thomas Henry Huxley Posting Best PracticesNumbers / Tally TablesSQL-4-Life
Post #608942
 Posted Wednesday, November 26, 2008 3:57 AM
 Forum Newbie Group: General Forum Members Last Login: Wednesday, November 26, 2008 7:04 AM Points: 3, Visits: 4
 HI Just see below...............select 21/ case when datediff(dd,getdate(),getdate()) <1 then 1 else datediff(dd,getdate(),getdate()) end
Post #608943
 Posted Wednesday, November 26, 2008 4:04 AM
 SSCommitted Group: General Forum Members Last Login: Wednesday, February 24, 2010 4:10 AM Points: 1,550, Visits: 2,232
 Is that the solution you using or the problem you having?We still don't understand why you feel the need to test GETDATE vs GETDATE cause the answer is ALWAYS the same!!!! ----------------------------------------------Try to learn something about everything and everything about something. - Thomas Henry Huxley Posting Best PracticesNumbers / Tally TablesSQL-4-Life
Post #608947
 Posted Wednesday, November 26, 2008 4:07 AM
 SSC Veteran Group: General Forum Members Last Login: Monday, November 28, 2016 6:17 AM Points: 280, Visits: 423
 datediff(dd,getdate(),getdate()) IS ALWAYS 0 and therefore LESS THEN 1What is the purpose of doing that?
Post #608951

 Permissions