468,503 Members | 2,219 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,503 developers. It's quick & easy.

Discrepencies between .Net and TSQL

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
Nov 20 '05 #1
6 3428
"Jayson" <ja********@carolina.rr.com> wrote in message
news:DA**********************************@microsof t.com...
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

Nov 20 '05 #2
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" <ja********@carolina.rr.com> wrote in message
news:DA**********************************@microsof t.com...
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.
thanks
jayson

Nov 20 '05 #3
Dim ReportMonth As Date = DateAdd(DateInterval.Month, -1, Date.Now())

-Rob Teixeira [MVP]

"Jayson" <ja********@carolina.rr.com> wrote in message
news:DA**********************************@microsof t.com...
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.
thanks
jayson

Nov 20 '05 #4
that's exactly what i was looking for, thanks.
static void jayKnight();
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #5
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();
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #6
when looking at such a conversation someone can ask himself why they add a
AddMonths method in the DateTime structure...

Dominique
"Jayson" <ja********@carolina.rr.com> wrote in message
news:DA**********************************@microsof t.com...
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.
thanks
jayson

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Steve | last post: by
2 posts views Thread by Steve | last post: by
2 posts views Thread by dynoweb | last post: by
1 post views Thread by TOM GUGGER | last post: by
16 posts views Thread by David Lozzi | last post: by
7 posts views Thread by Filips Benoit | last post: by
8 posts views Thread by David Lozzi | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.