How to sum up **ALL** values on a given date in a SQL Server database?









up vote
0
down vote

favorite












I have a stopwatch project in C#. I will start recording time and store it in my database in a "Timespan" column. I also have a "Timestamp" column which records the date that the time was recorded.



Now I want to have a store procedure to give the program a date and I want my program to search the database and sum up ALL values in every corresponding "Timespan" columns on the given date. In other words If I give it a date I want my program to look up every "Timestamp" column cells and if there is one or more timestamp equal to the given date I want the program to go ahead and check all of their related "Timespan" columns and sum them up.



MY CODE (WHICH ONLY WORKS IF THERE IS ONLY 1 TIMESTAMP VALUE EQUAL TO THE GIVEN DATE - IT YELLS AT ME IF THERE IS TWO OR MORE AND THIS IS MY PROBLEM):



CREATE PROCEDURE SumOnDateReport
@QueryDate DATETIME
AS
DECLARE @total_sec INT
DECLARE @HourTime INT
DECLARE @MinTime INT
DECLARE @SecTime INT

SET @HourTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 1, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)
SET @MinTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 3, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)
SET @SecTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 5, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)

SET @total_sec = @HourTime * 3600 + @MinTime * 60 + @SecTime

INSERT INTO InfoTable(TotalSecColumn) VALUES (null)

UPDATE InfoTable
SET TotalSecColumn = @total_sec
WHERE TimeStampColumn = @QueryDate

SELECT TotalSecColumn
FROM InfoTable
WHERE TimeStampColumn = @QueryDate


all of my table columns are NVARCHAR but Timestampcolumn is datetime.










share|improve this question



















  • 1




    What datatype is this TimeStampColumn in your table??
    – marc_s
    Nov 11 at 8:49










  • @marc_s its datetime
    – absolute455
    Nov 11 at 9:56














up vote
0
down vote

favorite












I have a stopwatch project in C#. I will start recording time and store it in my database in a "Timespan" column. I also have a "Timestamp" column which records the date that the time was recorded.



Now I want to have a store procedure to give the program a date and I want my program to search the database and sum up ALL values in every corresponding "Timespan" columns on the given date. In other words If I give it a date I want my program to look up every "Timestamp" column cells and if there is one or more timestamp equal to the given date I want the program to go ahead and check all of their related "Timespan" columns and sum them up.



MY CODE (WHICH ONLY WORKS IF THERE IS ONLY 1 TIMESTAMP VALUE EQUAL TO THE GIVEN DATE - IT YELLS AT ME IF THERE IS TWO OR MORE AND THIS IS MY PROBLEM):



CREATE PROCEDURE SumOnDateReport
@QueryDate DATETIME
AS
DECLARE @total_sec INT
DECLARE @HourTime INT
DECLARE @MinTime INT
DECLARE @SecTime INT

SET @HourTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 1, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)
SET @MinTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 3, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)
SET @SecTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 5, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)

SET @total_sec = @HourTime * 3600 + @MinTime * 60 + @SecTime

INSERT INTO InfoTable(TotalSecColumn) VALUES (null)

UPDATE InfoTable
SET TotalSecColumn = @total_sec
WHERE TimeStampColumn = @QueryDate

SELECT TotalSecColumn
FROM InfoTable
WHERE TimeStampColumn = @QueryDate


all of my table columns are NVARCHAR but Timestampcolumn is datetime.










share|improve this question



















  • 1




    What datatype is this TimeStampColumn in your table??
    – marc_s
    Nov 11 at 8:49










  • @marc_s its datetime
    – absolute455
    Nov 11 at 9:56












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a stopwatch project in C#. I will start recording time and store it in my database in a "Timespan" column. I also have a "Timestamp" column which records the date that the time was recorded.



Now I want to have a store procedure to give the program a date and I want my program to search the database and sum up ALL values in every corresponding "Timespan" columns on the given date. In other words If I give it a date I want my program to look up every "Timestamp" column cells and if there is one or more timestamp equal to the given date I want the program to go ahead and check all of their related "Timespan" columns and sum them up.



MY CODE (WHICH ONLY WORKS IF THERE IS ONLY 1 TIMESTAMP VALUE EQUAL TO THE GIVEN DATE - IT YELLS AT ME IF THERE IS TWO OR MORE AND THIS IS MY PROBLEM):



CREATE PROCEDURE SumOnDateReport
@QueryDate DATETIME
AS
DECLARE @total_sec INT
DECLARE @HourTime INT
DECLARE @MinTime INT
DECLARE @SecTime INT

SET @HourTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 1, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)
SET @MinTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 3, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)
SET @SecTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 5, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)

SET @total_sec = @HourTime * 3600 + @MinTime * 60 + @SecTime

INSERT INTO InfoTable(TotalSecColumn) VALUES (null)

UPDATE InfoTable
SET TotalSecColumn = @total_sec
WHERE TimeStampColumn = @QueryDate

SELECT TotalSecColumn
FROM InfoTable
WHERE TimeStampColumn = @QueryDate


all of my table columns are NVARCHAR but Timestampcolumn is datetime.










share|improve this question















I have a stopwatch project in C#. I will start recording time and store it in my database in a "Timespan" column. I also have a "Timestamp" column which records the date that the time was recorded.



Now I want to have a store procedure to give the program a date and I want my program to search the database and sum up ALL values in every corresponding "Timespan" columns on the given date. In other words If I give it a date I want my program to look up every "Timestamp" column cells and if there is one or more timestamp equal to the given date I want the program to go ahead and check all of their related "Timespan" columns and sum them up.



MY CODE (WHICH ONLY WORKS IF THERE IS ONLY 1 TIMESTAMP VALUE EQUAL TO THE GIVEN DATE - IT YELLS AT ME IF THERE IS TWO OR MORE AND THIS IS MY PROBLEM):



CREATE PROCEDURE SumOnDateReport
@QueryDate DATETIME
AS
DECLARE @total_sec INT
DECLARE @HourTime INT
DECLARE @MinTime INT
DECLARE @SecTime INT

SET @HourTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 1, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)
SET @MinTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 3, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)
SET @SecTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 5, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)

SET @total_sec = @HourTime * 3600 + @MinTime * 60 + @SecTime

INSERT INTO InfoTable(TotalSecColumn) VALUES (null)

UPDATE InfoTable
SET TotalSecColumn = @total_sec
WHERE TimeStampColumn = @QueryDate

SELECT TotalSecColumn
FROM InfoTable
WHERE TimeStampColumn = @QueryDate


all of my table columns are NVARCHAR but Timestampcolumn is datetime.







sql-server date time






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 10:02

























asked Nov 11 at 8:13









absolute455

658




658







  • 1




    What datatype is this TimeStampColumn in your table??
    – marc_s
    Nov 11 at 8:49










  • @marc_s its datetime
    – absolute455
    Nov 11 at 9:56












  • 1




    What datatype is this TimeStampColumn in your table??
    – marc_s
    Nov 11 at 8:49










  • @marc_s its datetime
    – absolute455
    Nov 11 at 9:56







1




1




What datatype is this TimeStampColumn in your table??
– marc_s
Nov 11 at 8:49




What datatype is this TimeStampColumn in your table??
– marc_s
Nov 11 at 8:49












@marc_s its datetime
– absolute455
Nov 11 at 9:56




@marc_s its datetime
– absolute455
Nov 11 at 9:56












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Since it's a DATETIME column - you should refrain from trying to guess the string format (there's no default, system-inherent string representation - so your code would always depend on the language/regional settings) - use the proper DATETIME function to deal with this!!



Change your code (those three SET statement with the CONVERT and SUBSTRING) into something like this:



SELECT 
@HourTime = SUM(DATEPART(HOUR, TimeStringColumn)),
@MinTime = SUM(DATEPART(MINUTE, TimeStringColumn)),
@SecTime = SUM(DATEPART(SECOND, TimeStringColumn))
FROM
InfoTable
WHERE
TimeStampColumn = @QueryDate


This should work just fine with multiple rows for the same @QueryDate, too!






share|improve this answer




















  • Im getting TimeSpan columns values from label.text in C#. so I have to use NVARCHAR in my table and substring to do the rest things... isnt there any way to solve my problem with my time span column being a nvarchar?
    – absolute455
    Nov 11 at 10:09











  • @absolute455: I don't understand - your code clearly shows you're loading this stuff from the database - not from a text lable ......
    – marc_s
    Nov 11 at 10:10










  • my code in sql is a storeprocedure. it gets the "QueryDate" from my c# program. my c# program contains labels. it gets QueryDate from those labels. which are texts. @marc_s
    – absolute455
    Nov 12 at 6:31











  • Although I tried to convert the labels.texts into dates to be able to use your code but i was not successfull because when they are converted they are all turned to 12:00 AM. I dont know how to fix it
    – absolute455
    Nov 12 at 6:33










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246936%2fhow-to-sum-up-all-values-on-a-given-date-in-a-sql-server-database%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Since it's a DATETIME column - you should refrain from trying to guess the string format (there's no default, system-inherent string representation - so your code would always depend on the language/regional settings) - use the proper DATETIME function to deal with this!!



Change your code (those three SET statement with the CONVERT and SUBSTRING) into something like this:



SELECT 
@HourTime = SUM(DATEPART(HOUR, TimeStringColumn)),
@MinTime = SUM(DATEPART(MINUTE, TimeStringColumn)),
@SecTime = SUM(DATEPART(SECOND, TimeStringColumn))
FROM
InfoTable
WHERE
TimeStampColumn = @QueryDate


This should work just fine with multiple rows for the same @QueryDate, too!






share|improve this answer




















  • Im getting TimeSpan columns values from label.text in C#. so I have to use NVARCHAR in my table and substring to do the rest things... isnt there any way to solve my problem with my time span column being a nvarchar?
    – absolute455
    Nov 11 at 10:09











  • @absolute455: I don't understand - your code clearly shows you're loading this stuff from the database - not from a text lable ......
    – marc_s
    Nov 11 at 10:10










  • my code in sql is a storeprocedure. it gets the "QueryDate" from my c# program. my c# program contains labels. it gets QueryDate from those labels. which are texts. @marc_s
    – absolute455
    Nov 12 at 6:31











  • Although I tried to convert the labels.texts into dates to be able to use your code but i was not successfull because when they are converted they are all turned to 12:00 AM. I dont know how to fix it
    – absolute455
    Nov 12 at 6:33














up vote
0
down vote













Since it's a DATETIME column - you should refrain from trying to guess the string format (there's no default, system-inherent string representation - so your code would always depend on the language/regional settings) - use the proper DATETIME function to deal with this!!



Change your code (those three SET statement with the CONVERT and SUBSTRING) into something like this:



SELECT 
@HourTime = SUM(DATEPART(HOUR, TimeStringColumn)),
@MinTime = SUM(DATEPART(MINUTE, TimeStringColumn)),
@SecTime = SUM(DATEPART(SECOND, TimeStringColumn))
FROM
InfoTable
WHERE
TimeStampColumn = @QueryDate


This should work just fine with multiple rows for the same @QueryDate, too!






share|improve this answer




















  • Im getting TimeSpan columns values from label.text in C#. so I have to use NVARCHAR in my table and substring to do the rest things... isnt there any way to solve my problem with my time span column being a nvarchar?
    – absolute455
    Nov 11 at 10:09











  • @absolute455: I don't understand - your code clearly shows you're loading this stuff from the database - not from a text lable ......
    – marc_s
    Nov 11 at 10:10










  • my code in sql is a storeprocedure. it gets the "QueryDate" from my c# program. my c# program contains labels. it gets QueryDate from those labels. which are texts. @marc_s
    – absolute455
    Nov 12 at 6:31











  • Although I tried to convert the labels.texts into dates to be able to use your code but i was not successfull because when they are converted they are all turned to 12:00 AM. I dont know how to fix it
    – absolute455
    Nov 12 at 6:33












up vote
0
down vote










up vote
0
down vote









Since it's a DATETIME column - you should refrain from trying to guess the string format (there's no default, system-inherent string representation - so your code would always depend on the language/regional settings) - use the proper DATETIME function to deal with this!!



Change your code (those three SET statement with the CONVERT and SUBSTRING) into something like this:



SELECT 
@HourTime = SUM(DATEPART(HOUR, TimeStringColumn)),
@MinTime = SUM(DATEPART(MINUTE, TimeStringColumn)),
@SecTime = SUM(DATEPART(SECOND, TimeStringColumn))
FROM
InfoTable
WHERE
TimeStampColumn = @QueryDate


This should work just fine with multiple rows for the same @QueryDate, too!






share|improve this answer












Since it's a DATETIME column - you should refrain from trying to guess the string format (there's no default, system-inherent string representation - so your code would always depend on the language/regional settings) - use the proper DATETIME function to deal with this!!



Change your code (those three SET statement with the CONVERT and SUBSTRING) into something like this:



SELECT 
@HourTime = SUM(DATEPART(HOUR, TimeStringColumn)),
@MinTime = SUM(DATEPART(MINUTE, TimeStringColumn)),
@SecTime = SUM(DATEPART(SECOND, TimeStringColumn))
FROM
InfoTable
WHERE
TimeStampColumn = @QueryDate


This should work just fine with multiple rows for the same @QueryDate, too!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 10:01









marc_s

567k12810961246




567k12810961246











  • Im getting TimeSpan columns values from label.text in C#. so I have to use NVARCHAR in my table and substring to do the rest things... isnt there any way to solve my problem with my time span column being a nvarchar?
    – absolute455
    Nov 11 at 10:09











  • @absolute455: I don't understand - your code clearly shows you're loading this stuff from the database - not from a text lable ......
    – marc_s
    Nov 11 at 10:10










  • my code in sql is a storeprocedure. it gets the "QueryDate" from my c# program. my c# program contains labels. it gets QueryDate from those labels. which are texts. @marc_s
    – absolute455
    Nov 12 at 6:31











  • Although I tried to convert the labels.texts into dates to be able to use your code but i was not successfull because when they are converted they are all turned to 12:00 AM. I dont know how to fix it
    – absolute455
    Nov 12 at 6:33
















  • Im getting TimeSpan columns values from label.text in C#. so I have to use NVARCHAR in my table and substring to do the rest things... isnt there any way to solve my problem with my time span column being a nvarchar?
    – absolute455
    Nov 11 at 10:09











  • @absolute455: I don't understand - your code clearly shows you're loading this stuff from the database - not from a text lable ......
    – marc_s
    Nov 11 at 10:10










  • my code in sql is a storeprocedure. it gets the "QueryDate" from my c# program. my c# program contains labels. it gets QueryDate from those labels. which are texts. @marc_s
    – absolute455
    Nov 12 at 6:31











  • Although I tried to convert the labels.texts into dates to be able to use your code but i was not successfull because when they are converted they are all turned to 12:00 AM. I dont know how to fix it
    – absolute455
    Nov 12 at 6:33















Im getting TimeSpan columns values from label.text in C#. so I have to use NVARCHAR in my table and substring to do the rest things... isnt there any way to solve my problem with my time span column being a nvarchar?
– absolute455
Nov 11 at 10:09





Im getting TimeSpan columns values from label.text in C#. so I have to use NVARCHAR in my table and substring to do the rest things... isnt there any way to solve my problem with my time span column being a nvarchar?
– absolute455
Nov 11 at 10:09













@absolute455: I don't understand - your code clearly shows you're loading this stuff from the database - not from a text lable ......
– marc_s
Nov 11 at 10:10




@absolute455: I don't understand - your code clearly shows you're loading this stuff from the database - not from a text lable ......
– marc_s
Nov 11 at 10:10












my code in sql is a storeprocedure. it gets the "QueryDate" from my c# program. my c# program contains labels. it gets QueryDate from those labels. which are texts. @marc_s
– absolute455
Nov 12 at 6:31





my code in sql is a storeprocedure. it gets the "QueryDate" from my c# program. my c# program contains labels. it gets QueryDate from those labels. which are texts. @marc_s
– absolute455
Nov 12 at 6:31













Although I tried to convert the labels.texts into dates to be able to use your code but i was not successfull because when they are converted they are all turned to 12:00 AM. I dont know how to fix it
– absolute455
Nov 12 at 6:33




Although I tried to convert the labels.texts into dates to be able to use your code but i was not successfull because when they are converted they are all turned to 12:00 AM. I dont know how to fix it
– absolute455
Nov 12 at 6:33

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246936%2fhow-to-sum-up-all-values-on-a-given-date-in-a-sql-server-database%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線