简体   繁体   中英

CAST(date AS DATE) date format

I have following php routine to extract values from a table. I'm trying to convert the DATE type from "2014-08-10", value to "20140810", value

Rtn:

//query
$query = mysql_query("SELECT CAST(date AS DATE), EtotalDay from MonthData group by 1 order by 1") 
OR die ('Query is invalid: ' . mysql_error());

//write the results
while ($row = mysql_fetch_array($query)) {echo  $row['CAST(date AS DATE)'] . "," . $row['EtotalDay'] . "\n";

Haw can I do this?

You should use

DATE_FORMAT(date, '%Y%m%d')

instead of CAST(), complete with an column alias name for the retrieving of the field

$query = mysql_query("SELECT DATE_FORMAT(date, '%Y%m%d') formattedDate, EtotalDay from MonthData group by 1 order by 1") or die ('Query is invalid: ' . mysql_error());


//write the results
while ($row = mysql_fetch_array($query)) {echo  $row['formattedDate'] . "," . $row['EtotalDay'] . "\n";

see the manual, DATE_FORMAT

DATE_FORMAT(date,format)

Formats the date value according to the format string.

The following specifiers may be used in the format string. The “%” character is required before format specifier characters.

%d Day of the month, numeric (00..31)
%m Month, numeric (00..12)
%Y Year, numeric, four digits

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM