472,971 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,971 software developers and data experts.

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 3511
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
Hi; I'm brand spanking new to sqlserver ( nice so far ). I need to make a simple data change across a list of tables. Basically replace an old date with a new date. However, the people I am...
2
by: Steve | last post by:
Hi; I have been writing a lot of short tsql scripts to fix a lot of tiny database issues. I was wondering if a could make an array of strings in tsql that I could process in a loop, something...
18
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between...
2
by: dynoweb | last post by:
I have several *.sql files with schema/data changes to be applied to our current database. Is there a way to create a TSQL script that could be run from the SQL Query Analyzer that would...
1
by: TOM GUGGER | last post by:
OMNI GROUP tgugger@aimexec.com T-SQL/ CONTRACT TO PERM/ ATLANTA
3
by: David Lozzi | last post by:
Howdy, ISSUE 1: See issue 2 below. I have a distance calculator on my site which works great. However, the users need to sort by distance, which make sense. I'm not sure how to do it other than...
16
by: David Lozzi | last post by:
Hello, I have some code that adds a new user. The new user has a checkboxlist of items which they can be associated with. I would like to send this list of items to TSQL along with the new user...
7
by: Filips Benoit | last post by:
Dear all, Tables: COMPANY: COM_ID, COM_NAME, ..... PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID, PRP_DEFAULT_VALUE ( nvarchar) COMPANY_PROPERTY: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE...
8
by: David Lozzi | last post by:
I'm fairly new to ASP.Net 2.0 SQLDatasource objects. It defaults using TSQL statments for the SELECT, INSERT, UPDATE, DELETE commands, which is great and it works. However, I've always been taught...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.