Xử lý DateTime trong MS SQL
Hôm nay phải động đến vấn đề xử lý datetime trong SQL, lướt qua trên mạng thu thập về các hàm tiện ích phục vụ cho công việc. Các hàm cơ bản mà MS SQL cung cấp gồm có DATEADD, DATEDIFF, từ những hàm cơ bản này chúng ta có thể tạo ra những hàm tiện ích khác cho công việc gặp phải.
DATEADD(param1, amount, @datetime): Trả về kiểu datetime dựa vào thời điểm cho trước và khoảng thời gian thêm vào. Tham số param1, xác định đơn vị của khoảng thời gian thêm vào như yy(yyyy) cho năm, mm(m) cho tháng, dd(d) cho ngày. Ví dụ DateAdd(dd, 5, GetDate()).
DATEDIFF(<interval>, <startDate>, <endDate>): Trả về khoảng thời gian giữa 2 thời điểm cho trước.
<interval> có thể là các giá trị sau:
- year
- quarter
- month
- dayofyear
- day
- week
- hour
- minute
- second
- millisecond
Các hàm tiện ích
create function DateOnly(@DateTime DateTime)
-- Returns @DateTime at midnight; i.e., it removes the time portion of a DateTime value.
returns datetime
as
begin
return dateadd(dd,0, datediff(dd,0,@DateTime))
end
go
create function Date(@Year int, @Month int, @Day int)
-- returns a datetime value for the specified year, month and day
-- Thank you to Michael Valentine Jones for this formula (see comments).
returns datetime
as
begin
return dateadd(month,((@Year-1900)*12)+@Month-1,@Day-1)
end
go
create function Time(@Hour int, @Minute int, @Second int)
-- Returns a datetime value for the specified time at the "base" date (1/1/1900)
-- Many thanks to MVJ for providing this formula (see comments).
returns datetime
as
begin
return dateadd(ss,(@Hour*3600)+(@Minute*60)+@Second,0)
end
go
create function TimeOnly(@DateTime DateTime)
-- returns only the time portion of a DateTime, at the "base" date (1/1/1900)
returns datetime
as
begin
return @DateTime - dbo.DateOnly(@DateTime)
end
go
create function DateTime(@Year int, @Month int, @Day int, @Hour int, @Minute int, @Second int)
-- returns a dateTime value for the date and time specified.
returns datetime
as
begin
return dbo.Date(@Year,@Month,@Day) + dbo.Time(@Hour, @Minute,@Second)
end
go
- huyvq's blog
- Log in to post comments
- 33703 reads
Comments
thinh (not verified)
Thu, 02/19/2009 - 16:27
Permalink
Làm sao lấy second và milisecond ra khỏi giá trị trả về của GETD
Hi,
Thấy bác có vẽ pro, mình muốn hỏi. Khi lấy giá trị tra về của hàm GETDATE(), mình muốn lấy chẳn đến phút thôi. Ví dụ: 17/02/2009 2:58:27 PM, mình chỉ muốn lấy đến 17/02/2009 2:58 PM thôi.
Nhờ chỉ giáo.
Thịnh
dido (not verified)
Sat, 12/19/2009 - 20:49
Permalink
Very good post, thanks a lot.
Very good post, thanks a lot.
Anonymous (not verified)
Mon, 03/30/2009 - 15:13
Permalink
xin lỗi, ai thiết kế
xin lỗi, ai thiết kế cái box "các hàm tiện ích" => quá tệ, đảm bảo người đọc sẽ phát khùng, ko tin xin mời thử đọc
huyvq
Mon, 03/30/2009 - 16:24
Permalink
Hic hic, bạn phải cho
Hic hic, bạn phải cho mình biết nó tệ thế nào còn sửa chứ :(
khung roi (not verified)
Tue, 03/30/2010 - 14:53
Permalink
thấy j đâu mà cảm ơn chời
thấy j đâu mà cảm ơn chời
Anonymous (not verified)
Sat, 05/29/2010 - 19:54
Permalink
tệ ở chỗ chữ quá nhỏ ko đọc
tệ ở chỗ chữ quá nhỏ ko đọc đc gì ráo - nếu cận thì bực chửi là phải rồi heheh
Anonymous (not verified)
Sat, 01/08/2011 - 09:05
Permalink
mình có 3 cột riêng lẻ ngày,
mình có 3 cột riêng lẻ ngày, tháng, năm
mình muốn tạo thành một cột dd/mm/yyyy cột năm mà có khoảng trắng thì không thực hiện thì phải làm sao xin chỉ zùm?!!.
Anonymous (not verified)
Wed, 07/06/2011 - 11:11
Permalink
nếu bác chỉ muốn lấy tới phút
nếu bác chỉ muốn lấy tới phút thôi thì bác tạo một biến kiểu timestamp(10) rồi gán lấy giá trị getdate.
Anonymous (not verified)
Sat, 07/16/2011 - 17:09
Permalink
thanks thanks you!
thanks
thanks you!