I have a table accountmaster.
when given a date,it should return number of customers enrolled(DOE) in last three years of the given date.
here is my query, which is not returning any values.plz help me with changes .TIA
select count(ACID) as numberofcustomers from ACCOUNTMASTER
where
datepart(yy,doe)>= datepart(yy,dateadd(yy,-2,'2012/04/10'))
and
doe<= datepart(yy,'2012/04/10')
Your issue is an apparent misunderstanding on what DATEPART does, and how to compare dates. So let us break down your WHERE clause.
where
datepart(yy,doe)>= datepart(yy,dateadd(yy,-2,'2012/04/10'))
This clause compares the year value of the date in column doe with the year of a date two years prior to a fixed date, representing the date submitted by the user. So, datepart(yy, dateadd(yy, -2,'2012/04/10')) returns a value of 2010 as an int. This is compared with the datepart(yy,doe), which returns the integer year of the date value stored in the doe column. Since these two data types are both int, the compare does what it should and returns information from records dated 2010/01/01 and later. No problems here - unless you are expecting to return data from 2010/04/10 forward, in which case you want to compare the value dateadd(yy, -2,'2012/04/10') to the value in the doe column.
and doe<= datepart(yy,'2012/04/10')
In this case, there is a comparison issue. You are attempting to compare the integer value 2012 with the date value in the doe column. Since these are not the same data type, we need to find out what is being compared.
I assume that the doe column is a datetime data type (if it was a date or datetime2, you would be getting an error.) An integer can be compared to a datetime, since SQL Server uses a numeric datatype to represent the datetime data type. A value of 2012 represents the date 1905/07/06 (use CAST(2012 as datetime) to see for yourself), so the WHERE clause translates out to something like:
WHERE datepart(year, doe) >= 2010
AND doe < '1905/07/06'
Since a date can not be both before July, 1905 and on or after Jan, 2010, no data is returned.
What is actually needed here depends on whether or not you only want to compare years, or you want to compare year/month/day. For years only, you can replace the last doe with DATEPART(year, doe) and have a functional query, albeit not a fast one. To compare year/month/day, use the DATEADD as mentioned above, and just use the actual entered date in the final comparison.
Hopefully, that helps.
Related
I try to query Apache Lucene index which is built using a database table that contains a date column and my query refers to this very column. In Luke the search expression I use is as follows:
column_name:[yyyy-MM-dd TO yyyy-MM-dd]
The results returned are records which do not have dates (in the queried column) with the year in the start value or older and the ones that do have year after the year in the start value up to the year in the end value. So if I write column_name:[2011-05-22 TO 2015-09-03] - I will get records with the dates in the column with years 2012, 2013, 2014, 2015.
However, the results will not be precise according to the search expression - the month and days value will be ignored. No matter what month and day I will set up - the search will result in records being returned with dates across each of the searched year (from 01.01 to 31.12).
The issue occurs if I use another date format like:
column_name:[yyyyMMdd TO yyyyMMdd]
I am looking to find what might be causing this? Is this a matter of date formatting while indexing? Or is this a matter of date format in the search expression?
I should add for more clarity that the search expression:
column_name:"yyyy-MM-dd"
will return records as exepcted - so with the date included in the expression.
Analysis is the issue.
When you index, your dates are getting split into three terms: "2011", "05", and "22". Any one of the terms can be matched against a range query, but not all three. So for [2011-05-22 TO 2015-09-03], the term 2011 does not fall lexicographically in that range, while 2012, 2013, 2014 and 2015 all do. For another example, if you searched for [1999-01-01 TO 2000-01-01], you would also find all dates on the twentieth of a month, because 20 falls within that lexicographic range.
Since this field is just a date, you should just avoid tokenizing it. Use StringField instead of a TextField, and re-index.
In my database, I have a report_year column (char(4)) and a report_month column (varchar(2)). I am making an ssrs report that would use a stored procedure and would pull data from this table and my parameters are the date and year. I am succesful at doing this by casting both of the columns and concatenating them, also adding a "/" in between. So in SSRS report, the parameter that users need to put is the month and date (ex. 09/2016).
Users want a drop down to get the dates. Since my parameter is a varchar, it would ask literally for the month and the year formatted above. Is there anyway to cast this to date without the day itself, only just the month and the year? I tried datediff and dateadd functions but I am not having any luck.
Select BegMonth = cast(Replace('09/2016','/','/01/') as date)
,EndMonth = EOMonth(cast(Replace('09/2016','/','/01/') as date))
Returns
BegMonth EndMonth
2016-09-01 2016-09-30
I have a query
SELECT
convert(varchar, dates, 101)
FROM
database
WHERE
dates BETWEEN '04/01/2015' AND '04/30/2015'
It returns all April dates but the problem is it is for every year (i.e. 2010, 2011, 2012, 2013, 2014, 2015). I think that it is not reading the last 2 digits of the year and just pulling everything from 2000 on. Am I correct in thinking this? How do I fix this?
Your dates column is likely not defined to be a date type. You can update the column type... or explicitly convert it in your query:
Select convert(varchar, dates, 101)
FROM database
Where convert(date,dates) BETWEEN '04/01/2015' and '04/30/2015'
The reason it would give you all April dates if your dates field was interpreted as a string of text is because all the April dates would be sorted together alphabetically. If it were properly interpreting your field to be a date, it would be able properly filter them.
You need to provide your dates in a standard format that SQL Server will understand correctly. See Correct way of specifying a given date in T-SQL.
For you it will be:
Where dates BETWEEN '20150401' and '20150430'
In tSQL this would work for you:
SELECT DATEPART(year, dates)
FROM [database]
WHERE DATEPART(MONTH, dates) = 4 AND DATEPART(year, dates)=2015;
I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12
I've got a column stored as the date/time data type upon which I wan't to calculate age in years. I'm using the DateDiff function in Access 2007.
SELECT Visits.ID, Visits.DOB, DateDiff("y",[DOB],[date]) AS age
FROM Visits;
As an aside to this question, I tried to use [date] to select the current date whenever the query is ran, but it just gave me a prompt to type in the date, it didn't grab it automatically.
DOB is the date of birth for each record.
Specify "yyyy" for the interval argument, instead of "y". "yyyy" is the year, "y" is the day of year.