S
S
Sergey Beloventsev2019-03-28 23:35:41
Yii
Sergey Beloventsev, 2019-03-28 23:35:41

How to use to_char in Yii2?

SELECT   to_char(events.date_start, 'MM') AS month_start
FROM "events"
     WHERE "event_id"=13;

how it is possible to create in ActiveRecord similar request I do so.
Event::find()->select('to_char(events.date_start, \'MM\') AS month_start)->where(["event_id"=>13])->one()

ERROR: syntax error (example position: \"AS\")\nLINE 1: to_char(events.date_start, \"'MM')\" As

for some reason, incomprehensible \" appear before and after MM)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2019-03-28
@Sergalas

When an Expression object is embedded within a SQL statement or fragment, it will be replaced with the $expression property value without any DB escaping or quoting. For example,

$expression = new Expression('NOW()');
$now = (new \yii\db\Query)->select($expression)->scalar();  // SELECT NOW();
echo $now; // prints the current date

https://www.yiiframework.com/doc/api/2.0/yii-db-ex...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question