$fileQueryBuilder->columns(
[
"id" => "d.discovered_file_id",
"company_name" => "d.company_name"
]
);
This is the part of my query builder where I mention the column names to be selected/displayed. Can I handle the 'company_name' field to show it's value if it has one,
and something like 'Not available' if it's empty, in this part of the query builder itself? Is there a way of doing that, like using a CASE WHEN same as in SQL?
What I tried- CASE WHEN d.company_name IS NOT NULL THEN d.company_name ELSE 'Not available' END => d.company_name
, but this doesn't work.
Isn't IF better in this case?
IF(d.company_name IS NOT NULL, d.company_name, 'Not available') as company_name
Also PHQL only supports case syntac like this:
CASE column WHEN value THEN some expression ELSE some expression END
Related
I want to get all the columns from the products table and apply sql case on pick_8 and pick_12 columns only but to do this i have to write all the column names in select. Is there any way in laravel where i can apply the simple get() method to retrieve all the columns with applying case on only 2, without having to write all the column names manually.
$products = Product::where('is_archive',0)->select('humanId','name','cost','retailPrice','memberPrice','points',
DB::raw('(CASE WHEN pick_8 = 1 THEN "True" ELSE "False" END) AS pick_8'),
DB::raw('(CASE WHEN pick_12 = 1 THEN "True" ELSE "False" END) AS pick_12'))->get()->toArray();
To retrieve all the columns you can use DB::raw method also.
$products = Product::where('is_archive',0)->select(
DB::raw('products.*'),
DB::raw('(CASE WHEN pick_8 = 1 THEN "True" ELSE "False" END) AS pick_8'),
DB::raw('(CASE WHEN pick_12 = 1 THEN "True" ELSE "False" END) AS pick_12'))->get()->toArray();
CASE statement will overwrite original values of pick_8 and pick_12 columns.
P.S. By the way. I guess you trying to use wrong tools to reach your goal. Take a look at eloquent-mutators
Long story short i need to return a varchar value if stored value is null but eithers bring me a NULL value or varchar value here is the code.
in the table i have some fields null and some with data
CASE WHEN DS.DBIRTH+DS.MBIRTH+DS.YBIRTH =
'' THEN CONVERT(VARCHAR(10),PT.CLIENTDBIRTHDATE,111)
ELSE 'INPUT DATE OF BIRTH'
CASE WHEN PT.CLIENTBIRTHDATE IS NULL THEN ''
ELSE '1800-01-01'
END
END AS BFIELD
this brings me something like this
NULL
1917/05/02
NULL
1923/02/02
1967/01/05
NULL
but i need something like this
01/01/1800
1917/05/02
01/01/1800
1923/02/02
1967/01/05
01/01/1800
sorry for the ultra noob question
Your current case statement looks off. I imagine what you want to do is wrap the result in coalesce:
COALESCE(CASE
WHEN DS.DBIRTH+DS.MBIRTH+DS.YBIRTH = ''
THEN CONVERT(VARCHAR(10),PT.CLIENTDBIRTHDATE,111)
ELSE 'INPUT DATE OF BIRTH'
END, '01/01/1800') AS BFIELD
Without the Case statement it should be something as simple as....
select CONVERT(VARCHAR(10) ,
CAST(ISNULL(DateColumn,'01/01/1800') AS DATE)
,111)
I need an SQL query that selects from Column A if Column C contains the string 'ebook'. Otherwise, select from Column B.
So something like:
IF (Table.ColumnC = "ebook") SELECT Table.ColumnA AS Publisher
ELSE SELECT Table.ColumnB AS Publisher
Select
case when columnC = 'e-book'
then columnA
else columnB
end as Publisher
from myTable;
You need a case statement
I reformatted to help show how this works.
Basically Case is a scalar (row per row) IF statement
There can be multiple conditions WHEN, similar to else if. The function goes from condition to condition, if no conditions are passed, the ELSE value is used.
There are simple and search forms of CASE Documentation
Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
select case columnC when 'ebook' then columnA else columnB end as publisher
from my_table;
In MySQL you can write it like this:
SELECT IF(Table.ColumnC = 'ebook', Table.ColumnA, Table.ColumnB) AS Publisher FROM Table...
If you have more than one possibility, then go for CASE..WHEN..THEN..ELSE..END.
I'm migrating a report from Crystal where i was able to create a column and fill using this formula...
if ({#SD Profile}="null") and ({#HD Profile 2}="null")
then ""
else
if ({#HD Profile 2}="null")
then "SD only"
else
if ({#SD Profile}="null")
then "HD only"
else
if({#SD Companion}="1")
then "HD+SD"
else "HD/SD"
I'd like to accomplish the same thing using SQL where i populate the values of a column based on the data in 3 other columns. Note I'm not adding a column to the DB just inserting into the report.
I've created the empty column in my SELECT statement using
CAST(NULL AS VARCHAR(30)) as "Content Type",
"SD_format"."name" as "SD Format",
"HD_format"."name" as "HD Format",
"destination"."sd_companion_required_flag"
Basically something like... If SD_Format.name ='null' then Content_Type = '' etc.
You should be able to replace the 'if' statement by a 'case' statement:
select
case
when SD_Profile is null and HD_Profile is null then ''
when HD_Profile is null then 'SD only'
when SD_Profile is null then 'HD only'
when Destination = '1' then 'HD + SD'
else 'HD/SD'
end as 'Content_Type'
from ...
*"SD_format"."name" as "SD Format",
"HD_format"."name" as "HD Format",
case
when "SD_format"."name" is null and "HD_format"."name" is null then ''
when "HD_format"."name" is null then 'SD only'
when "SD_format"."name" is null then 'HD only'
when "destination"."sd_companion_required_flag" = '1' then 'HD + SD'
else 'HD/SD'
end as "Content_Type",
Project is in CI. I'm stuck on writing a where statement.
I have a table with many fields, but want to focus on filter by 'user_type' and 'photos_count'.
I have a feed that generates the proper users, but I want to add a where statement that if 'user_type' is '1', check to see if 'photos_count' is not '0'. Basically if 'user_type' is '1' and he his 'photos_count' is '0', don't return him in results. BUT, only if 'user_type' is '1', if 'user_type' is '2,3,4, etc'. they can have 0 photos and still be returned in the results.
Best I could do would check if 'photos_count' != '0' for all user_types, not just user_type 1.
$this->db->where('user_type', 1)
$this->db->where('photos_count !=', 0)
I tried searching here, but just couldn't figure it out. Any help would be appreciated.
https://ellislab.com/codeigniter/user-guide/database/active_record.html
You can use CASE in WHERE clause
SELECT
*
FROM
t
WHERE
CASE
WHEN user_type = 1
THEN photos_count > 0
ELSE TRUE
END
Fiddle Demo
While writing active record query you can do so
$result=$this->db->select('*')
->from('t')
->where('CASE
WHEN user_type = 1
THEN photos_count > 0
ELSE TRUE
END ',null,FALSE)
->get()
->result();