in Education by
I am using SQL Server 2014 and I have the following T-SQL query which pulls data from a View Table: SELECT * from MyView WHERE StayDate >= '2014-07-01' I need to add a filter to this query that will be applied on a field called "CreatedOn" (it is datetime field). The filter needs to filter the "CreatedOn" based on the SAME DATE LAST YEAR. Thus, if today's date is '2015-10-26', my query should look this: SELECT * from MyView WHERE StayDate >= '2014-07-01' AND CreatedOn <= '2014-10-26' Since this query will be used in a PowerPivot environment, I am thinking along these lines: SELECT * from MyView WHERE StayDate >= '2014-07-01' AND CreatedOn <= getdate() How do I modify the getdate() part so that it becomes the Same Date Last Year? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
To get the date a year before the current date, you could use: DATEADD(YEAR, -1, GETDATE()) However, since that includes the time component, there's a possibility that some records will be left out. You should use this instead: < DATEADD(DAY, 1, DATEADD(YEAR, -1, DATEDIFF(DAY, '19000101', GETDATE()))) The above will return the date a year before the current plus one day. That is, if today's date is '2015-10-26', the above will return '2014-10-27'. Note that this will be without a time component and you should be using < for the comparison. More common date routines.

Related questions

0 votes
    I have the following simplified query which should filter the decrypted column values SELECT ResourceId, ClientId, ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    Let's say I have the following simple table variable: declare @databases table ( DatabaseID int, Name ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    The date stored in my database is 01-01-1900 for field emp_doj(Data Type DATE). But while retrieving ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Which header of HTTP response, provides the date and time of the resource when it was last modified?...
asked Nov 7, 2020 in Technology by JackTerrance
0 votes
    The different variant of Date() constructor to create date object is/are ___________ i. new Date(date) ii ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    Which of the following input control represents a date (year, month, day) encoded according to ISO 8601 in Web Form 2.0?...
asked Apr 10, 2021 in Education by JackTerrance
0 votes
    Which of the following input control represents a date consisting of a year and a week number encoded according to ISO 8601 in Web Form 2.0? A - week B - time C - number D - range...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    Which of the following input control represents a date consisting of a year and a month encoded according to ISO 8601 in Web ... - datetime B - datetime-local C - date D - month...
asked Dec 1, 2020 in Technology by JackTerrance
0 votes
    Which of the following input control represents a date (year, month, day) encoded according to ISO 8601 in Web Form 2.0? A - datetime B - datetime-local C - date D - month...
asked Dec 1, 2020 in Technology by JackTerrance
0 votes
    How will you add 1 year to current date using local datetime api of java8 in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    Your client noticed last month that his ad is often shown next to another advertiser's ad for the same search ... to other advertisers? Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    How to display the top five and the last five sales in the same view?...
asked Oct 30, 2020 in Technology by JackTerrance
0 votes
    What will be the syntax to find current date and time in format "YYYY-MM-DD"?...
asked Dec 18, 2020 in Technology by JackTerrance
+1 vote
    What is a pointer to a function? Give the general syntax for the same in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    Two friends were born in 1984 .The ptobability that they have same birth date is. a. 1/365 b. 1/366 c. 2/365 d. 3/365 Select the correct answer from above options...
asked Nov 13, 2021 in Education by JackTerrance
...