current_date casting - mysql

string selectSql = "update " + table + " set state_" + mode + "_id=1 WHERE stoping_" + mode + " < current_date;";
When I call current_date, it returns 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';";

The MySQL date format is YYYY-MM-DD, but using str_to_date() and date_format() you can change the 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')

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):
date field must have a format of YYYY-MM-DD, not anything else
such an update is senseless at all, because all such states being evaluated at select time
assembling table name at query time is a sign of very bad database design

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().

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;
}
}

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

Related

SQL Server - Convert datetime to JSON date format

How can I convert SQL smalldatetime or datetime like this:
select cast(getdate() as smalldatetime)
To JSON format like this:
/Date(1576278000000+0100)/
I just find this post in another issue like you got it.
Maybe it would work for you
"render": function ( data, type, row ) {
var fecha = new Date(parseInt(data.match(/\d+/)[0]));
return fecha.getMonth() + 1 + '/' + fecha.getDate() + '/' + fecha.getFullYear();;
},
"targets": 0
and this is the link that I found
https://es.stackoverflow.com/questions/125686/convertir-fecha-en-javascript

Wordpress and MySQL, putting a column of results into an array

I have a column in MySQL in the following format after I run a certain $sql:
colname
12
15
10
23
12
2
What I want is to transfer this into
$colname = array(12,15,10,23,12,2)
I came up with:
$results = $wpdb->get_results($sql);
$colname=array();
foreach($results as $result){
$colname[] = $result;}
Is this the most efficient way? The order is also very important
You can probably use something like:
$sql =
"SELECT " .
" group_concat(colname ORDER BY order_by SEPARATOR ',') AS txt_result " .
"FROM " .
" t ; " ;
$results = $wpdb->get_results($sql);
$colname = split(',', $results[0]['txt_result'])
Note that you need a certain ORDER BY expression. By default, SQL does not provide any determined order. $colname will be an array of textual representations of your numbers. You should convert them to numbers if need to.
See the result of the SQL query at dbfiddle here
Reference:
GROUP_CONCAT()

SQL query to retrieve data between two dates

I've written following code:
Dim date1 As Date
Dim date2 As Date
date1 = Convert.ToDateTime(DatePickerFromDate.Text)
date2 = Convert.ToDateTime(DatePickerToDate.Text)
Dim cnd As New OleDbCommand("SELECT * FROM Sales WHERE Invoice_Date BETWEEN " + date1 + " AND " + date2 + "", om)
om.Open()
Dim da As OleDbDataReader = cnd.ExecuteReader
While da.Read()
ComboBox1.Items.Add(da(0))
End While
da.Close()
om.Close()
I want to retrieve data between two dates that are been taken from two datepickers.
I tried BETWEEN, also i tried >= =< but result was empty though database contains data. Please help where I'm getting wrong
Your code is probably generating an error. When doing this type of querying, you should store the query string after substitution and print it out. You seem to be missing delimiters around the dates. So this may work in your specific case.
New OleDbCommand("SELECT * FROM Sales WHERE Invoice_Date BETWEEN '" + date1 + "' AND '" + date2 + "'", om)
However, you then need to be careful about the format of the dates. The application layer and the database might use different formats. If you are substituting directly into the query string, then use the format YYYY-MM-DD -- it is the ISO standard date format and generally understood.
Even better is to learn how to parameterize queries so you can actually pass in the date values as date parameters.
If you're using MS Access, this should be the syntax...
SELECT * FROM Sales WHERE Invoice_Date>=#" + date1 + "# and Invoice_Date<=#" + date2 + "#"
If you're using MS SQL Server or MySQL, then do something like this...
SELECT * FROM Sales WHERE Invoice_Date>='" + date1 + "' and Invoice_Date<='" + date2 + "'"

Maybe TIMEDIFF function in sql is not accepting column name as one of the parameters

My requirement is to pull records from mysql database which have just 5 mins left from the current time as per the one of the columns in the database. The column is has user inserted datetime.
date_default_timezone_set("UTC");
$utc_time = date("Y-m-d H:i:s", time());
echo "UTC Time: " . $utc_time . "<br>";
$result = mysql_query("select reminder_text, reminder_subject, reminder_date_time_utc $table_name where (TIME_TO_SEC (TIMEDIFF(reminder_date_time_utc , '$utc_time')) < 300) AND (TIMEDIFF(reminder_date_time_utc , '$utc_time')) > 0) ") or die(mysql_error());
here the reminder_date_time inside the TIMEDIFF function is the column name to pick up the DATETIME. Using this query I do not get the results but if I place the date instead of reminder_date_time it gives me the correct output. For example if I say TIMEDIFF('2013-07-12 11:05:00' , '$utc_time') it gives me the correct output. And this same value: 2013-07-12 11:05:00 is actually present in one of the rows of this column reminder_date_time_utc
Any advice where I am going wrong... Does TIMEDIFF function not accept column name as one of the parameters.
Do you forgot the FROM in your sql?
Why dont you try to do it like following:
$utc_time = date("Y-m-d H:i:s", time() - 30000);
and change the query to
'SELECT * FROM ' . $tablename . ' WHERE reminder_date_time_utc > "' . $utc_time . '"';
or use DATE_ADD() function :
'SELECT * FROM ' . $tablename . ' WHERE reminder_date_time_utc > DATE_ADD(NOW(), INTERVAL -'5' SECOND));

MS Access Syntax error (missing operator)

I am using the below code to get the record from a access database. Now i got the below error Syntax error (missing operator) in query expression.
I can't able to understand this issue. Please help me to fix this issue.
cmd = new OleDbCommand(#"Select * from tbl_men_schedule where fld_mem_id=" + 0 + " and fld_startdate=" + Convert.ToDateTime(txt_startDate.Text) + " and fld_enddate=" + Convert.ToDateTime(txt_enddate.Text) + "", con);
da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Syntax error (missing operator) in query expression 'fld_mem_id=0 and fld_startdate=1/4/2013 12:00:00 AM and fld_enddate=4/11/2013 12:00:00 AM'.
If fld_startdate and fld_enddate are Date/Time data type, enclose those date values with the # delimiter.
where
fld_mem_id=0
and fld_startdate=#1/4/2013 12:00:00 AM#
and fld_enddate=#4/11/2013 12:00:00 AM#
If they are text data type, enclose them with quotes.
where
fld_mem_id=0
and fld_startdate='1/4/2013 12:00:00 AM'
and fld_enddate='4/11/2013 12:00:00 AM'
However if you use a parameter query instead, you wouldn't need to delimit those values.
Your statement should be -
"Select * from tbl_men_schedule where fld_mem_id=" + 0 + " and fld_startdate='" + Convert.ToDateTime(txt_startDate.Text) + "' and fld_enddate='" + Convert.ToDateTime(txt_enddate.Text) + "'"
Hope this helps you
If you hardcoding "0"... and you're missing single quotes.
"Select * from tbl_men_schedule where fld_mem_id=0 and fld_startdate='" +
txt_startDate.Text + "' and fld_enddate='" +
txt_enddate.Text + "'"