|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Rounding datetimeAre there any problems rounding up dates like this
DECLARE @FiveDaysPrevious DATETIME SET @FiveDaysPrevious = '12/12/2006 2:12:12' SET @FiveDaysPrevious = DATEADD( HOUR, 23-datepart( HOUR,@FiveDaysPrevious ), @FiveDaysPrevious ) SET @FiveDaysPrevious = DATEADD( MINUTE, 59-datepart( MINUTE,@FiveDaysPrevious ), @FiveDaysPrevious ) SET @FiveDaysPrevious = DATEADD( SECOND, 59-datepart( SECOND,@FiveDaysPrevious ), @FiveDaysPrevious ) Wil (Sn0tt***@yahoo.co.uk) writes:
> Are there any problems rounding up dates like this Don't really know what you want to achieve, but this appears simpler:> > DECLARE @FiveDaysPrevious DATETIME > SET @FiveDaysPrevious = '12/12/2006 2:12:12' > SET @FiveDaysPrevious = DATEADD( HOUR, 23-datepart( > HOUR,@FiveDaysPrevious ), @FiveDaysPrevious ) > SET @FiveDaysPrevious = DATEADD( MINUTE, 59-datepart( > MINUTE,@FiveDaysPrevious ), @FiveDaysPrevious ) > SET @FiveDaysPrevious = DATEADD( SECOND, 59-datepart( > SECOND,@FiveDaysPrevious ), @FiveDaysPrevious ) dateadd(minute, -1, dateadd(day, 1, convert(char(8), @d, 112))) Also, beware that a dateformat like the you use is subject to different interpretations depending on language and datetime settings. See http://www.karaszi.com/SQLServer/info_datetime.asp. -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx Erland Sommarskog wrote:
> Don't really know what you want to achieve, but this appears simpler: I was trying to round up to the end of the day,> > dateadd(minute, -1, dateadd(day, 1, convert(char(8), @d, 112))) > > Also, beware that a dateformat like the you use is subject to different > interpretations depending on language and datetime settings. See > http://www.karaszi.com/SQLServer/info_datetime.asp. > > I wanted to avoid coverting in to a string format to try and avoid language and datetime settings. (Sn0tt***@yahoo.co.uk) writes:
> I was trying to round up to the end of the day, In that case my code should read:dateadd(ms, -3, Note the funky -3. In practice it would be better to write: WHERE datecol >= @fromdate AND datecol < dateadd(day, 1, convert(char(8), @d, 112)) > I wanted to avoid coverting in to a string format to try and avoid That's why I used 112. The format YYYYMMDD is a safe format.> language and datetime settings. -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx > WHERE datecol >= @fromdate And has been demonstrated many times, it's at least as safe, and on top of > AND datecol < dateadd(day, 1, convert(char(8), @d, 112)) > >> I wanted to avoid coverting in to a string format to try and avoid >> language and datetime settings. > > That's why I used 112. The format YYYYMMDD is a safe format. that more efficient, to avoid converting to a string and back in the first place. Using DATEDIFF is a better approach than string truncation. IMHO. A Aaron Bertrand [SQL Server MVP] (ten.xoc@dnartreb.noraa) writes:
> And has been demonstrated many times, it's at least as safe, and on top of Maybe. But I looked the expression you posted and asked myself: what is> that more efficient, to avoid converting to a string and back in the first > place. Using DATEDIFF is a better approach than string truncation. IMHO. that doing? Of course, if you do it often enough, you know that after a while. But I've been converting to style 112 for 15 years, so *I* know what that one is doing. -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx That seems like a lot of unnecessary steps. Much easier to go to the exact
day boundary at midnight than to try to take a second or a millisecond off of midnight. Then change BETWEEN to two separate clauses (>= and <). DECLARE @FiveDaysPrevious SMALLDATETIME; SET @FiveDaysPrevious = '20061212 2:12:12'; SET @FiveDaysPrevious = DATEDIFF(DAY, -1, @FiveDaysPrevious); SELECT @FiveDaysPrevious; In addition to not trying to find the last possible datetime value in a calendar day, I strongly recommend avoiding ambiguous formats like m/d/y or, to illustrate the problem, is that d/m/y? I have no idea, and neither will SQL Server if you move your code from a US-based machine to one in the UK, or change the SET DATEFORMAT or SET LANGUAGE settings. A <Sn0tt***@yahoo.co.uk> wrote in message Show quote news:1157710431.743881.108720@h48g2000cwc.googlegroups.com... > Are there any problems rounding up dates like this > > DECLARE @FiveDaysPrevious DATETIME > SET @FiveDaysPrevious = '12/12/2006 2:12:12' > SET @FiveDaysPrevious = DATEADD( HOUR, 23-datepart( > HOUR,@FiveDaysPrevious ), @FiveDaysPrevious ) > SET @FiveDaysPrevious = DATEADD( MINUTE, 59-datepart( > MINUTE,@FiveDaysPrevious ), @FiveDaysPrevious ) > SET @FiveDaysPrevious = DATEADD( SECOND, 59-datepart( > SECOND,@FiveDaysPrevious ), @FiveDaysPrevious ) > > Wil > |
|||||||||||||||||||||||