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.
Related
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.
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
SELECT strftime('%W', 'Week'), sum(income) FROM tableOne GROUP BY Week;
Format for date is a simple date: YYYY-MM-DD
PROBLEM: When run no value for the Week column is provided. Any suggestions?
There is data in the table and when the query is run the income is summarized by the date in the week column. Thing is, this column contains a date that may be any day of the week and often multiple different days of the same week. I need to summarize the income by week.
In SQL, 'Week' is a string containing four characters.
To reference the value in a column named "Week", remove the quotes: Week.
Sometimes we have dates that are unknown, eg: We won't know the date but just the month and year.
eg: --/Dec/2015, the date is not known? How do we accept these kind of values?
Any ideas on can this be stored on SQL Server database?
I would store it as a date in the database. Because with a date you have benefits like:
Calculation function like DATEADD and DATEDIFF.
You will be able to sort on one column
Index will be one column
When you need to get the year and the month. You just use MONTH and YEAR
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