Solved

T-SQL query returns data from wrong year.

Posted on 2004-03-29
4
261 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

791 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