SQL
Vấn đề debug trong VS 2005
Từ khi chuyển sang laptop mới phải cài đặt bao nhiêu thứ để có công cụ làm việc, nhưng nhiều lúc gặp phải lỗi cũng không biết là do gì nữa. Như mấy hôm nay gặp phải cái lỗi cực chuối, chẳng là khi làm với ASP.NET hay debug luôn trong VS 2005, nhưng không hiểu tại sao cứ debug connect đến cơ sở dữ liệu thì toàn bị "request time out" và hiện ra thông báo dạng như thế này "Ensure that client side components such as SQLLE.DLL are installed and registered on SQLSERVER_COMPUTERNAME". Thật là bực mình vì ngồi lập trình mà không debug được khác nào mò mẫm. Sau vài lần lân la lên hỏi ông google cũng không tìm được giải pháp thích hợp, tự dưng hôm nay chợt nghĩ đến cái Firewall trong XP, thế là disable nó tự dưng lại debug ngon lành. Chắc hẳn thằng Firewall nó chặn cái port hoặc program gì đó làm cho VS 2005 debug không suôn sẻ nữa. Tuy chưa biết rõ nguyên nhân nhưng cũng là một kinh nghiệm...
Blog's topic:
- huyvq's blog
- Log in to post comments
- 2345 reads
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
- 33679 reads