If I want to add ‘period’ in the database (one month), and would like to make calculations with the first day of the period. What would I have to do? [closed]

1

I have a question.

I want to add ‘period’ in the database. A period would always be 1 month. How Would I be able to make calculations with the period?

For example I want to calculate the following:

A = Last day of Current Period – Last day of Previous Period

How would I be able to make the system understand what the last day of the period is? And the previous period?

That could be for example 28, 30 or 31. Could anyone please help me out.

In this case it would be:

31th of October – 30th of September

Thanks!

4

3

Calculating anything involves behavior. Database tables store data, not behavior. When dealing with dates, choose a solution that properly models all of the crazy rules around dates. There is no way to store data that calculates something. Instead, store data that describes the period.

Your program can query the database for this information. Based on the type of period and duration, you would need to write programming code to calculate this. Programming languages have built-in library functions or data types to handle date calculations. For example, if implementing this in the .NET technology stack, the DateTime type should be used.

You could write stored procedures in the database to handle the date calculations, but only choose this option if the database vendor has a good set of library functions for calculating dates.

0

Most modern SQL databases provide you with the functions for doing this. For example, with MS SQL Server, all you need for calculating what you asked for are the functions

  • YEAR, MONTH, DAY, DATEFROMPARTS, DATEADD and DATEDIFF

(See https://www.w3schools.com/sql/sql_ref_sqlserver.asp for a full reference or other databases).

Assumed you have a column myDate and want to calculate the value A from your question, this can be a little tricky with these functions. I would approach it this way:

  • add one month to myDate: DATEADD(month,1,mydate)

  • get the month and year from that, replace the day of the month by 1, and subtract one day:

     DATEADD(day,-1, DATEFROMPARTS(YEAR(DATEADD(month,1,mydate)), MONTH(DATEADD(month,1,mydate)), 1))
    

This gives you the last day of the current month.

Now do the same for DATEADD(month, -1, myDate), this gives you the last day of the previous period. Finally, use DATEDIFF to calculate the difference.

Of course, trying to cram all this stuff into one large SQL statement is not the best idea, hence you may consider to use a stored procedure where you can provide some variables for the intermediate values.

I am sure there is a simpler solution out there, but the approach above is almost straightforward and should not be to hard to understand.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật