how to format date field in MYSQL - mysql

All,
I need to retrieve the date in some specific format in MYSQL.
For eg,
it should return month/year(10/2009).
My MYSQL version is 5.1.
Thanks,
Srinivasan.

DATE_FORMAT function
DATE_FORMAT(col,'%m/%Y')

date_format(yourdatefield, '%c/%Y') as formatted_date
will do that- http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

use DATE_FORMAT function
http://www.mysqlformatdate.com will help you use the function properly

Check the documentation for this function in MySQL
DATE_FORMAT(date, format)
DATE_FORMAT(col, '%m/%Y')

For IIS, I just use a function:
FUNCTION mySLQDate(xdate)
IF xdate <> "" THEN
mySLQDate = year(xdate) & "/" & month(xdate) & "/" & day(xdate)
END IF
END FUNCTION

Related

Retreive date from MySQL DATETIME in Doctrine QueryBuilder

I have a MySQL date stored in DATETIME format. So I would like to know how to use date() in my Doctrine QueryBuilder's where clause. For example, 2013-02-01 12:51:17 is the date in MySQL. But I need to retrieve only the date. This is what I have tried:
$qb = $this->getEntityManager()->createQueryBuilder()
->select('t.balance','a.id','t.date')
->from('TestMainBundle:Transaction','t')
->groupBy('a.id')
->orderBy('a.id')
->where("t.date in date('t.date') ");
return $qb->getQuery()->getResult();
I received the following error:
QueryException: [Syntax Error]: Error: Expected Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS, got 'date'
Hi You can use SUBSTRING to fix your probleme
->where('SUBSTRING(t.date, 1, 10) IN (:param)')
->setParameter('param', array('2017-04-06'))
As it is pointed out in the comments, you cannot use mysql-specific date() or date_format() functions in Doctrine.
But for the particular case of searching a certain date in the datetime field, you can treat a date like a string, and thus use LIKE operator
->Where('t.date LIKE :date')
->setParameter(':date', "$date%")
As of
But I need to retreive only the date
you just format the returned value using format("Y-m-d") method. i.e.
echo $row->getDate()->format("Y-m-d");
You don't need the "date" in your where clause.
Juste remove it like this :
->where('t.date in (:yourwanteddate)')
->setParameter('yourwanteddate', '2013-02-01 12:51:17');

SQL Incorrect syntax near '> when calculating age from date of birth

I am trying to get age while calculating it from dob (DATE) and compare it whether is it greater than 64.However it gets an error Incorrect syntax near '>'.
Below are the code
sqlStr.Append("SELECT count(CustomerRace) as CustomerCount, CustomerRace");
sqlStr.Append("FROM Customer");
sqlStr.Append("WHERE SELECT DATEDIFF(YYYY,CustomerDOB,GETDATE()) > 64");
sqlStr.Append("GROUP BY CustomerRace");
Change your code like this, hope this will work for you.
sqlStr.Append("SELECT count(CustomerRace) as CustomerCount, CustomerRace ");
sqlStr.Append("FROM Customer ");
sqlStr.Append("WHERE DATEDIFF(YY,CustomerDOB,GETDATE()) > 64 ");
sqlStr.Append("GROUP BY CustomerRace");
Datediff function incorrect parameter count.Use below one. And also if you use mysql GETDATE() function will not work use now() to get current date;
sqlStr.Append("SELECT count(CustomerRace) as CustomerCount, CustomerRace");
sqlStr.Append("FROM Customer");
sqlStr.Append("WHERE DATEDIFF(CustomerDOB, now()) > 64");
sqlStr.Append("GROUP BY CustomerRace");
sqlStr.Append("SELECT count(CustomerRace) as CustomerCount, CustomerRace");
sqlStr.Append("FROM Customer");
sqlStr.Append("WHERE DATEDIFF(yy,CustomerDOB,GETDATE()) > 64");
sqlStr.Append("GROUP BY CustomerRace");
which is still having the same issue

Codeigniter - Format date in WHERE clause

Is it possible to format the following date in an active record where clause?
I need to format channel_titles.entry_date to be in the format Y-m-d, like the $yesterday variable.
$yesterday = date('Y-m-d', strtotime('yesterday'));
$this->db->where('channel_titles.entry_date', $yesterday);
You can use DATE_FORMAT function like that:
$this->db->where("DATE_FORMAT(from_unixtime(channel_titles.entry_date), '%Y-%m-%d')", $yesterday, false);
Try this
$this->db->where("DATE_FORMAT(channel_titles.entry_date,'%Y-%m-%d') ", $yesterday);

mysql - how to replace a sub-string?

a field is a varchar, i want to replace a sub string of this field.
ab123fg => ab456fg
the above is just a example.
just like the sub string to be replaced.
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace
REPLACE(str, 'ab123fg ', 'ab456fg')
EDIT:
MySQL doesnt have a find and replace regex function, maybe have a look at UDFs

formatting date field from MYSQL

I am using a DATE field in my MYSQL table, and pulling it through on a php page. The problem is it comes out as "2011-04-23"
Is there a way I can reformat this as 23/04/2011?
Thanks :)
date("d/m/Y", strtotime("2011-04-23"));
that should do it
date()
strtotime()
DATE_FORMAT(date,format)
Look here: http://dev.mysql.com/doc/refman/5.0/es/date-and-time-functions.html
Assuming variable $date contains your MySQL data:
$date = '2011-04-23';
$timezone = 'Europe/London'; // this is optional argument
$formatted = DateTime::createFromFormat('Y-m-d', $date, new DateTimeZone($timezone));
// or without the optional timezone - where php will assume the default timezone from your OS
$formatted = DateTime::createFromFormat('Y-m-d', $date);
echo $formatted->('d/m/Y');