I have 2 datetime columns in a table and I want to subtract each row one from another and somehow put results in new 3rd column rows as int.
I know I have to use DATEFIFF somehow, but what confuses me even more how do I parse each row of a table (while loop perhaps)?
Thank you!
Example to go by from SQL 2008 assuming Date1Column and Date2Column are datetime fields. I gave a few examples showing delta between dates in minutes, days, months.
Select
,CAST(DATEDIFF(mi, Date1Column, Date2Column) as int) as 'DeltaMinutes'
,CAST(DATEDIFF(day, Date1Column, Date2Column) as int) as 'DeltaDays'
,ISNULL(DATEDIFF(MONTH, Date1Column, Date2Column), -9999) as 'LastSWAge'
From Table
Related
I have two tables with a time column (year-day-month hr:min:sec)
Let's say name of table 1 is plc and column name Collect
Name of table2 is Adm and column name Disc
I want to subtract the time of Collect (2005-01-03 18:10:05) from the disc column (2005-01-03 20:15:10) in day, hours, minutes.
Any help would be appreciated!
I would surely go for the datediff function.
Have a look at this link:
https://msdn.microsoft.com/en-us/library/ms189794(v=sql.90).aspx
If you want, you can select difference in minutes, and then, with the minutes, calculate the days and hours
Use Date() or Datepart() function to fetch the specific part of the date and subtract the two.
You can get more details of the above two at the following link:
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql
I'm trying to obtain the total amount of time difference from two timestamp columns (datetime)
I currently have a Table 1 setup like the following:
Time_Line_Down => datetime
Time_Line_Ran => datetime
Total_Downtime => Computed column with formula:
(case when [Time_Line_Down] IS NULL then NULL else CONVERT([varchar],case when [Time_Line_Ran] IS NULL then NULL else [Time_Line_Ran]-[Time_Line_Down] end,(108)) end)
Every time some conditions occur, I am copying those three columns (I have more columns but the problem is on this ones) into another Table 2 originally setup like the following:
Time_Line_Down => datetime
Time_Line_Ran => datetime
Total_Downtime => datetime
I then use an excel spreadsheet to "Get External Data" from SQL Server and use a pivot table to work with the data.
Example
Time_Line_Down = 2015-02-20 12:32:40.000
Time_Line_Ran = 2015-02-20 12:34:40.000
Total_Downtime = 1900-01-01 00:02:00.000
Desired Output
I want the pivot table to be able to give me a Grand Total of downtime from all rows in that table
Let's say it was forty five hours, fifty minutes and thirty seconds of accumulated downtime it should read like (45:50:30)
The problem:
Even if I format the Total_Downtime column in the excel pivot table as h:mm:ss to read like this:
Total_Downtime = 0:02:00
As rows accumulate and the Grand Total is calculated the "Date" part of the timestamp is messing the result is the total exceeds 24 hours
What I have tried
I changed the data type format of column Total_Downtime in Table 2 to time(0) so that it won't send the "Date" part, only the "Time" part of the timestamp, it is working and reads out 00:02:00
But now all the values in my pivot table on excel for that column are 0:00:00 no matter what value is actually in the SQL table.
Any suggestions?
You can use the Excel time format [h]:mm:ss which can go beyond 24 hours.
Alternatively, you can use the SQL function DATEDIFF to get the total downtime in seconds, and then convert that to however you need to display it in Excel, e.g.
case when [Time_Line_Down] IS NULL then NULL else case when [Time_Line_Ran] IS NULL then NULL else datediff(ss, Time_Line_Ran, Time_Line_Down) end end
I don't think you need the CASE statements here, you can just use
datediff(ss, Time_Line_Ran, Time_Line_Down)
Thank you all for your help,
I went ahead an tried the function DATEDIFF as suggested, I changed Table 1 computed column formula and Table 2 Total_Downtime column data type to int. Once imported into excel this numeric value needed some extra calculations.
In principle is the best answer and should work for anyone trying to calculate the difference from two timestamps, as mentioned before, is pretty straight forward.
But in my situation I needed to maintain two things:
1) The format 00:00:00 for the column Total_Downtime in Table 1, which changed to an integer value when using DATEDIFF
2) The pivot table Total_Downtime column format [h]:mm:ss (suggested by TobyLL) in excel, which required several calculations to convert from seconds
Solution
After learning that every time I copied from Table 1 to Table 2 the computed value (e.g. 00:02:00) changed to 1900-01-01 00:02:00.000 and that when imported to Excel it equaled to 1.001388889, I decided to force the "Date" part of the time stamp to be 1899-12-31 so that Excel would only calculate the Grand total in the pivot table with the "Time" (decimal) part.
I would like to have a date and time column in my table. The main purpose of having these 2 columns is to be able to return query results like:
Number of treatments done in the period November 2011.
Number of people working in shifts between 00:01 and 08:00 hours.
I have two tables, which have the following attributes in them(among others):
Shift(day, month, year)
Treatment(start_time, date)
For the first table- Shift, query results need to return values in
terms of (ex: December 30,2012)
For the second table, start_time needs to have values like 0001 and
0800(as I mentioned above). While, date can return values like
'November 2011'.
Initially I thought using the date datatype for declaring each of the day/month/year/date variables would do the job. But this doesn't seem to work out. Should I use int, varchar and int respectively for day, month and year respectively? Also, since the date variable does not have component parts, will date datatype work here? Lastly, if I use timestamp data type for the start_time attribute, what should be the value I enter in the insert column- should it be 08:00:00?
I'm using SQL Server 2014.
Thank You for your help.
AFAIK it is better to use one column by type of DateTime instead of two columns which hold Date and Time separately.
Also you could simply query this column either by Date or Time by casting it to corresponding type :
DECLARE #ChangeDateTime AS DATETIME = '2012-12-09 16:07:43.937'
SELECT CAST(#ChangeDateTime AS DATE) AS [ChangeDate],
CAST(#ChangeDateTime AS TIME) AS [ChangeTime]
results to :
ChangeDate ChangeTime
---------- ----------------
2012-12-09 16:07:43.9370000
Long title, easy meaning:
How is it possible to extract from a date like "2014-04-04 10:47:30.000", which is stored in one column, it's components like year, month and day?
I'm not interested in the time.
For example, I have a table called "Incidents". Inside the table we got a column called "IncidentID" and a column called "ReportingDate", in which dates like the above-mentionend are stored. Let's say we have about 50k Incidents, therefore we have also 50k dates.
A year has 365 days. I want to query for the count of the Incidents, which were reported on different dates - for instance on the 5th of October 2013.
So: How can I get the components of the date and put them into another table while having own columns for the components and how can I query for the Incidents as well?
I guess at first I have to change the datatype of the date from DATETIME to DATE, but I'm not quite sure how to go further. May anyone help me while giving me a code and explains me what it does for a sql-noob? :-)
To achieve this
I want to query for the count of the Incidents, which were reported on
different dates - for instance on the 5th of October 2013.
you haven't do this:
I guess at first I have to change the datatype of the date from
DATETIME to DATE, but I'm not quite sure how to go further.
Just query
SELECT
IncidentID
FROM incidents
WHERE ReportingDate >= '20131005'
AND ReportingDate < '20131006'
I have this simple data base:
7/2/2013
7/13/2013
I write a simple SQL statement to select the greatest date from a list of date. I try to use the (max function) as follow:
select max([P_Date]) from [BalDB].[dbo].[tab_Product]
The result was incorrect; it gives me the smallest date not the greatest as follow:
7/2/2013
So please help me to know what is the problem in my SQL statement and how can I solve it
Problem: Get the greatest date from a list of date or compare it with local date and take the greater!!
The sql max function returns the largest value of the selected column, in your case since your data type is a nvarchar the largest value is what is alphabetically larger, which in this case is 7/2/2013 (since the "2" is greater then the "1" in "13").
What you need to do is basically what #David mentioned, either chance the data type of the column or if it isn't feasible then you can cast it in your query as a datetime
For example
select max(cast([P_Date] as datetime)) from [BalDB].[dbo].[tab_Product]
The max function is making this slower than it needs to be, try this.
select top 1 convert(datetime,P_Date) from [BalDB].[dbo].[tab_Product] order by convert(datetime,P_Date) desc
Now your dates should be date types, not varchars, but for the sake of querying your data as is, this will work.