Some issues to keep in mind when querying data from an Oracle database for the first few months

Source: Internet
Author: User

In a recent project, there is a requirement is to query the database in the previous months of historical data, but due to their own consideration does not completely cause the program bug, now this piece to make a good summary, I hope to no longer make this very low-level error, first put out a query used in a sub-function, And then make a detailed analysis of this:

 private string AddMonths (string originaltime, int months) {string returnstring = string.            Empty;            string[] Dataandtime = Originaltime.split (new char[] {"});                if (Dataandtime.length > 0) {string date = ""; if (!string.                    Isnullorwhitespace (Dataandtime[0]) {date = dataandtime[0]; string[] Yearmonthday = date.                    Split (new char[] {'/'}); int year = Int.                    Parse (Yearmonthday[0]); int month = Int.                    Parse (yearmonthday[1]); int day = Int.                    Parse (yearmonthday[2]);                            if (months! = 0) {if (month > months) {                            month = month-months;                            Determine the total number of days of the month int day = Datetime.daysinmonth (year, month);               if (Day > days)                 Day = days; returnstring = year. ToString () + "/" + month. ToString () + "/" + day.                        ToString () + "" + dataandtime[1];                            } if (month ==months) {month = 12;                            Year = year-1;                            int days = Datetime.daysinmonth (year, month);                            if (Day > Days) day = days; returnstring = year. ToString () + "/" + month. ToString () + "/" + day.                                                ToString () + "" + dataandtime[1]; } if (Month < months) {month = month + 12-m                            Onths;                            Year = year-1;                            int days = Datetime.daysinmonth (year, month); if (Day > Days) Day = days; returnstring = year. ToString () + "/" + month. ToString () + "/" + day.                        ToString () + "" + dataandtime[1];                    }} else {returnstring = Originaltime;        }}} return returnstring; }

There are two parameters in this function, the first originaltime represents the most recent date queried from the database, the second parameter is months, which indicates how many months to push forward from the last date, and in our program we first get the current year and month by dividing the string. Comparing our current month with the query period months, the first case is that the current month is greater than the query period, and the last month that gets is the month and the months, where the year does not need to change, but one thing that must be noted is the value of day, For example, the query from the database is exactly December 31, at this time if the query period is 6 months, the direct return to the difference of June 31, the procedure will inevitably be wrong, because there is no 31st in June, this must be paid attention to, so we have the following judgment in the procedure,

int days = Datetime.daysinmonth (year, month)

if (Day > days)

Day = days;

There's a function in it. Datetime.daysinmonth (year, month) This function is used to determine how many days in a certain January, if the current date is more than the number of days after the date of the query, then only the maximum number of days in the month can be taken, Just like December 31 forward 6 months must be June 30 instead of June 31, these simple logic problems must not be wrong, this is to pay special attention to. According to the relationship between month and months must be discussed in three cases, namely: Month>months and Month=months and month<months three cases to discuss one can not be less, in short, the date of return must be reasonable year, The month must be between 1 and 12, the returned day must be within the reasonable range of the month, this need to write code when very careful, and timely summary, did not learn from experience and lessons.

Some issues to be aware of when querying data from an Oracle database for the first few months

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.