Thủ thuật xử lý DateTime trong MS SQL

Thủ thuật 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

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
How Digital Business Will Evolve in 2012: 6 Big Ideas

How Digital Business Will Evolve in 2012: 6 Big Ideas

In these often-trying economic times, a tide of sometimes overwhelming change in technology and its disruptive consequences has begun rolling over our organizations

Ngày 5 - Học về Big Data – NoSQL là gì?

Ngày 5 - Học về Big Data – NoSQL là gì?

NoSQL là viết tắt cho Not Relational SQL hay Not Only SQL. Nhiều người nghĩ rằng NoSQL có nghĩa là No SQL, là không đúng

5 Free Windows 7 Product Key Finder

5 Free Windows 7 Product Key Finder

Lost your Windows 7 OS Product / Serial key, don,t worry there are lots of tool available which allows you to find and recover Windows 7 Product key easily

Công ty diệt chuột T&C

 

Diet con trung