please correct me with the sql query - sql

Whats wrong wrong with this query please correct me on this syntactcally it's going wrong please correct me on this how to concatenate this
string cmd = "SELECT *
FROM [tbl_students]
WHERE course_id=#courseId
AND branch_id IN ("+branchId+")
AND (#passoutYear is null or passout_year>=#passoutYear)
AND (#currentBacklog is null or current_backlog<=#currentBacklog)
AND gender=#sex
AND (#eGap is null or gapin_education<=#egap)
AND (#firstYrPercent is null or first_yea_percent>=#firstYrPercent
AND (#secondYrpercent is null or second_year_percent>=#secondYrPercent)
AND (#thirdYrPercent is null or third_year_percent>=#thirdYrPercent)
AND (#finalYrpercent is null or final_year_percent>=#finalYrpercent)
AND (#currentDegreePercentage is null current_degree_percent>=#currentDegreePercentage)
AND (#highSchoolPercentage is null high_school_percentage>=#highSchoolPercentage)
AND (#higherSchoolPercentage is null higher_school_percentage>=#higherSchoolPercentage)
AND (#graduationPercent is null graduation_percent>=#graduationPercentage)
AND (#diplomaPercentage is null diploma_percentage>=#diplomaPercenage)
AND (#noOfAtkt is null or no_of_atkt<=#noOfAtkt)
AND (#date is null or date_of_birth>=#date)";

First, if branchId is a string, then you'll need these single quotes:
branch_id IN('"+branchId+"') AND
though I don't understand why it's not parameterized like so:
branch_id = #branchId AND
Second, you may have misspelled the word 'year' in this field name:
AND (#firstYrPercent is null or first_year_percent>=#firstYrPercent
Finally, you're missing a few OR's here:
AND (#currentDegreePercentage is null OR current_degree_percent>=#currentDegreePercentage)
AND (#highSchoolPercentage is null OR high_school_percentage>=#highSchoolPercentage)
AND (#higherSchoolPercentage is null OR higher_school_percentage>=#higherSchoolPercentage)
AND (#graduationPercent is null OR graduation_percent>=#graduationPercentage)
AND (#diplomaPercentage is null OR diploma_percentage>=#diplomaPercenage)

I think I got it.
Are you getting an error while trying to COMPILE the code, as opposed to running the SQL query?
I'm going to bet that you're using C#, and that you should do multi-line strings with an #-symbol before the "-symbol. Such as this:
string blabla = #"
hello
there
";
Then of course you need it also when you open up the string after branchId

Related

Fixed SQL Query if paramerter empty also incluce NULL

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.

query to find all db parameter values including null

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;

Clojure: How do I delete an entry from a table when the rows are null? (in other words, containing no value)

(sql/delete-rows "tableName" ["scenarioname = ? AND scenarioid= ?" nil nil]))
For some reason, this doesn't work... Why?
what you want is
(sql/delete-rows :tablename ["scenarioName is null and scenarioid is null"])
or
(sql/delete! dbcon :tableName ["scenarioName is null and scenarioid is null"])
assuming dbcon as your db connection.
Notes
delete-rows is deprecated ->
http://clojure.github.io/java.jdbc/#clojure.java.jdbc.deprecated/delete-rows
use delete!
null != null in SQL ->
Why does NULL = NULL evaluate to false in SQL server
clojure.java.jdbc where clause does not support operators like "is"
"in" and "between"

sql query and asp.net

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]...";

Oracle View problem with Select and division by zero

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.