SQL - choose number from string, and replace it - mysql

I hav the next query in SQL:
SELECT id FROM student WHERE DATE LIKE '%$inputdate'
the inputDate is in the next format: 2010.08.03
I also want to change the date of the date, by increasing the day in a week (+7).
I try this:
SELECT id
FROM student
WHERE DATE LIKE '%$inputdate'
OR DATE Like '%CAST(RIGHT("2012-11-03", 2)AS INT) + 7;
But it gives me SQL syntax error. How can I write it correctly?

since you are using MySQL, make use of DATE() function
SELECT id
FROM student
WHERE DATE(`DATE`) = DATE(inputdate) OR
DATE(`DATE`) = DATE(DATE_ADD(DATE(inputdate),INTERVAL 7 DAY))
SOURCES
DATE()
DATE_ADD()

Related

Extract only the month in a query CodeIgniter

I want to make a query where I get people birthday's in a current month, I have problems with MONTH() it does not work for me, the date format in the database is: 04/18/1990, and I want to compare if the month current date('m') is equal to the month of the database.
Any suggestions?
you can try this one
SELECT * FROM table_name where extract(month from BirthDate) = date("m");
where date("m") return current month and BirthDate is column name in which you have stored birthdate.
Try this
"select month(str_to_date(birthdate), '%Y/%m/%d')
from users
where month(str_to_date(birthdate), '%Y/%m/%d') = '$month'"
Here, i m assuming your delimeter is '/', Please set your delimeter according to your string date i.e 2013/05/04 or 2013,05,04 or 2013-05-04
could be you have some convertion issue try using
select month(str_to_date(my_col, '%m/%d/%Y'))
from my_table
where month(str_to_date(my_col, '%m/%d/%Y')) = month(now())

Put in where sql only the date from datetime

I have a table in my database like this :
id date origine
1 2015-12-04 16:54:38 1
Now I want to get only data witch have the date = 2015-12-04. So I tried like this :
select * from table where id = 1 and date = "2014-12-04"
But I have no data. Can you help me please ?
You can use the date function:
where id = 1 and date(date) = '2015-12-04'
However, for performance reasons, it is often better to use inequalities. This allows MySQL to use an index on id, date for the query:
where id = 1 and
(date >= '2015-12-04' and date < date_add('2014-12-04', interval 1 day))
you can use Date Function of mysql which returns date from DateTime or truncate the Time part
select * from table where id = 1 and Date(date) = "2014-12-04"
There are several date related function out there you can use, take the following:
select *
from table
where id = 1
and date_format(date, '%Y-%m-%d') = '2015-12-04';
date_format will format your date column to a particular format.
In MSSQL you can simply say
SELECT *
FROM Table
WHERE ID = 1
AND Date > '2015-12-04'
I'm not familiar with mysql, but I assume something similar would work here. This date gets formatted as 2015-12-04 00:00:00 so in effect it matches everything with a date of 2015-12-04 and a time greater than 00:00:00.
If you happen to have rows with time of 00:00:00, just use >= instead.

TIMESTAMPDIFF, multiple row results on new column

I have a student database which has a date of enrolment column. I need to run a query on sql that lets look up this date and today's date, and display the difference in yy Years mm Months.
I'm using TIMESTAMPDIFF to work out the difference between these dates but can only produce one value, how can I adapt it to work it out for the entire row? i need to create a new column with this data.
This is the query:
SELECT TIMESTAMPDIFF(MONTH, CURDATE(), (SELECT time_enrolled FROM student) )
AS newDate
If I add a "where" statement at the end i get the specified id for example:
SELECT TIMESTAMPDIFF(MONTH, CURDATE(), (SELECT time_enrolled FROM student WHERE f_id = 4) )
AS newDate
Don't use a subquery. Your original query should be:
SELECT TIMESTAMPDIFF(MONTH, CURDATE(), time_enrolled) AS newDate
FROM student;
The subquery will generate an error if it returns more than one row in a scalar context -- such as returning a value in the select statement.
You can add an appropriate where clause to this query if you like.
EDIT:
To get years and months, use modulo arithmetic:
SELECT floor(TIMESTAMPDIFF(MONTH, CURDATE(), time_enrolled) / 12) as years,
mod(TIMESTAMPDIFF(MONTH, CURDATE(), time_enrolled), 12) as months
FROM student;

Fetch month and day value from date attribute of table in mysql

I have created a table as below.
actor(id, first_name, last_name, dob);
Where dob is in Y-m-d format. So I would like to fetch actors, who they have b'days for perticluar month or perticcular day. Please help me to get the solution.
Use this query for date
select * from actor where DAY(dob) = 12;
And for month
select * from actor where MONTH(dob) = 5;
You can combine both of them as well using AND operator.
Have a look at this. You will find more date and time functions that you can use
Try day and month functions
select * from actor
where day(dob)=x and month(dob)=y
See more datetime functions here http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Select by Month of a field

How can I get records from my table using month/year? I have a table like this:
Name - varchar
DueDate -datetime
Status -boll
DueDate is project due date, I want record corresponding to month/year, not full date, I mean record for specific month.
How can I do this in mysql?
Simply use MONTH() and YEAR():
SELECT * FROM Project WHERE MONTH(DueDate) = 1 AND YEAR(DueDate) = 2010
You could use a between statement:
select * from Project
where DueDate between '2010-01-01' and '2010-02-01'
Do not use this here recommended solution with MONTH() and YEAR(). It prevents MySQL to use index on column DueDate if you have one and it forces MySQL to examine all rows in table.
Instead, use BETWEEN clause like this:
SELECT * FROM Project
WHERE DueDate BETWEEN '2010-01-01' AND '2010-02-01'
Or another solution with IN clause:
SELECT * FROM Project
WHERE DueDate IN ('2010-01-01', '2010-01-02', '2010-01-03', ..., '2010-01-31')
This two solutions allows MySQL to use index, so performance will be better.
SELECT Name FROM Table Where MONTH(datetime) = 1;
1 = January.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_month
it will select current year's specific month
SELECT * FROM Project WHERE MONTH(DueDate) = 1 AND YEAR(DueDate) = YEAR(NOW())
If you input month format "Y-m" you can use:
SELECT * FROM Project WHERE DATE_FORMAT(DueDate,"%Y-%m") = '2010-01'
You can extract the MONTH() and YEAR() for your DueDate.
SELECT * WHERE MONTH(DueDate) = '5' AND YEAR(DueDate) = '1987';
SELECT * FROM TABLE WHERE DueDate LIKE '2020-01%';
use in case you can substring your date data.