I'm using the following where condition in a fixed sql query:
and ([NC_DATA].WORK_CENTER_BO LIKE '%[Param.4]%' OR '[Param.4]' IS NULL)
and its working fine when I enter a value for Param.4, but when I don't use Param.4 it's not giving the NULL paramter only data with values. How can I achieve that I get data with values and also data = NULL
Thanks for helping!
What I tried:
and ([NC_DATA].WORK_CENTER_BO LIKE '%[Param.4]%' OR '[Param.4]' IS NULL)
I get:
Column 1
WORK_CENTER_BO
ABC
123
DEF
456
I expecting:
Column 1
WORK_CENTER_BO
ABC
123
DEF
456
GHI
NULL
You can try using the COALESCE function as follows:
and (COALESCE([NC_DATA].WORK_CENTER_BO, '[Param.4]') LIKE '%[Param.4]%')
If your "WORK_CENTER_BO" is null, it will be replaced with [Param.4] to match the condition.
You could try like this:
AND (([NC_DATA].WORK_CENTER_BO LIKE '%'||NVL('[Param.4]',[NC_DATA].WORK_CENTER_BO)||'%') OR [NC_DATA].WORK_CENTER_BO IS NULL)
a rule of a thumb: All comparisons with NULL resolve to false.
does this query get all values including nulls,where the parameter not available in Table.
select name,display_value
from v$spparameter where nvl(name,'null') in(
'memory_target','sga_target','pga_aggregate_target','pga_aggregate_limit','db_cache_size',
'shared_pool_size','large_pool_size','result_cache_max_size','processes',
'session_cached_cursors','open_cursors','db_securefile','cpu_count',
'parallel_max_servers','job_queue_processes','log_buffer','_b_tree_bitmap_plans',
'use_large_pages','audit_trail','nls_sort','plsql_code_type','resource_manager_plan',
'shared_servers','max_shared_servers','dispatchers','event','undo_retention',
'_highthreshold_undoretention','parallel_adaptive_multi_user','parallel_force_local',
'db_files','db_performance_profile','_srvntfn_max_concurrent_jobs',
'_optimizer_use_feedback','optimizer_dsdir_usage_control',
'_sql_plan_directive_mgmt_control','_fix_control','global_txn_processes',
'_disable_autotune_gtx', '_ges_server_processes','sga_max_size',
'_securefiles_concurrency_estimate','streams_pool_size')
order by name;
You can try to add OR name is null in where caluse.
Because SQL NULL isn't a value. you need to use IS NULL to get it.
select name,display_value
from v$spparameter
where
name in('memory_target','sga_target','pga_aggregate_target','pga_aggregate_limit','db_cache_size','shared_pool_size','large_pool_size','result_cache_max_size','processes','session_cached_cursors','open_cursors','db_securefile','cpu_count','parallel_max_servers','job_queue_processes','log_buffer','_b_tree_bitmap_plans','use_large_pages','audit_trail','nls_sort','plsql_code_type','resource_manager_plan','shared_servers','max_shared_servers','dispatchers','event','undo_retention','_highthreshold_undoretention','parallel_adaptive_multi_user','parallel_force_local','db_files','db_performance_profile','_srvntfn_max_concurrent_jobs','_optimizer_use_feedback','_optimizer_dsdir_usage_control','_sql_plan_directive_mgmt_control','_fix_control','global_txn_processes','_disable_autotune_gtx', '_ges_server_processes','sga_max_size','_securefiles_concurrency_estimate','streams_pool_size')
OR
name is null
order by name;
Almost. You're converting the null value into the string 'null', but you forgot to include it in the "in" list. Just add 'null' to list.
select name,display_value
from v$spparameter where nvl(name,'null') in('null',
'memory_target','sga_target','pga_aggregate_target','pga_aggregate_limit','db_cache_size',
'shared_pool_size','large_pool_size','result_cache_max_size','processes',
'session_cached_cursors','open_cursors','db_securefile','cpu_count',
'parallel_max_servers','job_queue_processes','log_buffer','_b_tree_bitmap_plans',
'use_large_pages','audit_trail','nls_sort','plsql_code_type','resource_manager_plan',
'shared_servers','max_shared_servers','dispatchers','event','undo_retention',
'_highthreshold_undoretention','parallel_adaptive_multi_user','parallel_force_local',
'db_files','db_performance_profile','_srvntfn_max_concurrent_jobs',
'_optimizer_use_feedback','optimizer_dsdir_usage_control',
'_sql_plan_directive_mgmt_control','_fix_control','global_txn_processes',
'_disable_autotune_gtx', '_ges_server_processes','sga_max_size',
'_securefiles_concurrency_estimate','streams_pool_size')
order by name;
hello friends following is my query where displaylist is object if stringBuilder which have zero length sometimes but in that case it gives error incorrect syntax near FROM so how can i avoid this problem do i need to write a different query in if else form to avoid this or please suggest me a best solution to make and also from efficiency and performance point of view
string cmd = #"SELECT [tbl_course].course_name as Course_Name , [tbl_branch].branch_name as Branch_Name,"+displayList+#" FROM [tbl_students], [tbl_course], [tbl_branch]
WHERE [tbl_students].course_id= #courseId
AND [tbl_students].branch_id IN(" + branchId + #")
AND (#firstYrPercent is null OR[tbl_students].first_year_percent>=#firstYrPercent)
AND (#secondYrpercent is null OR[tbl_students].second_year_percent>=#secondYrPercent)
AND (#thirdYrPercent is null OR[tbl_students].third_year_percent>=#thirdYrPercent)
AND (#finalYearpercent is null OR[tbl_students].final_year_percent>=#finalYearpercent)
AND (#currentDegeePercentage is null OR[tbl_students].current_degree_percent>=#currentDegeePercentage)
AND (#passoutYear is null OR[tbl_students].passing_year>=#passoutYear)
AND (#currentBacklog is null OR[tbl_students].current_backlog<=#currentBacklog)
AND [tbl_students].gender=#sex
AND (#eGap is null OR [tbl_students].gapin_education<=#eGap)
AND (#highSchoolPercentge is null OR[tbl_students].highschool_percentage>=#highSchoolPercentge)
AND (#higherSchoolPercentage is null OR[tbl_students].ssc_percentage>=#higherSchoolPercentage)
AND (#grauationPercentage is null OR[tbl_students].graduation_percentage>=#grauationPercentage)
AND (#diplomaPercentage is null OR[tbl_students].diploma_percentage>=#diplomaPercentage)
AND (#noOfAtkt is null OR[tbl_students].number_of_ATKT<=#noOfAtkt)
AND (#validDate is null OR[tbl_students].DOB<=#validDate)
AND [tbl_students].branch_i
d=[tbl_branch].branch_id AND [tbl_students].course_id=[tbl_course].course_id";
The problem is the comma right before you insert the value of displayList. If displayList is empty, then your SQL statement looks like this:
...[tbl_branch].branch_name as Branch_Name, FROM [tbl_students]...
So either include the comma in dispalyList (displayList = ", [tbl_branch].branch_foo") or use a conditional statement, when inserting it into the SQL:
string cmd = #"SELECT [tbl_course].course_name as Course_Name , [tbl_branch].branch_name as Branch_Name" + String.IsNullOrEmpty(displayList) ? "" : ", " + displayList + #" FROM [tbl_students]...";
I want to create an view in Oracle which calculates an "utilization rate in percent".
AS SELECT
sw.SWITCH_ID,
sw.ASSET_ID,
sw.SYSTEMNAME,
sw.MAX_INSTALLABLE_PORTS,
sw.INSTALLED_PORTS,
sw.USED_PORTS,
(sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INSTALLED_PORTS,
sw.RES_INFRASTRUCTURE_PORTS,
sw.USED_INFRASTRUCTURE_PORTS,
sw.FREE_INFRASTRUCTURE_PORTS,
(sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INFRASTRUCTURE_PORTS,
sw.RESERVED_DEVICE_PORTS,
sw.USED_DEVICE_PORTS,
sw.FREE_DEVICE_PORTS,
(sw.FREE_DEVICE_PORTS/sw.RESERVED_DEVICE_PORTS)*100 AS UTIL_DEVICE_PORTS,
sw.RUN_DATE
Problem: sometimes sw.INSTALLED_PORTS or sw.MAX_INSTALLABLE_PORTS can be NULL (same for other UTIL Rows).
Is there any nice way to do something like:
if (sw.INSTALLED_PORTS or sw.MAX_INSTALLABLE_PORTS == null)
UTIL_INSTALLABLE_PORTS = null
else (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INSTALLABLE_PORTS,
or a little shorter:
sw.INSTALLED_PORTS/NULLIF(sw.MAX_INSTALLABLE_PORTS,0)
Regards,
Rob.
Divizion by NULL is not the same as divizion by zero (as you reference the problem in the title).
select 1/null from dual = null
select null/null from dual = null
So you'll automatically get what you want by (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100.
I think, the problem is when sw.MAX_INSTALLABLE_PORTS is zero. In this case you can use the following:
case
when sw.MAX_INSTALLABLE_PORTS = 0 then null
else (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100
end
CASE
WHEN sw.INSTALLED_PORTS IS NULL OR sw.MAX_INSTALLABLE_PORTS IS NULL THEN NULL
ELSE (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100
END UTIL_INFRASTRUCTURE_PORTS
You could use COALESCE() which returns the first non-null from its arguments, like :
(COALESCE(sw.INSTALLED_PORTS, 0)/COALESCE(sw.MAX_INSTALLABLE_PORTS,1))*100
AS UTIL_DEVICE_PORTS
Note that I used 1 as the coalesce default in the denominator, you don't want a DIVIDE/0 instead.