August 23, 2013 at 10:09 am
I am trying to view data for past one year ...Don't know whats wrong with the following:
select ProdMonth ,ProdYear from Production
where DATEADD(yyyy,-1,getdate())
Kindly advise
August 23, 2013 at 10:23 am
sharonsql2013 (8/23/2013)
I am trying to view data for past one year ...Don't know whats wrong with the following:select ProdMonth ,ProdYear from Production
where DATEADD(yyyy,-1,getdate())
Kindly advise
You will need to compare some data column to the calculated date one year ago.
....where DATECOLUMN >= DATEADD(yyyy,-1,getdate())
August 23, 2013 at 11:17 am
select ProdMonth ,ProdYear from Production
where ProdYear >= DATEADD(yyyy,-1,getdate())
returns no data... However , there is data present. DOn't know whats wrong...
August 23, 2013 at 11:21 am
What's the data type of prodyear? If you are comparing an entire date against a column that only contains the year as an int or a 4 position varchar, you won't get what you are looking for.
August 23, 2013 at 11:26 am
batgirl (8/23/2013)
sharonsql2013 (8/23/2013)
I am trying to view data for past one year ...Don't know whats wrong with the following:select ProdMonth ,ProdYear from Production
where DATEADD(yyyy,-1,getdate())
Kindly advise
You will need to compare some data column to the calculated date one year ago.
....where DATECOLUMN >= DATEADD(yyyy,-1,getdate())
This is one of the biggest reasons why I try to discourage people from saving parts of dates in separate columns.
What is the datatype of the ProdYear and ProdMonth columns?
--Jeff Moden
Change is inevitable... Change for the better is not.
August 23, 2013 at 11:29 am
Yes , Prod year is int.
August 23, 2013 at 12:19 pm
So it would have to be something like:
select ProdMonth ,ProdYear from Production
where ProdYear >= cast(datepart(yyyy,DATEADD(yyyy,-1,getdate())) as int)
I second Jeff's suggestion to store dates as dates.
August 23, 2013 at 12:26 pm
That sure helps.
August 23, 2013 at 3:49 pm
sharonsql2013 (8/23/2013)
Yes , Prod year is int.
What about ProdMonth? We'll need that info to identify rows that are a year old. And, because you only have Year and Month data, it's going to be impossible to figure out what rows are available in terms of 365 days.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 28, 2013 at 11:37 am
David Webb-CDS (8/23/2013)
I second Jeff's suggestion to store dates as dates.
I have to third Jeff's "store dates as dates" recommendation. I seem to spend a lot of time trying to convince people of this. Some people like to store some date parts differently or make up their own approach. Once you get bitten by doing this, you'll convert and stay converted. Your data will then follow. 🙂
One word of caution, however, is that the datetime data type is a point in time, not a duration of time. I've seen where people get in trouble trying to store a duration as a datetime data type.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply