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 Timestamp
column is datetime.
sql-server date time
add a comment |
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 Timestamp
column is datetime.
sql-server date time
1
What datatype is thisTimeStampColumn
in your table??
– marc_s
Nov 11 at 8:49
@marc_s its datetime
– absolute455
Nov 11 at 9:56
add a comment |
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 Timestamp
column is datetime.
sql-server date time
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 Timestamp
column is datetime.
sql-server date time
sql-server date time
edited Nov 11 at 10:02
asked Nov 11 at 8:13
absolute455
658
658
1
What datatype is thisTimeStampColumn
in your table??
– marc_s
Nov 11 at 8:49
@marc_s its datetime
– absolute455
Nov 11 at 9:56
add a comment |
1
What datatype is thisTimeStampColumn
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
add a comment |
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!
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
add a comment |
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!
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
add a comment |
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!
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
add a comment |
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!
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!
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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