SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date range selection error for a table view


Date range selection error for a table view

Author
Message
DerbyNeal
DerbyNeal
Old Hand
Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)

Group: General Forum Members
Points: 304 Visits: 909
Hi,

I have created a view using the query below which is based on dates within my RMMarketingPrice table. The view is used to load a screen list selection with values from the current month from the 1st until the previous day which is fine unless the current date is the 1st, in which case it produces an ‘out-of-range’ error message due to the range selected.

Create/Load table code

USE [Test]
GO
/****** Object: Table [dbo].[Table_1] Script Date: 07/25/2014 13:38:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[RMMarketPrice](
[Date] [smalldatetime] NOT NULL
) ON [PRIMARY]
;


INSERT dbo.RMMarketPrice
(
Date
)

VALUES
('2014-06-25 00:00:00')
;

INSERT dbo.RMMarketPrice
(
Date
)

VALUES
('2014-07-01 00:00:00')
;

INSERT dbo.RMMarketPrice
(
Date
)
VALUES
('2014-07-12 00:00:00')
;

INSERT dbo.RMMarketPrice
(
Date
)
VALUES
('2014-07-23 00:00:00')
;



Table View code

SELECT DISTINCT TOP (100) PERCENT Date
FROM dbo.RMMarketPrice
WHERE (Date BETWEEN CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '-' + RIGHT('0' + CAST(MONTH(GETDATE()) AS VARCHAR(2)), 2) + '-' + '01 00:00:00' AND
CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '-' + RIGHT('0' + CAST(MONTH(GETDATE()) AS VARCHAR(2)), 2) + '-' + RIGHT('0' + CAST(DAY(GETDATE() - 1)
AS VARCHAR(2)), 2) + ' 00:00:00')
ORDER BY Date
;



Any ideas how I can prevent this please.

Thanks in advance,
anthony.green
anthony.green
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10186 Visits: 6378
What is the expected outcome when it is the 1st of the month?



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


DerbyNeal
DerbyNeal
Old Hand
Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)

Group: General Forum Members
Points: 304 Visits: 909
Aah yes... A 'null' would be fine. Thanks :-)
anthony.green
anthony.green
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10186 Visits: 6378
Just return null? No other data needs to be returned?



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


anthony.green
anthony.green
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10186 Visits: 6378
Does this do what you need?


SELECT DISTINCT TOP (100) PERCENT Date
FROM dbo.RMMarketPrice
WHERE Date BETWEEN dateadd(mm, datediff(mm, 0, @date), 0) AND
dateadd(dd, datediff(dd, 0, @date) - 1, 0)
ORDER BY Date





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


DerbyNeal
DerbyNeal
Old Hand
Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)

Group: General Forum Members
Points: 304 Visits: 909
I only want data from the current month. The selection criteria is from the 1st until the previous day (i.e. as today is the 25th, then from 01/07/2014 until 24/07/2014). But in the example where today would be the 1st of the month, I don't want any current months data returning.

Sorry, I didn't see your previous post whilst I was typing. I'll try it now.
DerbyNeal
DerbyNeal
Old Hand
Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)

Group: General Forum Members
Points: 304 Visits: 909
Many thanks Anthony
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search