sql query and asp.net - sql

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

Related

SQL checking equality inside a case statement inside where clause?

Here is the code:
WHERE 1=1
AND TU.Auction_I IN (41, 42)
AND #StatePickupID = CASE
WHEN #StatePickupID IS NOT NULL
THEN (UP.TransportStateID = #StatePickupID)
END
AND #StateDropoffID = CASE
WHEN #StateDropoffID IS NOT NULL
THEN (UD.TransportStateID = #StateDropoffID)
END
So I only want to return records where UP.TransportStateID is equal to StatePickupID if it is not null, same thing for DropoffID. But I get a syntax error message where the equals sign is and I cannot find any other way to check for equality online. Any help or advice would be much appreciated.
I only want to return records where UP.TransportStateID is equal to StatePickupID if it is not null
This would translate as the following predicate:
#StatePickupID IS NULL OR #StatePickupID = UP.TransportStateID
When the parameter is not null, the predicate filters on TransportStateID; when the parameter is null, the filter is a no-op.
In your where clause, for both parameters :
WHERE 1 = 1
AND TU.Auction_I IN (41, 42)
AND ( #StatePickupID IS NULL OR #StatePickupID = UP.TransportStateID )
AND ( #StateDropoffID IS NULL OR #StateDropoffID = UD.TransportStateID )

Casting a text to varchar

I'm attempting to cast a text to a varchar due to a previous issue I had regarding this error message:
The data types text and varchar are incompatible in the equal to operator RSS
With a quick search, I found that I need to cast the value which is text to that of a varchar. I have done so:
#Query("SELECT e FROM DeStudent e " +
"WHERE CAST(e.studentExpression as varchar) = :studentExpression" +
"AND e.deactivationTime = '9999-12-31 00:00:00.000' " +
"AND (:studentName is null OR e.name = :studentName)" +
"AND (:studentDescription is null OR e.description = :studentDescription)" +
"AND (:studentExpression is null OR e.studentExpression = :studentExpression)"
)
However I seem to be getting this error:
2020-07-08 14:42:39.947 ERROR o.h.hql.internal.ast.ErrorCounter: line 1:142: unexpected token: e
And it's not allowing me to run the application due to this modification. I'm unsure what I have done, any help? Thank you. I've tried different variations as well, such as moving the cast to the select section but it's still the same error.
The error message is telling you that the issue is with the letter 'e' not being understood. Your select clause is where the issue is. You wrote SELECT e FROM DeStudent e, which aliased the DeStudent table as 'e', but the select clause should specify columns, not just 'e'. To select everything, use *.
SELECT * FROM DeStudent e

sql statement with an if inside of it?

I've got the following sql query which works. However I need help with the fields that have a /10000 calculation. Occasionally there will be no data in the field to be calculated, so my query below is returning a 0. How do I check for data > 0 before I perform the /10000 calculation?
SELECT
timesheet.customerID,
customer.customer,
timesheet.projectID,
project.project_title,
timesheet.chargeID,
charge.charge_description,
timesheet.notes,
timesheet.mon/10000,
timesheet.tues/10000,
timesheet.wed/10000,
timesheet.thurs/10000,
timesheet.fri/10000,
timesheet.sat/10000,
timesheet.sun/10000,
timesheet.lineID
FROM
timesheet_line timesheet
LEFT JOIN customer ON timesheet.customerID = customer.customerID
LEFT JOIN project ON timesheet.projectID = project.projectID
LEFT JOIN charge_rate charge ON timesheet.chargeID = charge.chargeID
WHERE
timesheet.timesheetID = '" & tTimesheetID & "' --VBA variable added here
ORDER BY
timesheet.lineID
You could do it like this (example column)
CASE timesheet.wed > 0 THEN timesheet.wed/10000 ELSE null END as wed,
if the value can be null then you have to do this:
CASE COALESCE(timesheet.wed,0) > 0 THEN timesheet.wed/10000 ELSE null END as wed,
You have a few options:
The COALESCE function is perfect when you want to translate possible NULL values to something else:
COALESCE(NullableColumn / 10000, DefaultValueIfNull)
-- btw., NULL divided by 10,000 should yield NULL, not 0.
Some SQL dialects (such as SQL Server's T-SQL) have a very similar function called ISNULL.
CASE is closer to an if:
CASE WHEN NullableColumn IS NOT NULL THEN NullableColumn / 1000 ELSE DefaultValue END

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"

please correct me with the sql query

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