flukester
asked on
Return year only from datetime field
I'm hoping there is an easy way I can return year only or trim a datetime field to year only. I am selecting all from a table, creating a datatable and looping through the rows to find all annual reports in the table.
Dim dr As DataRow
For Each dr In dscomm.Rows
reportYear += dr("ReportYear")
Next
This is an example of the data that is returned:
8/30/2002 7:24:00 AM10/1/2003 4:28:03 PM9/24/2004 4:30:51 PM
What I need to do is get the years only and then link those years to another page which I can handle, but I can't figure out a method to just return year?
Dim dr As DataRow
For Each dr In dscomm.Rows
reportYear += dr("ReportYear")
Next
This is an example of the data that is returned:
8/30/2002 7:24:00 AM10/1/2003 4:28:03 PM9/24/2004 4:30:51 PM
What I need to do is get the years only and then link those years to another page which I can handle, but I can't figure out a method to just return year?
use similar code :
DateTime.parse (dr("ReportYear").ToString ()).Year;
DateTime.parse (dr("ReportYear").ToString
You just can get it, if you use the YEAR scalar function of SQL;
SQL statement...
"Select year(date_field), date_field, another_field from table"
FerG
Saludos.
--
Ing. Fernando D. Giletta
San Fco. Cba. Argentina.
SQL statement...
"Select year(date_field), date_field, another_field from table"
FerG
Saludos.
--
Ing. Fernando D. Giletta
San Fco. Cba. Argentina.
Use SQL YEAR function. Change your sql select query to something like this:
Select YEAR(theDateTimeFieldName) As ReportYear From YourTable
Select YEAR(theDateTimeFieldName) As ReportYear From YourTable
ASKER
Both codes work, but it seems they are adding the years together instead of giving me each year. The outcome of both scenarios is 6009
Years are 2002,2003, 2004?
Years are 2002,2003, 2004?
Arghhhhhh, fingers are not working fast enough.
It's because this line:
reportYear += reportData.Year
The "+" sign means that you are adding to.
reportYear += reportData.Year
The "+" sign means that you are adding to.
ASKER
That's what I thought but
For Each dr In dscomm.Rows
Dim reportData As DateTime = CType(dr("ReportYear"), DateTime)
reportYear = reportData.Year
Next
only returns the last record.
For Each dr In dscomm.Rows
Dim reportData As DateTime = CType(dr("ReportYear"), DateTime)
reportYear = reportData.Year
Next
only returns the last record.
Yes, because it overwrites reportYear every time it enters the loop, giving you the last record value as a result.
Do you need all the year in separate strings?
Do you need all the year in separate strings?
ASKER
Juan, yes I need to show all of them on the page.
then you could use something like this, it will give you a list containing all the years:
Dim reportYear As List(Of String)
For Each dr In dscomm.Rows
Dim reportData As DateTime = CType(dr("ReportYear"), DateTime)
reportYear.Add(reportData.Year)
Next
ASKER
Juan, I apologize, I'm still not getting it. I added the import statement for System.Collections.Generic and still get an error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error:
Line 85: For Each dr In dscomm.Rows
Line 86: Dim reportData As DateTime = CType(dr("ReportYear"), DateTime)
Line 87: reportYear.Add(reportData. Year) 'highlighted as error
Line 88: Next
the table doesn't have any null values. What am I missing?
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept
Source Error:
Line 85: For Each dr In dscomm.Rows
Line 86: Dim reportData As DateTime = CType(dr("ReportYear"), DateTime)
Line 87: reportYear.Add(reportData.
Line 88: Next
the table doesn't have any null values. What am I missing?
Oops, I'm sorry, it should be like this (missed the New keyword):
Dim reportYear As New List(Of String)
For Each dr In dscomm.Rows
Dim reportData As DateTime = CType(dr("ReportYear"), DateTime)
reportYear.Add(reportData.Year)
Next
ASKER
I tried that as well and the output on the page is:
System.Collections.Generic .List`1[Sy stem.Strin g]
Public variable up top Public reportYear As New List(Of String)
Then below
Dim dr As DataRow
For Each dr In dscomm.Rows
Dim reportData As DateTime = CType(dr("ReportYear"), DateTime)
reportYear.Add(reportData. Year)
Next
System.Collections.Generic
Public variable up top Public reportYear As New List(Of String)
Then below
Dim dr As DataRow
For Each dr In dscomm.Rows
Dim reportData As DateTime = CType(dr("ReportYear"), DateTime)
reportYear.Add(reportData.
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Okay, tested the value of outputToDisplay and all the years show up separated by a comma. I'll tweak it for my page display now. Thank you very much for walking me through this and your quick responses!
No worries, I'm glad it works!
Open in new window