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


5 years or older


5 years or older

Author
Message
craig.jenkins
craig.jenkins
Right there with Babe
Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)

Group: General Forum Members
Points: 730 Visits: 206
Guys, if i have a list of dates in a table how do i query how many of the dates are 5 years or older?
Subramaniam Chandrasekar
Subramaniam Chandrasekar
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3106 Visits: 531
craig.jenkins - Tuesday, February 20, 2018 4:38 AM
Guys, if i have a list of dates in a table how do i query how many of the dates are 5 years or older?

DATE functions could be used for your filtration.

HanShi
HanShi
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27692 Visits: 4129
Create your WHERE something like:

WHERE  [your_date_column] > DATEADD (year, -5 , GETDATE() ) 


Try to not wrap the function around your column so the query can still use any indexes on that column.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)

Group: General Forum Members
Points: 194915 Visits: 23765
craig.jenkins - Tuesday, February 20, 2018 4:38 AM
Guys, if i have a list of dates in a table how do i query how many of the dates are 5 years or older?

Do you want five years ago today, or 1/1/five years ago? Is your date column actually a datetime column, in which case you might want to 'set' the time to 00:00.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
craig.jenkins
craig.jenkins
Right there with Babe
Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)

Group: General Forum Members
Points: 730 Visits: 206
Thank Guys, i've attached some dummy data below. As you can see the regdates are not older than 5 years

-- Sample data
IF OBJECT_ID('tempdb..#LOGFILE') IS NOT NULL DROP TABLE #LOGFILE
SELECT * INTO #LOGFILE FROM (VALUES
('BAA', '2018-02-19 10:53:08.837','2016-03-31 00:00:00.000','123451'),
('BAA', '2018-02-19 10:53:08.837','2015-02-28 00:00:00.000','123452')


) d (Dealer, Created, Regdate, RegNo)


SELECT DISTINCT * FROM #LOGFILE
WHERE #LOGFILE.Dealer = 'BAA'--
AND #LOGFILE.Created >='2018-02-19' AND #LOGFILE.Created <'2018-02-20'
AND #LOGFILE.RegdATE > DATEADD (year, -5 , GETDATE() )
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)

Group: General Forum Members
Points: 194915 Visits: 23765
Thank Guys, i've attached some dummy data below. As you can see the regdates are not older than 5 years


And that is why you are selecting them.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
craig.jenkins
craig.jenkins
Right there with Babe
Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)

Group: General Forum Members
Points: 730 Visits: 206
I'm an idiot, sorry guys
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)

Group: General Forum Members
Points: 194915 Visits: 23765
craig.jenkins - Tuesday, February 20, 2018 5:50 AM
I'm an idiot, sorry guys

Don't be so hard on yourself, we've all done it Smile



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
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