Войти в систему

Home
    - Создать дневник
    - Написать в дневник
       - Подробный режим

LJ.Rossia.org
    - Новости сайта
    - Общие настройки
    - Sitemap
    - Оплата
    - ljr-fif

Редактировать...
    - Настройки
    - Список друзей
    - Дневник
    - Картинки
    - Пароль
    - Вид дневника

Сообщества

Настроить S2

Помощь
    - Забыли пароль?
    - FAQ
    - Тех. поддержка



Пишет Abu Antos' ([info]syarzhuk)
@ 2005-10-19 10:52:00


Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Музыка:Reverend Glasseye And His Wooden Legs - Black River Falls
Entry tags:code, sql

[programmism] DateDiff
You would think that the T-SQL function that is called DateDiff would return the difference between the two dates. For example, if your two dates are 10:53 and 10:54 (the difference is 1 minute), and you want to get the difference in hours, you would expect that 1 minute difference would be rounded to 0 hours. It does happen with 10:53 and 10:54, however, you'll be surprised when you calculate the difference between 10:59 and 11:00. A quick check shows that the difference between '1/31/2005 23:59:59' and '2/1/2005' (1 second time interval) is equal to 1 minute, 1 hour, 1 day and 1 month depending on the first argument to DateDiff!
SELECT DateDiff(ss, '1/1/2005 1:00:00 AM', '1/1/2005 1:00:01 AM')
SELECT DateDiff(ss, '1/31/2005 23:59:59', '2/1/2005')
SELECT DateDiff(mi, '1/1/2005 1:00:00 AM', '1/1/2005 1:00:01 AM')
SELECT DateDiff(mi, '1/31/2005 23:59:59', '2/1/2005')
SELECT DateDiff(hh, '1/1/2005 1:00:00 AM', '1/1/2005 1:00:01 AM')
SELECT DateDiff(hh, '1/31/2005 23:59:59', '2/1/2005')
SELECT DateDiff(dd, '1/1/2005 1:00:00 AM', '1/1/2005 1:00:01 AM')
SELECT DateDiff(dd, '1/31/2005 23:59:59', '2/1/2005')
SELECT DateDiff(mm, '1/1/2005 1:00:00 AM', '1/1/2005 1:00:01 AM')
SELECT DateDiff(mm, '1/31/2005 23:59:59', '2/1/2005')


Of course, it is mentioned in the help - "DATEDIFF Returns the number of date and time boundaries crossed between two specified dates", but who in their right mind decided to implement it like that??? Note to self: only use DateDiff for seconds (ss), as others are not helpful at all..



(Добавить комментарий)


[info]gladiolux@lj
2005-10-19 12:42 (ссылка)
Suppose you need to find out whether two payments cleared on the same day or not.

(Ответить) (Ветвь дискуссии)


[info]syarzhuk@lj
2005-10-19 12:46 (ссылка)
I understand why someone might need to know the "number of date and time boundaries crossed". I don't understand why the function that calculates it is called DateDiff.

(Ответить) (Уровень выше) (Ветвь дискуссии)


[info]gladiolux@lj
2005-10-19 12:56 (ссылка)
Well I thought you'd written only use DateDiff for seconds (ss), as others are not helpful at all.. rather than ... as others have a misleading name

(Ответить) (Уровень выше) (Ветвь дискуссии)


[info]syarzhuk@lj
2005-10-19 12:58 (ссылка)
they are not helpful at all to me

(Ответить) (Уровень выше)


[info]dmpogo@lj
2005-10-19 13:22 (ссылка)
I'm not a professional programmer or an expert in naming conventions (my functions tend to be called aaa and bbb :) but the functionality seems useful.

(Ответить) (Ветвь дискуссии)


[info]syarzhuk@lj
2005-10-19 13:34 (ссылка)
I'm not saying it is not useful (gladiiolux shows an example when it would be required) - I'm saying that misleading names are even worse than aaa and bbb.

(Ответить) (Уровень выше)


[info]109@lj
2005-10-19 14:16 (ссылка)
you are spoiled by .NET's "TimeSpan = DateTime - DateTime", don't you?

anyway, you just described the first second thing junior mssql developer learns about t-sql (the first one being to not use "= null")

(Ответить) (Ветвь дискуссии)


[info]syarzhuk@lj
2005-10-19 14:31 (ссылка)
Причём тут .Net? Я в его сторону ещё и не начал копать. Просто, по-моему, функция с названием DateDiff должна выдавать difference between dates, а не что-то другое. А is null vs =null - это вообще стандарт, кажется, и к MS отношения не имеет.
О, сколько ещё (http://www.livejournal.com/community/ru_programming/278230.html) открытий чудных готовит нам T-SQL...

(Ответить) (Уровень выше)


[info]sraider@lj
2005-10-19 14:36 (ссылка)
Books online:
------
DATEDIFF
Returns the number of date and time boundaries crossed between two specified dates.
------
I.e. datediff(hour, '1/31/2005 23:59:59', '2/1/2005') must return ONE hour-cross.

(Ответить) (Ветвь дискуссии)


[info]syarzhuk@lj
2005-10-19 14:43 (ссылка)
Ну дык я ж не говорю, что это противоречит документации. Я говорю, что задокументированный результат функции противоречит названию. Это все равно, что определить Abs(x) = -x, и это задокументировать. В 50% плюс одном случае результат будет совпадать с интуитивным :)

(Ответить) (Уровень выше) (Ветвь дискуссии)


[info]sraider@lj
2005-10-19 15:09 (ссылка)
а... не до конца прочитал твой пост. да, ребята из Microsoft слажали по крайней мере с названием функции.

(Ответить) (Уровень выше)