473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to do a "year-to-date" SQL query where "year" commences in August?

ITM
Does anyone have an example of an SQL query which returns rows for the
year-to-date, but where the "year" commences on August 1st?

e.g. select * from mytable where datefield > last august 1st

TIA for any help
Isabel
Jul 20 '05 #1
2 23333
"ITM" <it*@manning.uk.com> wrote in message news:pb********************************@4ax.com...
Does anyone have an example of an SQL query which returns rows for the
year-to-date, but where the "year" commences on August 1st?

e.g. select * from mytable where datefield > last august 1st

TIA for any help
Isabel


CREATE TABLE T
(
d DATETIME NOT NULL PRIMARY KEY
)

-- Sample data
INSERT INTO T (d)
VALUES ('20030801')
INSERT INTO T (d)
VALUES ('20030901')
INSERT INTO T (d)
VALUES ('20030501')
INSERT INTO T (d)
VALUES ('20021201')

-- For current date
SELECT d
FROM T
WHERE (MONTH(CURRENT_TIMESTAMP) >= 8 OR
d >= CAST(YEAR(CURRENT_TIMESTAMP) - 1 AS CHAR(4)) + '0801')
AND
(MONTH(CURRENT_TIMESTAMP) < 8 OR
d >= CAST(YEAR(CURRENT_TIMESTAMP) AS CHAR(4)) + '0801')
ORDER BY d

d
2003-08-01 00:00:00.000
2003-09-01 00:00:00.000

-- For provided date
CREATE FUNCTION YearToDate (@d DATETIME)
RETURNS TABLE
AS
RETURN(
SELECT d
FROM T
WHERE (MONTH(@d) >= 8 OR
(d BETWEEN
CAST(YEAR(@d) - 1 AS CHAR(4)) + '0801' AND @d))
AND
(MONTH(@d) < 8 OR
(d BETWEEN CAST(YEAR(@d) AS CHAR(4)) + '0801' AND @d))
)

SELECT d
FROM YearToDate('20030701')
ORDER BY d

d
2002-12-01 00:00:00.000
2003-05-01 00:00:00.000

Regards,
jag
Jul 20 '05 #2
ITM
Many thanks for this!

I.

On Fri, 19 Dec 2003 13:10:53 GMT, "John Gilson" <ja*@acm.org> wrote:
"ITM" <it*@manning.uk.com> wrote in message news:pb********************************@4ax.com...
Does anyone have an example of an SQL query which returns rows for the
year-to-date, but where the "year" commences on August 1st?

e.g. select * from mytable where datefield > last august 1st

TIA for any help
Isabel


CREATE TABLE T
(
d DATETIME NOT NULL PRIMARY KEY
)

-- Sample data
INSERT INTO T (d)
VALUES ('20030801')
INSERT INTO T (d)
VALUES ('20030901')
INSERT INTO T (d)
VALUES ('20030501')
INSERT INTO T (d)
VALUES ('20021201')

-- For current date
SELECT d
FROM T
WHERE (MONTH(CURRENT_TIMESTAMP) >= 8 OR
d >= CAST(YEAR(CURRENT_TIMESTAMP) - 1 AS CHAR(4)) + '0801')
AND
(MONTH(CURRENT_TIMESTAMP) < 8 OR
d >= CAST(YEAR(CURRENT_TIMESTAMP) AS CHAR(4)) + '0801')
ORDER BY d

d
2003-08-01 00:00:00.000
2003-09-01 00:00:00.000

-- For provided date
CREATE FUNCTION YearToDate (@d DATETIME)
RETURNS TABLE
AS
RETURN(
SELECT d
FROM T
WHERE (MONTH(@d) >= 8 OR
(d BETWEEN
CAST(YEAR(@d) - 1 AS CHAR(4)) + '0801' AND @d))
AND
(MONTH(@d) < 8 OR
(d BETWEEN CAST(YEAR(@d) AS CHAR(4)) + '0801' AND @d))
)

SELECT d
FROM YearToDate('20030701')
ORDER BY d

d
2002-12-01 00:00:00.000
2003-05-01 00:00:00.000

Regards,
jag


Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: LRW | last post by:
I'm not exactly sure how to even ask the question, and I know my terminology is not good as I'm a SQL beginner, but, here goes. I need to find a way to make an if statement within an array...or,...
4
by: Phil Powell | last post by:
re: http://us4.php.net/manual/en/function.imagegif.php I have one class method that resizes images, including GIF images, and works just fine doing so, even up to this line: eval('image' ....
16
by: aurora | last post by:
Hello! Just gone though an article via Slashdot titled "The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software" http://www.gotw.ca/publications/concurrency-ddj.htm]. It argues...
8
by: Mario T. Lanza | last post by:
I'm not sure what I'm doing wrong. I have a form that has mnay input fields. Before each input field is a label enclosed in custom LABEL tags. Inside my CSS I have: LABEL { width: 120px; }
32
by: Will Hartung | last post by:
Can someone clarify that multiple classes in the "class" attribute are ok and "legal" and not some fluke? So, I can do: ..pink {color: pink} ..bold {font-weight: bold} ..medium {font-size:...
10
by: ryankbrown | last post by:
I was wondering if anybody has a document that states do's and don'ts when programming for a MS SQL DB. I was under the impression that Microsoft once produced such a document. Rather than a "Best...
48
by: mahurshi | last post by:
I am new to c++ classes. I defined this "cDie" class that would return a value between 1 and 6 (inclusive) It runs fine and gives no warnings during compilation. I was wondering if you guys...
5
by: Stewart | last post by:
Hi there, I would like to calculate a person's age in years (between 2 dates). I am using the following function for this calculation (source: http://www.mvps.org/access/datetime/date0001.htm)...
7
by: Eric | last post by:
Hi For this code, int getopt (int argc, char *const argv, const char *opts) what does the "char *const argv" mean? Does it equal to "char **const argv"? Or "char *const *argv"? Which is the...
5
by: maury | last post by:
Hello, I have a DB table with data filled from a weather sensor probe, I have one row every 10 minutes and the data fields is not in DateTime format but in string format: yyyyMMddHHmm So for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.