SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to calculate Average datetime in sql server


How to calculate Average datetime in sql server

Author
Message
malleswarareddy_m
malleswarareddy_m
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2193 Visits: 1189
DECLARE @date1 DATETIME,@date2 DATETIME,@date3 DATETIME;
SET @date1=CONVERT(varchar,GETDATE()+1,108)
SET @date2=CONVERT(varchar,GETDATE()+2,108)
SET @date3=CONVERT(varchar,GETDATE()+3,108)

SELECT * INTO #temp
FROM(
SELECT @date1 AS date
UNION
SELECT @date2 AS date
UNION
SELECT @date3 AS date) AS p


Can any one send how to calculate the average of datetime in sql server 2005 and avg function is not working and i have no permission to create cursor or functions in a database i only able to create temp tables.

Malleswarareddy
I.T.Analyst
MCITP(70-451)
Gianluca Sartori
Gianluca Sartori
SSCrazy Eights
SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)

Group: General Forum Members
Points: 9808 Visits: 13350
Convert the date column to FLOAT, then back to DATETIME:


DECLARE @test TABLE (
dtColumn datetime
)

INSERT INTO @test VALUES (GETDATE() + RAND())
INSERT INTO @test VALUES (GETDATE() + RAND())
INSERT INTO @test VALUES (GETDATE() + RAND())
INSERT INTO @test VALUES (GETDATE() + RAND())


SELECT CAST(AVG(CAST(dtColumn AS FLOAT)) AS datetime)
FROM @test



Hope this helps
Gianluca

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
malleswarareddy_m
malleswarareddy_m
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2193 Visits: 1189
Hi thanks for reply,

this above code is fine but it will take the average time not full datetime.

I want to calculate time not average of date.

Malleswarareddy
I.T.Analyst
MCITP(70-451)
Gianluca Sartori
Gianluca Sartori
SSCrazy Eights
SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)

Group: General Forum Members
Points: 9808 Visits: 13350
Just the time part? OK, take the fractional part only:


DECLARE @test TABLE (
dtColumn datetime
)

INSERT INTO @test VALUES (GETDATE() + RAND())
INSERT INTO @test VALUES (GETDATE() + RAND())
INSERT INTO @test VALUES (GETDATE() + RAND())
INSERT INTO @test VALUES (GETDATE() + RAND())


SELECT CONVERT(char(8),CAST(AVG(CAST(dtColumn AS FLOAT) - FLOOR(CAST(dtColumn AS FLOAT))) AS datetime),108)
FROM @test



--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
malleswarareddy_m
malleswarareddy_m
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2193 Visits: 1189
ok.Its working fine

Malleswarareddy
I.T.Analyst
MCITP(70-451)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search