How to get Sum(value) between two dates using MySQL and CSharp?JPJose Palmieri4yAsked Mar 21, 2022, 6:55 PM EDT2 answersShareLinkedInXCopy linkSaveI'am using the SQL bellow, MySQL and C# string str = "Select Sum(value) from where and date between (date1) and (date2)"; I need to get the result of this SQL on C#, how do I do that?2 AnswersKnow the answer? Post it. Somebody with the same question will find it here.Muhammad Imran AnsariRank #326 · 7.6K reputation4y agoPosted Mar 21, 2022, 10:18 PM EDT0Step-1: Write a storeprocedure with data parameters at SQL side to calculate and get the sum of value. like:ALTER PROCEDURE sp_getSum @fromDate AS DATE, @toDate AS DATE AS BEGIN SELECT SUM(ISNULL(valueCol, 0)) FROM yourTable WHERE dateCol BETWEEN @fromDate AND @toDate END Step-2: Call this store procedure in C# to get the result. like:SqlConnection conn = new SqlConnection(); conn.ConnectionString = "YourConnectionString"; // Add your SQL Connection String here using (SqlCommand cmd = new SqlCommand("sp_getSum", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@fromDate", "2022-03-01"); cmd.Parameters.AddWithValue("@toDate", "2022-03-10"); if (conn.State != ConnectionState.Open) conn.Open(); int sumVal = Convert.ToInt32(cmd.ExecuteScalar()); } 0Gajendra JangidRank #157 · 14.9K reputation4y agoPosted Mar 21, 2022, 10:35 PM EDT0Hi, Use below codes Sqlcreate proc usp_getTotal @date2 as date, @date2 as date as begin SELECT SUM(ISNULL(col, 0)) FROM tableName WHERE date BETWEEN @date1 AND @date2 end 0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
How to get Sum(value) between two dates using MySQL and CSharp?JPJose Palmieri4yAsked Mar 21, 2022, 6:55 PM EDT2 answersShareLinkedInXCopy linkSaveI'am using the SQL bellow, MySQL and C# string str = "Select Sum(value) from where and date between (date1) and (date2)"; I need to get the result of this SQL on C#, how do I do that?2 AnswersKnow the answer? Post it. Somebody with the same question will find it here.Muhammad Imran AnsariRank #326 · 7.6K reputation4y agoPosted Mar 21, 2022, 10:18 PM EDT0Step-1: Write a storeprocedure with data parameters at SQL side to calculate and get the sum of value. like:ALTER PROCEDURE sp_getSum @fromDate AS DATE, @toDate AS DATE AS BEGIN SELECT SUM(ISNULL(valueCol, 0)) FROM yourTable WHERE dateCol BETWEEN @fromDate AND @toDate END Step-2: Call this store procedure in C# to get the result. like:SqlConnection conn = new SqlConnection(); conn.ConnectionString = "YourConnectionString"; // Add your SQL Connection String here using (SqlCommand cmd = new SqlCommand("sp_getSum", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@fromDate", "2022-03-01"); cmd.Parameters.AddWithValue("@toDate", "2022-03-10"); if (conn.State != ConnectionState.Open) conn.Open(); int sumVal = Convert.ToInt32(cmd.ExecuteScalar()); } 0Gajendra JangidRank #157 · 14.9K reputation4y agoPosted Mar 21, 2022, 10:35 PM EDT0Hi, Use below codes Sqlcreate proc usp_getTotal @date2 as date, @date2 as date as begin SELECT SUM(ISNULL(col, 0)) FROM tableName WHERE date BETWEEN @date1 AND @date2 end 0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Muhammad Imran AnsariRank #326 · 7.6K reputation4y agoPosted Mar 21, 2022, 10:18 PM EDT0Step-1: Write a storeprocedure with data parameters at SQL side to calculate and get the sum of value. like:ALTER PROCEDURE sp_getSum @fromDate AS DATE, @toDate AS DATE AS BEGIN SELECT SUM(ISNULL(valueCol, 0)) FROM yourTable WHERE dateCol BETWEEN @fromDate AND @toDate END Step-2: Call this store procedure in C# to get the result. like:SqlConnection conn = new SqlConnection(); conn.ConnectionString = "YourConnectionString"; // Add your SQL Connection String here using (SqlCommand cmd = new SqlCommand("sp_getSum", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@fromDate", "2022-03-01"); cmd.Parameters.AddWithValue("@toDate", "2022-03-10"); if (conn.State != ConnectionState.Open) conn.Open(); int sumVal = Convert.ToInt32(cmd.ExecuteScalar()); } 0
Gajendra JangidRank #157 · 14.9K reputation4y agoPosted Mar 21, 2022, 10:35 PM EDT0Hi, Use below codes Sqlcreate proc usp_getTotal @date2 as date, @date2 as date as begin SELECT SUM(ISNULL(col, 0)) FROM tableName WHERE date BETWEEN @date1 AND @date2 end 0
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Mar 21, 2022, 10:18 PM EDT
Gajendra JangidRank #157 · 14.9K reputationPosted Mar 21, 2022, 10:35 PM EDT