What is the Purpose of the IsNull in this query? - sql

I have a desktop application that I am converting to web and I am having trouble understanding the purpose of the IsNull parts of the query. The query is for Ms SQL and I know it has a IsNull function but this is not it. So I'm confused as to it's purpose. Below is my query:
UPDATE tb_category
SET
Email = #Email,
CandidateID = #CandidateID,
Code = #Code,
TestDate = #TestDate,
Description = #Description,
PointsEarned = #PointsEarned,
PointsAvailable = #PointsAvailable,
Average25th = #Average25th,
Average75th = #Average75th,
ImportedDate = #ImportedDate,
CreationDate = #CreationDate,
TestNum = #TestNum,
CategoryNum = #CategoryNum
WHERE ((Email = #Original_Email)
AND (CandidateID = #Original_CandidateID)
AND (Code = #Original_Code)
AND (TestDate = #Original_TestDate)
AND ((#IsNull_Description = 1 AND Description IS NULL) OR (Description = #Original_Description))
AND (PointsEarned = #Original_PointsEarned)
AND ((#IsNull_PointsAvailable = 1 AND PointsAvailable IS NULL) OR (PointsAvailable =
#Original_PointsAvailable))
AND ((#IsNull_Average25th = 1 AND Average25th IS NULL) OR (Average25th = #Original_Average25th))
AND ((#IsNull_Average75th = 1 AND Average75th IS NULL) OR (Average75th = #Original_Average75th))
AND ((#IsNull_ImportedDate = 1 AND ImportedDate IS NULL) OR (ImportedDate = #Original_ImportedDate))
AND ((#IsNull_CreationDate = 1 AND CreationDate IS NULL) OR (CreationDate = #Original_CreationDate))
AND (TestNum = #Original_TestNum)
AND (CategoryNum = #Original_CategoryNum));
I tried simplifying the update statement by removing the IsNull sections but that did not work.

In SQL null is not equal (=) to anything—not even to another null, so in your query in case if both values are null (old and new one) you need to take that into account and check values with IS NULL.

I'm seeing this pattern repeated several times in the WHERE clause:
#IsNull_Description = 1 AND Description IS NULL
It means that a variable, #IsNull_SomeColumnName, which is presumably set earlier in the code, has a value of 1, and the column that the variable relates to is currently NULL.
The function IsNull(Param1, Param2) is used to substitute the value of the second parameter for the value of the first parameter if the first parameter IS NULL, and the function returns the value of Param2.
In SQL Server, and quite a few other RDBMSs, the IS NULL syntax is used to check if a value is currently NULL. Here, Description IS NULL will return TRUE if, well, Description is null, and FALSE if it is not.

Related

Data Type mismatch in criteria expression when Table Adapter Update

I have already been searched solution and can't get the right solution yet.
ObjDRow = DataDataSet.Client.Rows.Find(strClientNo)
With ObjDRow
.ClientName = txtClientName.Text.Trim
.ClientAddr = txtAddr.Text.Trim
If txtRegOffice.Text = "" Then
.ClientRegOfficeAddr = txtAddr.Text.Trim
Else
.ClientRegOfficeAddr = txtRegOffice.Text.Trim
End If
.MailtoCorresAddr = RBtnCorresAddr.Checked
.MailtoRegOffice = RBtnRegOffice.Checked
.ClientHPhone = mskHandPhone.Text.Trim
.ClientPager = mskPagerNo.Text.Trim
.ClientTel = mskTelephone.Text.Trim
.ClientFaxNo = mskFax.Text.Trim
.ClientEmail = txtEmail.Text.Trim
.PrimaryPartner = txtPriPartner.Text.Trim
.SecondPartner = txtSecPartner.Text.Trim
.BroughtInBy = cboPreferredBy.Text.Trim
.PersonIncharge = cboPersonIncharge.Text.Trim
.GLAC = cboGLAcode.Text.Trim
.ContactPerson = txtContactPerson.Text.Trim
.AcraNo = txtAcraNo.Text.Trim
.Active = chkActive.Checked
If dtpfyear.Checked = True Then
.YearEnd = dtpfyear.Text
End If
.DeptNo = cboDeptNo.Text.Trim
.DateJoined = dtDateJoined.Value
If cboClientName.SelectedIndex = -1 Then
.Group = txtClientNo.Text
Else
.Group = cboClientName.SelectedValue
End If
.GroupStatus = RButtonMainYes.Checked
.MainGroup = RButtonSubYes.Checked
If IsDate(dtIncorporationDate.Text) Then
.DateOfIncorporation = dtIncorporationDate.Text
Else
.SetDateOfIncorporationNull()
End If
End With
ObjDRow.EndEdit()
ClientTableAdapter.Update(DataDataSet.Client)
Error is occurs when ClientTableAdapter Update.
This error occurs for some client only.
I already check datatype of database and table adapter's datatype and all datatype are same.
My input value datatype and table adapter's datatype are same.
This error occurs even I command all line of update code (.ClientName to last line) but this error still occurs.WTF
Most answers say this is a single quote problem but In my case, There is no single quote.
All data types are same and input values are the same with datatype.
** Updated**
This error still occurs even I do like=>
ObjDRow = DataDataSet.Client.Rows.Find(strClientNo)
ObjDRow.EndEdit()
ClientTableAdapter.Update(DataDataSet.Client)
There is nothing change just select and update.
But if I remove ObjDRow.EndEdit().All are fine. There is no error.

Using IF Statement inside a Where Clause in SQL Server

I have two params that are passed in a function which passed down to the SQL string. Variables can be null or has a value (int). If x is not null, use "this" column else use "that" column. I'm using SQL Server.
// Inside a function with two variables passed, x and y
$sql = "
SELECT
[...]
FROM
[...]
WHERE
[...]
AND [...]
-- This is the tricky part
AND
--- if $x is not null, use foo column else use bar column
IF (x, R.column = 2, R.another_column = 3)
[...]
";
Is this possible to select a column based on the value of the variable passed in?
AND
(
($x is not null and R.column = 2) OR
($x is null and R.another_column = 3)
)
Unless I'm misunderstanding, you just need logic, or a case statement would work.
$sql = "
SELECT
[...]
FROM
[...]
WHERE
[...]
AND
[...]
-- This is the tricky part
AND
--- if $x is not null, use foo column else use bar column
-- IF (x, R.column = 2, R.another_column = 3)
(
(X IS NULL AND R.column = 2)
OR
(X IS NOT NULL AND R.another_column = 3)
)
";
AND
--- if $x is not null, use foo column else use bar column
case
when x is NULL then R.another_column = 3
else R.column = 2
end
--in SQL Server...
DECLARE #x INT = 3;
SELECT *
FROM sys.schemas
WHERE (#x IS NULL AND schema_id = 2)
OR (#x IS NOT NULL and schema_id = 3)
--optional depending on how much you execute this query, it may help
OPTION(RECOMPILE)

SQL Server Case in Where clause

I am trying to create a stored proc and have a where clause where to different operations can take place depending on the value of a parameter passed in:
WHERE
(cdr.CircuitReference = #CircuitReference)
AND
CASE WHEN (#JDEDocumentReference <> 'Unbilled Calls')
THEN
sct.JDEDocumentReference = #JDEDocumentReference
ELSE
((sct.JDEDocumentReference IS NULL) AND (sc.StartDate IS NOT null AND ((sc.CloseDate IS null) OR (datediff(day,sc.CloseDate,getdate()) < 0)) AND stp.SipTrunksProduct = sct.ProductCode))
END
I've just posted my where clause above but when i try to execute the script i get the following error:
Incorrect syntax near '='.
Is this the correct way to do a conditional statement in a where clause of an sql query?
Thanks
This problem could be solved without a CASE statement by using the following:
WHERE
(cdr.CircuitReference = #CircuitReference)
AND
((#JDEDocumentReference <> 'Unbilled Calls' AND sct.JDEDocumentReference = #JDEDocumentReference)
OR
(#JDEDocumentReference = 'Unbilled Calls' AND ((sct.JDEDocumentReference IS NULL) AND (sc.StartDate IS NOT null AND ((sc.CloseDate IS null) OR (datediff(day,sc.CloseDate,getdate()) < 0)) AND stp.SipTrunksProduct = sct.ProductCode))))
STATEMENT FULLY WRONG : There is no need for case here(Even there is a possibility to it correctly. But here no needed).
USE:
(cdr.CircuitReference = #CircuitReference)
AND ((JDEDocumentReference <> 'Unbilled Calls'
AND #JDEDocumentReference) OR #JDEDocumentReference = 'Unbilled Calls' )
OR (JDEDocumentReference = 'Unbilled Calls'
AND ((sct.JDEDocumentReference IS NULL) AND (sc.StartDate IS NOT null AND ((sc.CloseDate IS null)
OR (datediff(day,sc.CloseDate,getdate()) < 0)) AND stp.SipTrunksProduct = sct.ProductCode)))
You can use something like this,
WHERE
(cdr.CircuitReference = #CircuitReference)
AND sct.JDEDocumentReference = case when #JDEDocumentReference <> 'Unbilled Calls' Then #JDEDocumentReference end

Entity Framework query with "not in"

I have a simple (well easy, not simple) query of "not in" on related tables.
SELECT CompetencyID, CompetencyName FROM Competency
WHERE (Deleted = 0) AND (CompanyID = 1) AND (CompetencyID NOT IN(SELECT CompetencyID
FROM CompetencyGroups WHERE (Deleted = 0) AND (CompanyID = 1) AND (GroupID = 1))) AND
(ParentID = 0) ORDER BY CompetencyName
In SQL I get the list that I need with remaining items not in the group. Now I want to bind this to a DataGrid using EF5.
I cannot get the query syntax properly (Using VB.net) to list the ID and the Name of the Competency...
Converted the provided c# answer to VB:
Dim excludeList = context.CompetencyGroups.Where(Function(x) x.Deleted = False And x.GroupID = GroupID).Select(Function(x) x.CompetencyID).ToArray
Dim results = context.Competencies.Where(Function(c) Not excludeList.Contains(c.CompetencyID) And c.Deleted = False And c.CompanyID = 1 And c.ParentID = 0).OrderBy(Function(c) c.CompetencyName)
GridView2.DataSource = results
GridView2.DataBind()
Hope this helps someone in the future. Took me about 4 hours to search, ask and convert...
Something like
var excludeList = context.CompetencyGroups.Where(x => x....).Select(x => x.CompetencyID).ToArray();
var results = context.Competency.Where(x => !excludeList.Contains(x.CompetencyID));
Update: Somebody else edited this and then someone rejected it, but the edit was a good one (to select the value)
If you need the Cast to make an array of Integers.
Dim excludeList = context.CompetencyGroups.Cast(Of CompentencyGroup).Select(Function(x) x.CompetencyID).ToArray()
Dim results = context.Competency.Where(Function(x) Not excludeList.Contains(x.CompetencyID))

IF ELSE CONDITION IN SQL WHERE CLAUSE

MY QUERY IS
DECLARE #AutoApprove BIT
SET #AutoApprove = (
SELECT AutoApprove
FROM dbo.CommentBox_Setting
WHERE UserModuleID = #myModuleID
AND PortalID = #portalID
AND CultureCode = #cultureCode
)
From this i will get whether 1 OR 0 (TRUE OR FALSE) furthermore i have
SELECT * FROM ComentBox_Comment
WHERE UpperModuleID = #UpperModuleID
AND ModuleID = #myModuleID
AND portalID = #portalID
AND cultureCode = #cultureCode
AND //Here i need to check condition
(IF(#AutoApprove=0){ THEN isapprove=1}else {do not check})
Note here isapprove is table filedName
I know ,i can do this with long query i need short and easy way.
Help me out.
Try something like
AND CASE WHEN #AutoApprove=0 THEN isapprove ELSE 1 END = 1
This will check isapprove = 1 if #AutoApprove = 0, or 1=1(ignore) otherwise.
CASE (Transact-SQL)