-3

Database:

   | date_paid (int) |
    1535558400
    1539532800

I am trying to query the following but i get SQL syntax error for the date part.

    $id = $this->input->post('id');
    $date = $this->input->post('date');

    $this->db->where('id',$id);
    $this->db->where('DATE_FORMAT(date_paid, "%Y-%m-%d")',$date);
    $query=$this->db->get('payments');
    $result=$query->row();

Is there any way to change the date format of the column date_paid so that i can actually compare it to the $this->input->post('date') data?

Error log:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2018-10-30'' at line 4

I am trying to modify an existing web application that is why as much as possible i do not want to change the data type of the column date_paid

thanks in advance

Ronald Torres
  • 199
  • 2
  • 4
  • 19

3 Answers3

0

Please, use like this

 DATE_FORMAT(FROM_UNIXTIME(`date_paid`), '%Y-%m-%d') AS 'date_changed'

You can refer here

Igor Carvalho
  • 668
  • 5
  • 19
0

You need to use

DATE_FORMAT(FROM_UNIXTIME(date_paid), '%Y-%m-%d') AS 'date_formatted'

so something like:

$id = $this->input->post('id');
$date = $this->input->post('date');

$this->db->select("DATE_FORMAT(FROM_UNIXTIME(`date_paid`), '%Y-%m-%d') AS date_formatted");
$this->db->from('payments');
$this->db->where("DATE_FORMAT(FROM_UNIXTIME(`date_paid`), '%Y-%m-%d') = " . $date, '', false);

$result = $q->row();
marcogmonteiro
  • 2,061
  • 1
  • 17
  • 26
  • You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (`payments`) WHERE `id` = '28' AND `date_formated` = '2018-10-30' at line 2 – Ronald Torres Dec 18 '18 at 09:31
  • @RonaldTorres Updated the query. – marcogmonteiro Dec 18 '18 at 09:57
-2

Use the syntax below

select convert(varchar(4), year(ColumnDate))+'/'+convert(varchar(4), month(ColumnDate))+'/'+convert(varchar(4), day(ColumnDate)) from YourTable

Paul Davies
  • 97
  • 2
  • 9