SQL Server LEFT binary equals wrong value - sql

I'm trying to understand what is going on with the following LEFT equality check.
select
CASE
when LEFT(0x000012CA,4) = cast(4842 as varbinary(4)) then 1
ELSE 0
END
Result: 1
Expected: 0
This should come out as 0 but will return 1. If I change 0x000012CA to any other value (but 0x000012EA which is 4842) the result correctly resolves to 0.
It appears that I can do
cast(LEFT(0x000012CA,4) as varbinary(4))
And it resolves properly, but that doesn't explain why say switching 4842 for 4841 works or 0x000012CA to 0x000012BA

Try an excessively larger number to see if it works.
select
CASE
when LEFT(0x000012CA,4) = cast(4842 as varbinary(400)) then 1
ELSE 0
END

Related

SQL using where like "%ads%" + CASE WHEN

My query is
SELECT Abonent FROM phone
WHERE `Street` LIKE "%примiська%" AND WHERE
CASE WHEN `House` % 2 = 0 THEN 'Правий'
ELSE 'Лiвий'
END AS Side
And I get
How can I do that case ?
The second where is definitely incorrect; it seems you want case in select: you filter Abonent being on required Street(s) and add a computed field to select showing on which side of a street (left or right) each abonent is.
select Abonent,
case
when `House` % 2 = 0 then 'Правий'
else 'Лiвий'
end as Side
from phone
where `Street` like '%примiська%'

What does this SQL statement produce?

I am having trouble finding documentation about this specific statement
SELECT IF (count(f.id)=0,1,0) as flgNew
FROM table f ON ...
WHERE ...
the table is joined with other tables and should return 1 if the entry in f is found or 0 if it has not been found.
So what does IF (count(f.id)=0,1,0) do?
It's basically the same as the following CASE statement:
SELECT CASE WHEN count(f.id) = 0
THEN 1
ELSE 0
END AS flgNew
...
It checks to see if the expression count(f.id) = 0 is true, and returns the value 1 if it is, and 0 if it is not.
You can read more on the IF() function in the official docs here:
https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#function_if

CASE in WHERE clause returns Error

I'm running this code:
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%' AND
CASE src
WHEN 0 THEN
uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
END
I get this error:
Incorrect syntax near the keyword 'IN'
I have no idea what's wrong.
A CASE statement is not appropriate in this case. Just use a simple OR condition:
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%'
AND (
src <> 0 -- add a COALESCE here if src can be NULL
OR uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
)
... which basically is the equivalent of only applying the uID filtering if src = 0, which is what you appeared to be trying to accomplish with your query.
It sounds like you don't have to use CASE please test this
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%'
AND ( (src = 0 --scr is 0 => must have uid in the list
AND UID IN (29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481))
OR src <> 0) --else src is not 0 and there is no additional condition
i have a doubt on your not like condition have you tested it alone?
Guess this is what you are looking for
WHERE aIDs NOT LIKE '%''101''%' or
(
src = 0
AND
uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481
)
It looks like your statement is not formatted properly. you placed a condition rather than a value to set within "When 0 then ...... END
the uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481) you have there is a condition and shouldn't have been there
The when part of a case statement should select a single value. what you are trying to do is to check for a condition.
Or if you are checking for uid in those values, you should do
case when src = 0 then
case when uID IN (29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
then 'It is a Client ID'
-- add another when or else part here if required
end
else 'Not a UID and not a Client ID'
end

SQL DB2 - conditional logic in WHERE clause

I need to pull back records based on a location ID, but I've got two fields that MAY contain the incoming criteria....
something like this
SELECT * FROM tbl
WHERE myVar = locationID
IF LocationID = 0
myVar = location2ID
this is a 'logical' example...
I think I can do
WHERE myVar = CASE WHEN locationID = 0 THEN location2ID ELSE locationID END
although I've read that CASE in a WHERE clause should be avoided...? why? or is this OK?
- either way it FAILS
WHERE CASE WHEN locationID=0 THEN location2ID=myVAr ELSE locationID=myVar END
also FAILS
thx
Sorry for the confusion lads - didn't mean to be "ambiguous" - looks like #2 will do what I want - but for the clarification requested here is the issue...
the table stores TWO locations, let's call then CURRENT_LOC and ORIGINAL_LOC... in most cases these will BOTH have data, but in some cases the 'thing' hasn't moved... so the CURRENT_LOC is '0'. MY issue is I'll be passing in a LOCATION ID, I want the records where CURRENT_LOC matches OR if the the CURRENT_LOC=0 then I ALSO want the ORIGINAL_LOC... where that matches...
does that help the discussion? I hope.
WHERE myVar = COALESCE(NULLIF(locationID, 0), location2ID)
Alternatively,
WHERE (
(locationID <> 0 AND myVar = locationID)
OR
(locationID = 0 AND myVar = location2ID)
)

Modify Return Value of SELECT-Statement (TSQL) [Optimizing query]

Problem:
A Database collumn has a Tristate (0,1,2).
Each of the values are used serversidely.
The Clientcode (which cant be changed anymore) is only able to understand '0,1'.
In the Clients view '1' is identic with '2'. So I want to change the SQL Query in the Database to return '1', if the specific value is > 0.
My current Solution is combining 2 Selects (using UNION SELECT) with different WHERE-Clauses and returning '1' or '0' as static values. Now I'm looking for a solution to 'translate' the value within only ONE SELECT statement.
This is my current Solution:
SELECT
dbo.Nachricht.NachrichtID, dbo.Nachricht.Bezeichnung, '1' AS BetrifftKontoeinrichtung,
FROM dbo.Nachricht INNER JOIN dbo.AdditionalData
ON dbo.Nachricht.NachrichtID = dbo.AdditionalData.NachrichtID
WHERE (dbo.Nachricht.NachrichtID in ( 450,439 ))
AND dbo.AdditionalData.BetrifftKontoeinrichtung > 0
UNION SELECT
dbo.Nachricht.NachrichtID, dbo.Nachricht.Bezeichnung, '0' AS BetrifftKontoeinrichtung,
FROM dbo.Nachricht INNER JOIN dbo.AdditionalData
ON dbo.Nachricht.NachrichtID = dbo.AdditionalData.NachrichtID
WHERE (dbo.Nachricht.NachrichtID in ( 450,439 ))
AND dbo.AdditionalData.BetrifftKontoeinrichtung = 0
You can use a case statement, like this:
SELECT
dbo.Nachricht.NachrichtID, dbo.Nachricht.Bezeichnung,
CASE WHEN dbo.AdditionalData.BetrifftKontoeinrichtung = 0
THEN '0' ELSE '1'
END AS BetrifftKontoeinrichtung,
FROM dbo.Nachricht
INNER JOIN dbo.AdditionalData
ON dbo.Nachricht.NachrichtID = dbo.AdditionalData.NachrichtID
WHERE (dbo.Nachricht.NachrichtID in ( 450,439 ))
Looks like you need to use CASE. A decent tutorial here
http://www.databasejournal.com/features/mssql/article.php/3288921/T-SQL-Programming-Part-5---Using-the-CASE-Function.htm
See the worked example
If you just CAST(CAST(val AS BIT) AS INT) you will get integer 0 for 0 and integer 1 for everything else.