Convert The Difference Between StartDate and EndDate Into Hours, Minutes And Seconds - tsql

Suppose I have a StartDate and an EndDate, how do I format or convert the difference into hh:mm:ss?
Thank you.

Try this
SELECT CONVERT(Varchar(10), Dateadd(Second, DATEDIFF(Second,StartDate,EndDate),0), 108)
For Example:
DECLARE #StartDate DateTime
DECLARE #EndDate DateTime
SET #StartDate = '2014-06-08 07:18:12.893'
SET #EndDate = '2014-06-09 05:58:40.893'
SELECT CONVERT(Varchar(10), Dateadd(Second, DATEDIFF(Second,#StartDate,#EndDate),0), 108)
Output:
+---------+
| Time |
+---------+
|22:40:28 |
+---------+
Fiddle Demo

Related

Postgresql extraction between two DateTime but ingnore only seconds

Postgresql extraction between two DateTime but ignore only seconds (seconds for two datetime = 00)
06.09.2014 18:54:35 - 06.09.2014 18:54:35
DD.MM.YYYY HH24:MI - DD.MM.YYYY HH24:MI
It sounds like you're looking for the date_trunc() function.
with test_data as (
select timestamp '2015-01-01 08:00:13' as ts union all
select timestamp '2015-01-01 08:13:27' union all
select timestamp '2015-01-01 09:00:27' union all
select timestamp '2015-01-01 09:01:42'
)
select *, date_trunc('minute', ts)
from test_data
where date_trunc('minute', ts) between '2015-01-01 08:00' and '2015-01-01 09:00';
ts date_trunc
--
2015-01-01 08:00:13 2015-01-01 08:00:00
2015-01-01 08:13:27 2015-01-01 08:13:00
2015-01-01 09:00:27 2015-01-01 09:00:00
If you need this kind of query to use an index, you'll need to create an index on the expression date_trunc('minute', your_column_name).

Update in sql server 2012 only time part of a date

Is there a way to perform an update in a sql server 2012 database but only of the time part?
I have this data:
id | date
1 | 2013-09-01 15:25:58.526
2 | 2013-05-10 12:12:34.345
3 | 2013-05-10 11:23:33.234
4 | 2013-04-07 15:34:01.345
And i want to change all the times to 00:00:00.000... how can i do that?
UPDATE YOUR_TABLE
SET date = CAST(
FLOOR(
CAST( date AS FLOAT )
) AS DATETIME
)
You can try
UPDATE Table1 SET [date] = CAST([date] as DATE)
SQL Fiddle DEMO
Another option would be something like
UPDATE Table1 SET [date] = DATEADD(dd, 0, DATEDIFF(dd, 0, [date]));
SQL Fiddle DEMO

PostgreSQL Select Sum of Number, Group by Date

I'm not sure if there is question like this out there already, couldn't find my solution, so sorry if this is a duplicate:
I have a table with dates:
|date (date)| tax (numeric) | (stuff in brackets is the type)
|2012-12-12 | 5.00 |
|2012-12-12 | 10.00 |
|2012-12-13 | 2.00 |
I want my output to look like this:
|date (date)| tax (numeric) | (stuff in brackets is the type)
|2012-12-12 | 15.00 |
|2012-12-13 | 2.00 |
I was thinking of doing a CTE type query because in the datebase, I store things as datetime without timezone, and a bunch of taxes
This is the start of my query:
with date_select as
(
select CAST(r.datetime as DATE), sum(r.tax) as tax
from suezensalon.receipt r
where r.datetime between '2012-12-12 00:00:00' and '2012-12-18 23:59:59'
group by r.datetime
order by r.datetime
)
This gives me the top table. What is the best way to do this? Is it by 'averaging the date'?
This is what ended up working:
with date_select as
(
select CAST(r.datetime as DATE), sum(r.tax) as tax
from suezensalon.receipt r
where r.datetime between '2012-12-12 00:00:00' and '2012-12-18 23:59:59'
group by r.datetime
order by r.datetime
)
select extract(Month from datetime) || '-' || extract(Day from datetime) || '-' || extract(Year from datetime) as Date, sum(tax)
from date_select
group by Date
order by Date;
I think the simplest option, and most likely to perform well in the presence of indexes, would be something like this:
SELECT
datetime::date as "Date",
sum(tax)
FROM suezensalon.receipt
WHERE datetime >= '2012-12-12 00:00:00'
AND datetime < '2012-12-19 00:00:00'
GROUP BY datetime::date
ORDER BY "Date";

Truncate Datetime to Second (Remove Milliseconds) in T-SQL

What is the best way to shorten a datetime that includes milliseconds to only have the second?
For example 2012-01-25 17:24:05.784 to 2012-01-25 17:24:05
This will truncate the milliseconds.
declare #X datetime
set #X = '2012-01-25 17:24:05.784'
select convert(datetime, convert(char(19), #X, 126))
or
select dateadd(millisecond, -datepart(millisecond, #X), #X)
CAST and CONVERT
DATEADD
DATEPART
The fastest, also language safe and deterministic
DATEADD(second, DATEDIFF(second, '20000101', getdate()), '20000101')
The easiest way now is:
select convert(datetime2(0) , getdate())
convert(datetime, convert(varchar, #datetime_var, 120), 120)
The following has very fast performance, but it not only removes millisecond but also rounds to minute. See (http://msdn.microsoft.com/en-us/library/bb677243.aspx)
select cast(yourdate as smalldatetime) from yourtable
Edit:
The following script is made to compare the scripts from Mikael and gbn I upvoted them both since both answers are great. The test will show that gbn' script is slightly faster than Mikaels:
declare #a datetime
declare #x int = 1
declare #mikaelend datetime
declare #mikael datetime = getdate()
while #x < 5000000
begin
select #a = dateadd(millisecond, -datepart(millisecond, getdate()), getdate()) , #x +=1
end
set #mikaelend = getdate()
set #x = 1
declare #gbnend datetime
declare #gbn datetime = getdate()
while #x < 5000000
begin
select #a = DATEADD(second, DATEDIFF(second, '20000101', getdate()), '20000101') , #x +=1
end
set #gbnend = getdate()
select datediff(ms, #mikael, #mikaelend) mikael, datediff(ms, #gbn, #gbnend) gbn
First run
mikael gbn
----------- -----------
5320 4686
Second run
mikael gbn
----------- -----------
5286 4883
Third run
mikael gbn
----------- -----------
5346 4620
declare #dt datetime2
set #dt = '2019-09-04 17:24:05.784'
select convert(datetime2(0), #dt)
Expanding on accepted answer by #Mikael Eriksson:
To truncate a datetime2(7) to 3 places (aka milliseconds):
-- Strip of fractional part then add desired part back in
select dateadd(nanosecond,
-datepart(nanosecond, TimeUtc) + datepart(millisecond, TimeUtc) * 1e6,
TimeUtc) as TimeUtc
The current max precision of datetime2(p) is (7) (from learn.microsoft.com)
--- DOES NOT Truncate milliseconds
--- 2018-07-19 12:00:00.000
SELECT CONVERT(DATETIME, '2018-07-19 11:59:59.999')
--- Truncate milliseconds
--- 2018-07-19 11:59:59.000
SELECT CONVERT(DATETIME, CONVERT(CHAR(19), '2018-07-19 11:59:59.999', 126))
--- Current Date Time with milliseconds truncated
SELECT CONVERT(DATETIME, CONVERT(CHAR(19), GETDATE(), 126))
SELECT CAST( LEFT( '2018-07-19 11:59:59.999' , 19 ) AS DATETIME2(0) )

Transact SQL help

Having trouble getting this to work have a web app using visual studios 2010 on front end and sql2008r2 backend gets this error
"Error inserting record.Conversion failed when converting date and/or time from character string."
I have these fields and datatypes
StartDate value=date
StartTime value=varchar(8)
EndDate value = date
EndTime value=varchar(8)
I am trying to add both startdate + StartTime into a customstartdate field but it is getting an error converting
convert(varchar(10), #StartDate,101) +
stuff(right(convert(varchar(26),#StartTime,109 ),15),7,7, ' ' ),
convert(varchar(10),#EndDate, 101) +
stuff(right(convert(varchar(26),#EndTime,109 ),15),7,7, ' ' ),
You should test your conversions in SQL Management Studio to see what it returns.
declare #StartDate datetime = getdate()
declare #EndDate datetime = getdate()
declare #StartTime varchar(8) = '10:00:00'
declare #EndTime varchar(8) = '11:00:00'
select
convert(varchar(10), #StartDate,101) +
stuff(right(convert(varchar(26),#StartTime,109 ),15),7,7, ' ' ) as StartDate,
convert(varchar(10),#EndDate, 101) +
stuff(right(convert(varchar(26),#EndTime,109 ),15),7,7, ' ' ) as EndDate
Result
StartDate EndDate
------------------- -------------------
03/23/201110:00: 03/23/201111:00:
You could also break your conversion into parts so you see what happens
select
convert(varchar(10), #StartDate,101) as DatePart,
convert(varchar(26),#StartTime,109) as Time26,
right(convert(varchar(26),#StartTime,109 ),15) as Time15,
stuff(right(convert(varchar(26),#StartTime,109 ),15),7,7, ' ' ) as TimeStuff
Result
DatePart Time26 Time15 TimeStuff
---------- -------------------------- --------------- ---------
03/23/2011 10:00:00 10:00:00 10:00:
This is what I would do. The result is a datetime.
select
convert(datetime, convert(varchar(10), #StartDate, 101)+' '+#StartTime) as StartDate,
convert(datetime, convert(varchar(10), #EndDate, 101)+' '+#EndTime) as EndDate
Result
StartDate EndDate
----------------------- -----------------------
2011-03-23 10:00:00.000 2011-03-23 11:00:00.000