Discrepencies between .Net and TSQL

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i've noticed some strange behavior in .net when it comes to figuring out the previous month via code. this only happens when the month is january, and i know why it's happening, i just don't understand why this issue isn't addressed by .net. we have reports that get run on previous month's data, so that would be december given that it's january. if i do DateTime.Now.Month-1, the result is of course 0, so i get an exception. i have to write code like this to test if it's january

If DateTime.Now.Month = 1 The
Dim monthCounter As Integer = 13 - CInt(strMonthsBack) ' strMonthsBack is the number of months to search bac
lblAETitle.Text = MonthName(monthCounter
Els
lblAETitle.Text = MonthName(DateTime.Now.Month-1
End I

of course this works fine, but it looks messy. i can do this in TSQL like this

DECLARE @REPORT_MONTH DATETIM
SET @REPORT_MONTH=DATEADD(month, -1, GETDATE()

TSQL automatically realizes that it's january, so it rolls it back to december. i know i can accomplish the same thing in .Net using the DateTime.Subtract method, but that involves setting up other DateTime/TimeSpan variables and thus can get just as messy for something this easy. am i missing something? i would think that the .Net runtime would do a quick check on the integer value of Month before proceeding, but it doesn't. any and all comments are greatly appreciated, please email me directly as i won't be monitoring this thread actively

thank
jayson
 
Jayson said:
i've noticed some strange behavior in .net when it comes to figuring out the
previous month via code. this only happens when the month is january, and i know
why it's happening, i just don't understand why this issue isn't addressed by
..net. we have reports that get run on previous month's data, so that would be
december given that it's january. if i do DateTime.Now.Month-1, the result is of
course 0, so i get an exception. i have to write code like this to test if it's
january:
If DateTime.Now.Month = 1 Then
Dim monthCounter As Integer = 13 - CInt(strMonthsBack) '
strMonthsBack is the number of months to search back
lblAETitle.Text = MonthName(monthCounter)
Else
lblAETitle.Text = MonthName(DateTime.Now.Month-1)
End If

What is there to address??? DateTime.Now.Month gives you and integer, not a
reference. DateTime.Now.Month - 1 is the EXACT SAME thing as (1 -1) so the value
is (0).

Now, to do the same thing in VB.Net, try the following code :)

lblAETitle.Text = MonthName(Now.AddMonths(-1).Month)


Hope this helps...

Mythran
 
THis may help:

Private Sub SetDates()
Dim FirstDayOfMonth As Date
Dim LastDayOfMonth As Date
If Now.Month = 1 Then
FirstDayOfMonth = New DateTime(Now.Year - 1, 12, 1)
LastDayOfMonth = New DateTime(Now.Year - 1, 12,
DateTime.DaysInMonth(Now.Year, 12))
Else
FirstDayOfMonth = New DateTime(Now.Year, Now.Month - 1, 1)
LastDayOfMonth = New DateTime(Now.Year, Now.Month - 1,
DateTime.DaysInMonth(Now.Year, Now.Month - 1))
End If
dtpBegin.Value = FirstDayOfMonth
dtpEndDate.Value = LastDayOfMonth

End Sub
Jayson said:
i've noticed some strange behavior in .net when it comes to figuring out
the previous month via code. this only happens when the month is january,
and i know why it's happening, i just don't understand why this issue isn't
addressed by .net. we have reports that get run on previous month's data,
so that would be december given that it's january. if i do
DateTime.Now.Month-1, the result is of course 0, so i get an exception. i
have to write code like this to test if it's january:
If DateTime.Now.Month = 1 Then
Dim monthCounter As Integer = 13 - CInt(strMonthsBack)
' strMonthsBack is the number of months to search back
lblAETitle.Text = MonthName(monthCounter)
Else
lblAETitle.Text = MonthName(DateTime.Now.Month-1)
End If

of course this works fine, but it looks messy. i can do this in TSQL like this:

DECLARE @REPORT_MONTH DATETIME
SET @REPORT_MONTH=DATEADD(month, -1, GETDATE())

TSQL automatically realizes that it's january, so it rolls it back to
december. i know i can accomplish the same thing in .Net using the
DateTime.Subtract method, but that involves setting up other
DateTime/TimeSpan variables and thus can get just as messy for something
this easy. am i missing something? i would think that the .Net runtime
would do a quick check on the integer value of Month before proceeding, but
it doesn't. any and all comments are greatly appreciated, please email me
directly as i won't be monitoring this thread actively.
 
Dim ReportMonth As Date = DateAdd(DateInterval.Month, -1, Date.Now())

-Rob Teixeira [MVP]

Jayson said:
i've noticed some strange behavior in .net when it comes to figuring out
the previous month via code. this only happens when the month is january,
and i know why it's happening, i just don't understand why this issue isn't
addressed by .net. we have reports that get run on previous month's data,
so that would be december given that it's january. if i do
DateTime.Now.Month-1, the result is of course 0, so i get an exception. i
have to write code like this to test if it's january:
If DateTime.Now.Month = 1 Then
Dim monthCounter As Integer = 13 - CInt(strMonthsBack)
' strMonthsBack is the number of months to search back
lblAETitle.Text = MonthName(monthCounter)
Else
lblAETitle.Text = MonthName(DateTime.Now.Month-1)
End If

of course this works fine, but it looks messy. i can do this in TSQL like this:

DECLARE @REPORT_MONTH DATETIME
SET @REPORT_MONTH=DATEADD(month, -1, GETDATE())

TSQL automatically realizes that it's january, so it rolls it back to
december. i know i can accomplish the same thing in .Net using the
DateTime.Subtract method, but that involves setting up other
DateTime/TimeSpan variables and thus can get just as messy for something
this easy. am i missing something? i would think that the .Net runtime
would do a quick check on the integer value of Month before proceeding, but
it doesn't. any and all comments are greatly appreciated, please email me
directly as i won't be monitoring this thread actively.
 
certainly gets the job done, though a higher level of complexity than i
wanted. the question wasn't if it could be done, but why at the expense
of so much verboseness?

cheers

static void jayKnight();
 
when looking at such a conversation someone can ask himself why they add a
AddMonths method in the DateTime structure...

Dominique


Jayson said:
i've noticed some strange behavior in .net when it comes to figuring out
the previous month via code. this only happens when the month is january,
and i know why it's happening, i just don't understand why this issue isn't
addressed by .net. we have reports that get run on previous month's data,
so that would be december given that it's january. if i do
DateTime.Now.Month-1, the result is of course 0, so i get an exception. i
have to write code like this to test if it's january:
If DateTime.Now.Month = 1 Then
Dim monthCounter As Integer = 13 - CInt(strMonthsBack)
' strMonthsBack is the number of months to search back
lblAETitle.Text = MonthName(monthCounter)
Else
lblAETitle.Text = MonthName(DateTime.Now.Month-1)
End If

of course this works fine, but it looks messy. i can do this in TSQL like this:

DECLARE @REPORT_MONTH DATETIME
SET @REPORT_MONTH=DATEADD(month, -1, GETDATE())

TSQL automatically realizes that it's january, so it rolls it back to
december. i know i can accomplish the same thing in .Net using the
DateTime.Subtract method, but that involves setting up other
DateTime/TimeSpan variables and thus can get just as messy for something
this easy. am i missing something? i would think that the .Net runtime
would do a quick check on the integer value of Month before proceeding, but
it doesn't. any and all comments are greatly appreciated, please email me
directly as i won't be monitoring this thread actively.
 
Back
Top