tags:

views:

39

answers:

5

Hi.

string selectSql = "update " + table + " set state_" + mode + "_id=1 WHERE stoping_" + mode + " < current_date;";

when i call current_date, it return yyyy-MM-dd format, but i want to return dd.MM.yyyy format, how can i do that. please help. my program works fine when i am trying

string selectSql = "update " + table + " set state_" + mode + "_id=1 WHERE stoping_" + mode + " < '16.04.2010';";
A: 

YYYY-MM-DD is mySQL's native date format. I find it hard to believe that doing a comparison against a DD-MM-YYYY string works?

Anway, what you are looking for is DATE_FORMAT().

Pekka
tring selectSql = "update " + table + " set state_" + mode + "_id=1 WHERE stoping_" + mode + " < '16.04.2010';";works fine!!!!, but i need to give current_date
Armen Mkrtchyan
A: 

Use any function like this to return your format

function ChangeDateforShow($inputdate) {
  if($inputdate>0) {
    $date  = substr($inputdate,8,2);
    $month = substr($inputdate,5,2);
    $year  = substr($inputdate,0,4);
    $show = $date.".".$month.".".$year;
    return $show;
  }
}
Karthik
no it doesnt works in SQL select command, when i am passing string, for example customDate, which is equal "16.04.2010", it's not workingstring selectSql = "update " + table + " set state_" + mode + "_id=1 WHERE stoping_" + mode + " < 'customDATE';";
Armen Mkrtchyan
function ChangeDateforDB($inputdate) { if($inputdate>0) { $date = substr($inputdate,0,2); $month = substr($inputdate,3,2); $year = substr($inputdate,6,4); $show = $year."-".$month."-".$date; return $show; }}Ok use this function in db itself likeselectSql = "update " + table + " set state_" + mode + "_id=1 WHERE stoping_" + mode + " < 'ChangeDateforDB(customDATE)';"Or otherwise you will move for mysql date_format() only
Karthik
A: 

The MySQL date format is YYYY-MM-DD, but using str_to_date() and date_format() you can change date format.

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

In your case, try DATE_FORMAT(current_date, '%d.%M.%Y')

hgulyan
thanks, its working!!!
Armen Mkrtchyan
you're welcome)
hgulyan
this example won't work though. DATE_FORMAT('2010-04-16', '%d.%M.%Y') would
Col. Shrapnel
I just pasted here date in his example, I could write just <current_date>, that's not the point.
hgulyan
That's huge SO problem: everyone "just paste", not even trying to think. One don't need DATE_FORMAT for the current date. As it would be senseless to use formatted date in comparison statements.
Col. Shrapnel
It's just your problem. You think, he writes in his code '2010-04-16' or '16.04.2020' ? He just needed to know how to convert the date to 'dd.MM.yyyy' and nothing more, I gave him the answer how to do that, so you think you gave him that answer he wanted?
hgulyan
And why did it work in for him, if you say, that it doesn't work? It means, he got what he wanted. The End.
hgulyan
A: 
function ChangeDateforDB($inputdate) {
  if($inputdate>0) {
    $date  = substr($inputdate,0,2);
    $month = substr($inputdate,3,2);
    $year  = substr($inputdate,6,4);
    $show = $year."-".$month."-".$date;
    return $show;
  }
}

Ok use this function in db itself like

selectSql = "update " + table + " set state_" + mode + "_id=1 WHERE stoping_" + mode + " < 'ChangeDateforDB(customDATE)';"

Or otherwise you will move for mysql date_format() only

Karthik
A: 

mysql has a function that return current date - curdate()

So, your query must be like

update table set state_mode_id=1 WHERE stoping_mode < curdate()

But you have 2 major faults (or even 3):

  1. date field must have a format of YYYY-MM-DD, not anything else
  2. such an update is senseless at all, because all such states being evaluated at select time
  3. assembling table name at query time is a sign of very bad database design
Col. Shrapnel