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

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ська%'

Related

CASE WHEN SQL Syntax Error

I am attempting to us the CASE statement for the first time and I cannot understand why I am getting a syntax error on the last WHEN and ELSE. I am trying to extract a substring if a value starts with a specific set of characters. My Code is below:
SELECT
CASE
WHEN LEFT ([RCode],2) = 'BB' THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT ([RCode],4) = 'APT-' THEN SUBSTRING([RCode],5,LEN([RCode])
WHEN LEFT ([RCode],4) = 'PS-' THEN SUBSTRING([RCode],4,LEN([RCode])
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]
Any guidance would be appreciated.
This won't (necessarily) fix your syntax problem, but I would recommend that you use like for the comparisons:
SELECT (CASE WHEN RCode LIKE 'BB%' THEN SUBSTRING([RCode], 3, LEN([RCode]))
WHEN RCode LIKE 'APT-%' THEN SUBSTRING([RCode], 5, LEN([RCode]))
WHEN RCode LIKE 'PS-%' THEN SUBSTRING([RCode], 4, LEN([RCode]))
ELSE [RCode]
END)
FROM [Xperdyte].[dbo].[tJCLines];
Then you don't have to count characters -- and the third condition will match.
Missed off two right parenthesis.
SELECT CASE WHEN LEFT([RCode],2) = 'BB'
THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT([RCode],4) = 'APT-'
THEN SUBSTRING([RCode],5,LEN([RCode]))
WHEN LEFT([RCode],4) = 'PS-'
THEN SUBSTRING([RCode],4,LEN([RCode]))
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]

SQL Server LEFT binary equals wrong value

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

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

Dynamic appending constraints in SQL query

I have this SQL
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
and <<either bdvalue or string value based on user selected value datatype>>
Here in the Query based on the devudp1.valueType I want to append below attribute
If the valueType is 3 then I want to append my above select clause with devudp1.bdvalue ='10', else it should be appended by devudp1.bdvalue = 'Hello'
So the above query when valueType is 3 will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.bdvalue = '10'
else it will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.stringValue = 'Hello'
Can anyone suggest me how to put this logic in place
Try this:
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND (
(<USER-SELECTED-VALUE> = 3 AND devudp1.bdvalue ='10') OR
(<USER-SELECTED-VALUE> <> 3 AND devudp1.stringvalue ='Hello')
)

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.