SQL Server: DateDiff()
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 252   1
URL.หัวข้อ / URL
SQL Server: DateDiff()

มีคนถามไว้ที่ http://www.tek-tips.com/viewthread.cfm?SQID=641587&SPID=183&page=4

เขามีวันที่เริ่มต้น และวันที่สิ้นสุด และต้องการหาผลต่างของทั้ง 2 วัน เป็น วัน ชั่วโมง และนาที อย่างนี้Start End Days Hours Minutes
18/08/2003 14:00 26/08/2003 13:55 7 23 55

ผมเลยลองทำแบบนี้

SET DATEFORMAT dmy --set Date format
GO
DECLARE @datestart datetime
DECLARE @dateend datetime
DECLARE @minutes int
DECLARE @days int
SET @days = (24*60) -- one day has 24*60 mins
SET @datestart = '18/08/2003 14:00'
SET @dateend = '26/08/2003 13:55'
set @minutes = DateDiff(minute,@datestart,@dateend)
select @minutes / @days As 'DAYS',
(@minutes % @days)/60 as 'HRS',
(@minutes % @days)%60 As 'MINS'
GO

จะได้
DAYS HRS MINS
----------- ----------- -----------
7 23 55


1 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R06800
   
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0666s