I am having some problems with Mssql 2008 and Yii Framework with incorrect bracket placement when using the query builder.
$queryCommand = Yii::app()->db->createCommand();
$selectArray = array();
$selectArray[] = 't.id';
$selectArray[] = "CASE t.name WHEN '' THEN t.name ELSE t.name + ' : ' + t.description END AS text";
$queryCommand->select($selectArray);
$queryCommand->from("table t");
I get a weird query string.
SELECT [t].[id], [CASE] AS [t].[name WHEN '' THEN t].[name ELSE t].[name + ' : ' + t].[description END AS text]
FROM [table] [t]
Is there a better way to do the case?
If I add a SUBSTRING function to the t.description no brackets are added.
$queryCommand = Yii::app()->db->createCommand();
$selectArray = array();
$selectArray[] = 't.id';
$selectArray[] = "CASE t.name WHEN '' THEN t.name ELSE t.name + ' : ' + SUBSTRING(t.description,0,30) END AS text";
$queryCommand->select($selectArray);
$queryCommand->from("table t");
I get
SELECT [t].[id], CASE t.name WHEN '' THEN t.name ELSE t.name + ' : ' + SUBSTRING(t.description,0,30) END AS text
FROM [table] [t]
Which runs fine because the brackets aren’t required.