Solved

T-SQL query returns data from wrong year.

Posted on 2004-03-29
4
252 Views
Last Modified: 2012-08-14
I have a query that prompts for a begin and end date and then pulls data from between those two dates.  My problem is that if I type 03/25/2004 or 3/25/04 for a begin and end date I end up with transaction that happened on 3/25/03.  
CODE:
ALTER PROCEDURE dbo.[Deposit Transactions by form of pmt]
(@BEGIN_DATE DATETIME,
@END_DATE DATETIME)
AS SELECT     dbo.[Payment Info].[Form of Pmt], dbo.[Payment Info].[Pmt Date], dbo.[Payment Info].Amount, dbo.[Payment Info].[Account Number],
                      dbo.[Hockey Season Tickets].[First Name] + ' ' + dbo.[Hockey Season Tickets].[Last Name] AS Name, dbo.[Hockey Season Tickets].[Company Name],
                      dbo.[Payment Info].[Check/Credit #], dbo.[Payment Info].[Pmt for]
FROM         dbo.[Hockey Season Tickets] INNER JOIN
                      dbo.[Payment Info] ON dbo.[Hockey Season Tickets].[Account Number] = dbo.[Payment Info].[Account Number]
WHERE     (CONVERT(nvarchar(8), dbo.[Payment Info].[Pmt Date], 101) >= CONVERT(nvarchar(8), @BEGIN_DATE, 101)) AND (CONVERT(nvarchar(8),
                      dbo.[Payment Info].[Pmt Date], 101) <= CONVERT(nvarchar(8), @END_DATE, 101)) AND (dbo.[Payment Info].[Pmt for] = N'deposit')
ORDER BY dbo.[Payment Info].[Pmt Date], dbo.[Hockey Season Tickets].[Company Name]

I know I missing something simple, but I'm going crazy looking for it.
0
Comment
Question by:BCooper
  • 3
4 Comments
 
LVL 3

Accepted Solution

by:
debi_mela earned 125 total points
ID: 10705967
make nvarchar(10) instead of 8..


ALTER PROCEDURE dbo.[Deposit Transactions by form of pmt]
(@BEGIN_DATE DATETIME,
@END_DATE DATETIME)
AS SELECT     dbo.[Payment Info].[Form of Pmt], dbo.[Payment Info].[Pmt Date], dbo.[Payment Info].Amount, dbo.[Payment Info].[Account Number],
                      dbo.[Hockey Season Tickets].[First Name] + ' ' + dbo.[Hockey Season Tickets].[Last Name] AS Name, dbo.[Hockey Season Tickets].[Company Name],
                      dbo.[Payment Info].[Check/Credit #], dbo.[Payment Info].[Pmt for]
FROM         dbo.[Hockey Season Tickets] INNER JOIN
                      dbo.[Payment Info] ON dbo.[Hockey Season Tickets].[Account Number] = dbo.[Payment Info].[Account Number]
WHERE     (CONVERT(nvarchar(10), dbo.[Payment Info].[Pmt Date], 101) >= CONVERT(nvarchar(10), @BEGIN_DATE, 101)) AND (CONVERT(nvarchar(10),
                      dbo.[Payment Info].[Pmt Date], 101) <= CONVERT(nvarchar(10), @END_DATE, 101)) AND (dbo.[Payment Info].[Pmt for] = N'deposit')
ORDER BY dbo.[Payment Info].[Pmt Date], dbo.[Hockey Season Tickets].[Company Name]

0
 
LVL 3

Expert Comment

by:debi_mela
ID: 10705994
Also if both the datatypes are datetime, compare them without converting to char...

ALTER PROCEDURE dbo.[Deposit Transactions by form of pmt]
(@BEGIN_DATE DATETIME,
@END_DATE DATETIME)
AS SELECT     dbo.[Payment Info].[Form of Pmt], dbo.[Payment Info].[Pmt Date], dbo.[Payment Info].Amount, dbo.[Payment Info].[Account Number],
                      dbo.[Hockey Season Tickets].[First Name] + ' ' + dbo.[Hockey Season Tickets].[Last Name] AS Name, dbo.[Hockey Season Tickets].[Company Name],
                      dbo.[Payment Info].[Check/Credit #], dbo.[Payment Info].[Pmt for]
FROM         dbo.[Hockey Season Tickets] INNER JOIN
                      dbo.[Payment Info] ON dbo.[Hockey Season Tickets].[Account Number] = dbo.[Payment Info].[Account Number]
WHERE   dbo.[Payment Info].[Pmt Date] >= @BEGIN_DATE AND
             dbo.[Payment Info].[Pmt Date] <=  @END_DATE, 101
 AND (dbo.[Payment Info].[Pmt for] = N'deposit')
ORDER BY dbo.[Payment Info].[Pmt Date], dbo.[Hockey Season Tickets].[Company Name]

0
 
LVL 3

Expert Comment

by:debi_mela
ID: 10706005
sorry here is the correct version...


ALTER PROCEDURE dbo.[Deposit Transactions by form of pmt]
(@BEGIN_DATE DATETIME,
@END_DATE DATETIME)
AS SELECT     dbo.[Payment Info].[Form of Pmt], dbo.[Payment Info].[Pmt Date], dbo.[Payment Info].Amount, dbo.[Payment Info].[Account Number],
                      dbo.[Hockey Season Tickets].[First Name] + ' ' + dbo.[Hockey Season Tickets].[Last Name] AS Name, dbo.[Hockey Season Tickets].[Company Name],
                      dbo.[Payment Info].[Check/Credit #], dbo.[Payment Info].[Pmt for]
FROM         dbo.[Hockey Season Tickets] INNER JOIN
                      dbo.[Payment Info] ON dbo.[Hockey Season Tickets].[Account Number] = dbo.[Payment Info].[Account Number]
WHERE   dbo.[Payment Info].[Pmt Date] >= @BEGIN_DATE AND
             dbo.[Payment Info].[Pmt Date] <=  @END_DATE
 AND (dbo.[Payment Info].[Pmt for] = N'deposit')
ORDER BY dbo.[Payment Info].[Pmt Date], dbo.[Hockey Season Tickets].[Company Name]

0
 

Expert Comment

by:DSpeckleback
ID: 10706083
Consider using:
WHERE   dbo.[Payment Info].[Pmt Date] BETWEEN @BEGIN_DATE AND @END_DATE.

Also keep in mind that if the payment date includes the time and you are looking for all payments on 03/25/2004, the @END_DATE should include 23:59:59 (or be < 03//26/2004).

Just some thoughts...
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now