I have a column (bday) which stores user's birthday info.
I searched similar questions and found that one
select * from users where datediff(year, bday, getdate()) between 18 and 22;
However, when I run this command I get
#1582 - Incorrect parameter count in the call to native function 'datediff'
What would be the correct way for this?
Error 1582 is a MySQL error, so presumably you are using MySQL.
The code you probably want is:
select u.*
from users u
where u.bdate >= curdate() - interval 22 year and
u.bdate < curdate() - interval 18 year;
This isn't exactly equivalent to the SQL Server (or Amazon Redshift) datediff(year . . . ) function. That would be more like:
select u.*
from users u
where year(u.bdate) - year(curdate()) between 18 and 22;
(Note: There might be an off-by-one error in this calculation.)
According to the documentation for MariaDB DATEDIFF only takes two arguments:
DATEDIFF(expr1,expr2)
expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation and it returns (expr1 – expr2) expressed as a value in days from one date to the other.
So your query should be:
select * from users where datediff(bday,getdate()) between 18 and 22;
Related
01) I have a MySQL table named issue. It is storing book details as book_no, book_name, is_date etc.. issued for various members.
02) I need to select list of books that should return on current date. The return date is calculated by adding 3 days to the is_date. I used following script. But I returned an empty result.
Variables used to Store return dates
$rdate=mysql_fetch_array(mysql_query("SELECT * FROM issue"));
$is_date=$rdate['is_date'];
$rt_date=mysql_fetch_array(mysql_query("SELECT
DATE_ADD('$is_date', INTERVAL 3 DAY) FROM dual"));
SELECT statement
SELECT issue.b_no, issue.b_name, issue.is_date
FROM issue WHERE '$rt_date' = CURDATE()
03) I can not understand what I am doing wrong.
I'm trying to get an SQL query to select all records from last month, I have this which from looking numerous places is exactly what I should need, and should work:
SELECT *
FROM orders
WHERE DATEPART(yy,DateOrdered) = DATEPART(yy,DATEADD(m,-1,GETDATE()))
AND DATEPART(m,DateOrdered) = DATEPART(m,DATEADD(m,-1,GETDATE()))
However I keep getting the error:
#1305 - FUNCTION retail.DATEPART does not exist
The query I'm using is word for word from other answers on here, yet I'm getting this error.
Thank you for any help -Tom
DATEPART is a Transact-SQL function, usable with Microsoft SQL Server. From the question tags, I assume you are using MySQL as your Database Management System.
Take a look at MySQL DATEDIFF
That would not work in mysql.
To translate that to mysql you could do:
SELECT *
FROM orders
WHERE YEAR(DateOrdered) = YEAR(DATE_SUB(CURDATE(), INTERVAL -1 MONTH))
AND MONTH(DateOrdered) = MONTH(DATE_SUB(CURDATE(), INTERVAL -1 MONTH))
See here for the date functions available in mysql.
It will also work.
SELECT *
FROM orders
WHERE MONTH(DateOrdered) = MONTH(CURRENT_DATE() - INTERVAL 1 MONTH)
AND YEAR(DateOrdered) = YEAR(CURRENT_DATE() - INTERVAL 1 MONTH)
I'm getting the above SQL error after executing this query.
SELECT r.SectionIDNum, r.PeopleIDNum, r.Completed, c.CourseID, s.DistrictIDNum, s.EndDate
FROM Registration r, Course c, Section s
WHERE r.SectionIDNum=s.SectionID AND c.CourseID=s.CourseIDNum AND r.Completed='Y'
AND s.EndDate between ('2012-06-31', 'yyyy-mm-dd') and ('2013-07-01', 'yyyy-mm-dd')
Apparently, the commas in the dates are causing the error but I don't know how to fix it.
June only has 30 days in it. So SQL Server is confused by your request to cast June 31 as a date.
This works fine:
SELECT CAST('2012-06-30' AS DATE)
One way to avoid end of month issues is to use the DATEADD() function, for example, to get one year and one day prior to July 1, 2013 like in your example:
SELECT DATEADD(day,-1,(DATEADD(year,-1,CAST('2013-07-01' AS DATE))))
Also, remember that BETWEEN is inclusive, so you're getting June 30 and July 1 in your example, perhaps just subtracting the year is sufficient.
Use the CAST() Function (CAST converts value of one data type to a different type. In this case, CHAR to DATETIME):
SELECT r.SectionIDNum
,r.PeopleIDNum
,r.Completed
,c.CourseID
,s.DistrictIDNum
,s.EndDate
FROM Registration r
,Course c
,Section s
WHERE r.SectionIDNum=s.SectionID
AND c.CourseID=s.CourseIDNum
AND r.Completed='Y'
AND s.EndDate BETWEEN CAST('20120630' AS DATETIME)
AND CAST('20130701'AS DATETIME)
You could also use:
CONVERT(DATETIME,'20130701')
Im running a sql query that is returning results between dates I have selected (2012-07-01 - 2012-08-01). I can tell from the values they are wrong though.
Im confused cause its not telling me I have a syntax error but the values returned are wrong.
The dates in my database are stored in the date column in the format YYYY-mm-dd.
SELECT `jockeys`.`JockeyInitials` AS `Initials`, `jockeys`.`JockeySurName` AS Lastname`,
COUNT(`runs`.`JockeysID`) AS 'Rides',
COUNT(CASE
WHEN `runs`.`Finish` = 1 THEN 1
ELSE NULL
END
) AS `Wins`,
SUM(`runs`.`StakeWon`) AS 'Winnings'
FROM runs
INNER JOIN jockeys ON runs.JockeysID = jockeys.JockeysID
INNER JOIN races ON runs.RacesID = races.RacesID
WHERE `races`.`RaceDate` >= STR_TO_DATE('2012,07,01', '%Y,%m,%d')
AND `races`.`RaceDate` <= STR_TO_DATE('2012,08,01', '%Y,%m,%d')
GROUP BY `jockeys`.`JockeySurName`
ORDER BY `Wins` DESC`
It's hard to guess what the problem is from your question.
Are you looking to summarize all the races in July and the races on the first of August? That's a slightly strange date range.
You should try the following kind of date-range selection if you want to be more precise. You MUST use it if your races.RaceDate column is a DATETIME expression.
WHERE `races`.`RaceDate` >= STR_TO_DATE('2012,07,01', '%Y,%m,%d')
AND `races`.`RaceDate` < STR_TO_DATE('2012,08,01', '%Y,%m,%d') + INTERVAL 1 DAY
This will pick up the July races and the races at any time on the first of August.
But, it's possible you're looking for just the July races. In that case you might try:
WHERE `races`.`RaceDate` >= STR_TO_DATE('2012,07,01', '%Y,%m,%d')
AND `races`.`RaceDate` < STR_TO_DATE('2012,07,01', '%Y,%m,%d') + INTERVAL 1 MONTH
That will pick up everything from midnight July 1, inclusive, to midnight August 1 exclusive.
Also, you're not using GROUP BY correctly. When you summarize, every column in your result set must either be a summary (SUM() or COUNT() or some other aggregate function) or mentioned in your GROUP BY clause. Some DBMSs enforce this. MySQL just rolls with it and gives strange results. Try this expression.
GROUP BY `jockeys`.`JockeyInitials`,`jockeys`.`JockeySurName`
My best guess is that the jocky surnames are not unique. Try changing the group by expression to:
group by `jockeys`.`JockeyInitials`, `jockeys`.`JockeySurName`
In general, it is bad practice to include columns in the SELECT clause of an aggregation query that are not included in the GROUP BY line. You can do this in MySQL (but not in other databases), because of a (mis)feature called Hidden Columns.
It is common to count the number of days that users login to the system (like stackexchange). In an ordinary user table, I update login information as
UPDATE users SET
last_login='date', number_of_login=number_of_login + 1, number_of_days=?
WHERE user_id='user_id'
where last_date is datetime
what is the best to check that current day is different from last_login to update number of days with number_of_day=number_of_day+1.
I have two methods in mind, but they seems to be naive:
Method 1: having a SELECT to catch last_login and compare it with current day in PHP
Method 2: using a trick like sub-SELECT
I hope to do this with one simple query (if possible).
I would use DATEDIFF MySql function: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff.
DATEDIFF() returns expr1 – expr2 expressed as a value in days from one
date to the other. expr1 and expr2 are date or date-and-time
expressions. Only the date parts of the values are used in the
calculation.
After you fetch results, you should check if fetched column is greater than zero to determine if dates are different.
So, if I don't get your comment wrong, you want to always update last_login, but number_of_days only if last_login != curdate(). Try this:
UPDATE users SET
number_of_days = if (last_login = curdate(), number_of_days, number_of_days + 1),
last_login='date', number_of_login = number_of_login + 1
WHERE user_id='user_id'