datediff

MS SQL Server : calculate age with accuracy of hours and minuets

安稳与你 提交于 2019-12-02 06:05:58
I need an SQL function to calculate age. It has to be accurate and cover all corner cases. It is for hospital ward for babies, so age of 30 minuets is a common case. I have a looked on other answers but could not find one that deals with all cases. For example: Baby born in 2014-04-29 12:59:00.000. And now is 2014-04-29 13:10:23.000, Age should be 0 Years, 0 Months, 0 Days, 0 Hours, 11 minutes It would be great if someone can provide the ultimate and definitive version of that function. (I am afraid that simple solutions with DateDiff are not good enough . As stated in a more popular question

DateDiff Function

喜你入骨 提交于 2019-12-02 05:46:14
I am trying some examples for DateDiff Function SELECT DATEDIFF(day,'2008-06-05','2008-08-05') AS DiffDate This statement gives me an error From keyword not found where expected. Why do I get this error and how can I solve it? Also, when I try this : SELECT DATEDIFF(day,datebegin,datestop) From table; I get this error "datediff" invalid identifier . How can I get day difference? What database are you using? A google search gave me this: http://www.mssqltips.com/sqlservertip/2508/sql-server-datediff-example/ DAY SELECT DATEDIFF(DD,'09/23/2011 15:00:00','08/02/2011 14:00:00') where 'DD' is used

MySQL Number of Days inside a DateRange, inside a month (Booking Table)

两盒软妹~` 提交于 2019-12-02 05:24:45
问题 I'm attempting to create a report for an accommodation service with the following information: Number of Bookings (Easy, use the COUNT function) Revenue Amount (Kind of easy). Number of Room nights. (Rather Hard it seems) Broken down into each month of the year. Limitations - I'm currently using PHP/MySQL to create this report. I'm pulling the data out of the booking system 1 month at a time, then using an ETL process to put it into MySQL. Because of this, I have duplicate records, when a

How can I use Datediff() in HQL

半世苍凉 提交于 2019-12-02 04:07:57
I'm trying to get the difference between dates. In my SQL SERVER it's works fine: Select CSERVICE_ORDER_ID FROM TSERVICE_ORDERS WHERE DATEDIFF(DAY, CDB_CREATE_DATE_TIME, CCANCELLATION_DATE) = 4 But in my HQL query I'm not getting it. The function Datefiff works in HQL Query? Is there any function with the same behavior? HQL doesn't support datediff, but if you still want to use datediff, you should use createNativeQuery() or createSQLQuery() to write that in sql. In your example, you just need the id anyway, not entity object, so this should be enough. or you can use in hql something like this

SQL Server find datediff between different rows, sum

僤鯓⒐⒋嵵緔 提交于 2019-12-02 03:52:28
I am trying to build a query that analyzes data in our time tracking system. Every time a user swipes in or out, it makes a row recording the swipe time and On or Off site (entry or exit). In user 'Joe Bloggs' case there are 4 rows, which I want to pair and calculate a total time spent on site for Joe Bloggs. The problem is that there are records that are not as easy to pair. In the example given, the second user has two consecutive 'on's, and I need to find a method for ignoring repeated 'on' or 'off' rows. ID | Time |OnOffSite| UserName ------------------------------------------------------

Calculate difference in months between two dates with Groovy

心不动则不痛 提交于 2019-12-02 03:24:08
问题 I need to calculate the difference in months between two dates. start = new Date(112, 4, 30) // Wed May 30 00:00:00 CEST 2012 end = new Date(111, 9, 11) // Tue Oct 11 00:00:00 CEST 2011 assert 8 == monthsBetween(start, end) Using Joda-Time it's really easy to achieve this with something like this: months = Months.monthsBetween(start, end).getMonths() How can I achieve this in a Groovy way, without using other libraries? 回答1: monthBetween = (start[Calendar.MONTH] - end[Calendar.MONTH]) + 1

Calculate difference in months between two dates with Groovy

萝らか妹 提交于 2019-12-02 00:38:10
I need to calculate the difference in months between two dates. start = new Date(112, 4, 30) // Wed May 30 00:00:00 CEST 2012 end = new Date(111, 9, 11) // Tue Oct 11 00:00:00 CEST 2011 assert 8 == monthsBetween(start, end) Using Joda-Time it's really easy to achieve this with something like this: months = Months.monthsBetween(start, end).getMonths() How can I achieve this in a Groovy way, without using other libraries? monthBetween = (start[Calendar.MONTH] - end[Calendar.MONTH]) + 1 yearsBetween = start[Calendar.YEAR] - end[Calendar.YEAR] months = monthBetween + (yearsBetween * 12) (start

Is SQL DATEDIFF(year, …, …) an Expensive Computation?

*爱你&永不变心* 提交于 2019-12-01 18:42:13
I'm trying to optimize up some horrendously complicated SQL queries because it takes too long to finish. In my queries, I have dynamically created SQL statements with lots of the same functions, so I created a temporary table where each function is only called once instead of many, many times - this cut my execution time by 3/4. So my question is, can I expect to see much of a difference if say, 1,000 datediff computations are narrowed to 100? EDIT: The query looks like this : SELECT DISTINCT M.MID, M.RE FROM #TEMP INNER JOIN M ON #TEMP.MID=M.MID WHERE ( #TEMP.Property1=1 ) AND DATEDIFF( year,

Why Datediff between GETDATE() and SYSDATETIME() in milliseconds is always different?

北慕城南 提交于 2019-12-01 14:45:58
I am trying to get Datediff between GETDATE() and SYSDATETIME() in milliseconds. SELECT DATEDIFF(ms, GETDATE() , SYSDATETIME()); The result I am getting is 0 or 1 or 2 or 3 . What is the reason for this difference? See this fiddle . They are two different function calls that can return two different times. Additionally GETDATE returns a datetime datatype which only has precision of 3-4 ms whereas SYSDATETIME() returns a datetime2(7) datatype. Even if both calls were to return exactly the same time you could see the issue that you are experiencing due to rounding. DECLARE @D1 DATETIME2 = '2012

Why Datediff between GETDATE() and SYSDATETIME() in milliseconds is always different?

帅比萌擦擦* 提交于 2019-12-01 12:48:22
问题 I am trying to get Datediff between GETDATE() and SYSDATETIME() in milliseconds. SELECT DATEDIFF(ms, GETDATE() , SYSDATETIME()); The result I am getting is 0 or 1 or 2 or 3 . What is the reason for this difference? See this fiddle. 回答1: They are two different function calls that can return two different times. Additionally GETDATE returns a datetime datatype which only has precision of 3-4 ms whereas SYSDATETIME() returns a datetime2(7) datatype. Even if both calls were to return exactly the