简体   繁体   中英

How to Sum Price from 6:00am till next day 6:00am

I need to sum price from current day 6:00am till next day 6:00am ( 24hour ). The problem is what if will run my code from 6:00am till 24:00(00:00) and what if I will run it from 24:01 till 6:00am ?

Should I create two exceptions for this or I can run it with one code?

My Sql Table Contains a DateTime column.

    var date = DateTime.Now;
            if ((date.Hour>=6&& date.Minute >0) && (date.Hour <= 24 && date.Minute < 59))
            {

            }
else if ((date.Hour>=0&& date.Minute >0) && (date.Hour <= 5 && date.Minute < 59))
            {

            }

Or if I can do it immediately with a sql query?

SELECT SUM(sumtotal) from customertrans where DATEPART(HOUR, DateTime)>=6 and DATEPART(HOUR, DateTime)<=24 and DATEPART(HOUR, DateTime)=DATEPART(HOUR, GETDATE())

If hour is from 24:00 till 6:00 I need to add also the previous day from 6:00 am till 24:00

Check this approch below:

var date = DateTime.Now;
        if ((date.Hour >= 6 && date.Minute >= 0) && (date.Hour <= 23 && date.Minute <= 59))
        {
            SqlCommand cmd = new SqlCommand("Select sum(sumTotal) from customertrans where datetime>='" + DateTime.Now.ToString("MM/dd/yyyy") + "' and DateTime >= DATEPART(HOUR, '6:00') ", con);
            string test = cmd.ExecuteScalar().ToString();
            MessageBox.Show(test);
        }
        else if ((date.Hour >= 0 && date.Minute >= 0) && (date.Hour <= 5 && date.Minute <= 59))
        {
            SqlCommand cmd = new SqlCommand("Select sum(sumTotal) datetime from customertrans where datetime>='" + DateTime.Now.AddDays(-1).ToString("MM/dd/yyyy") + "' and DateTime >= DATEPART(HOUR, '6:00') ", con);
            string test = cmd.ExecuteScalar().ToString();
            MessageBox.Show(test);
        }

from current day 6:00am till next day 6:00am (24hour)

Not true. Some days have 23:00 and others have 25:00, so bear that in mind. Given that you are calling this from C#, I find it a bit odd that you are doing stuff with DATEPART. Why not just do a ranged query on DateTime? If the field is indexed then you will avoid a table scan.

Why not something like:

        TimeSpan cutoverTime = TimeSpan.FromHours(6); // or however you have this defined

    // DateTime.Now is unstable, and the date may change between two consecutive calls
    // so read it into a temporary variable

    DateTime currentTime = DateTime.Now;
    DateTime startTime = currentTime.Date.Add(cutoverTime); // guess today
    if (startTime > currentTime)
    {
        // ok, guessed wrong, it was yesterday
        startTime = startTime.AddDays(-1);          
    }       
    DateTime endTime = startTime.AddDays(1);

    // And use parametised queries, not string concatenation (please)
    // SELECT SUM(sumtotal) from customertrans where DateTime >= @startTime and DateTime < @endTime

A further word of caution, you need to probably be thinking about timezones, maybe doing something with respect to UTCNow instead.

Use this in your where statement.

WHERE date BETWEEN FORMAT(GETDATE(), 'MM/dd/yyyy') + ' 06:00' AND FORMAT(GETDATE() + 1, 'MM/dd/yyyy') + ' 06:00'

This formats the current date in MM/dd/yyyy format then appends the time of 6:00 am to it (for start date), then does the same but adds 1 (which adds 1 day) to it to get tomorrows date.

For SQL 2008 you can do this:

where date BETWEEN CONVERT(VARCHAR(10), GETDATE(), 110) + ' 06:00' AND  CONVERT(VARCHAR(10), GETDATE() + 1, 110) + ' 06:00'

You can try this

DateTime start = date.Date.AddHours(6);
if ((date.Hour >= 6 && date.Minute >= 0) && (date.Hour <= 23 && date.Minute <= 59))
{
     sum = Sum(start, start.AddDays(1));
}
else if ((date.Hour >= 0 && date.Minute >= 0) && (date.Hour <= 5 && date.Minute <= 59))
{
     sum = Sum(start.AddDays(-1), start);
}

Where your sum method is declared like

public int Sum(DateTime start, DateTime end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM