This question already has answers here:
SQL use CASE statement in WHERE IN clause
(7 answers)
Closed 5 years ago.
Im trying to figure out if is it possible to do a statement like this :
select * from telephone
where TEL_number like #tel_number
and TEL_cat in (case
when #tel_number_cat = 0
then 5
else 1 or 3
end)
Is there a way to do a select where TEL_number like #tel_number and if #tel_number_cat = 0 then TEL_cat = 5 else TEL_cat = 1 or 3
Is it possible to use CASE Statement in WHERE IN () clause but in the ELSE I need to get 2 category of numbers (1,3)
What about this:
select * from telephone
where TEL_number like #tel_number
and
(
(TEL_cat = 5 and #tel_number_cat = 0)
OR
(TEL_cat IN (1,3) and #tel_number_cat <> 0)
)
Related
This question already has answers here:
SQL Server, division returns zero
(6 answers)
Division of integers returns 0
(2 answers)
Closed 4 months ago.
I want to calculate the percentage change of some statistics id using SQLSEVER.
I want to calculate %change using = ((curr-prev)/prev) * 100
My %change expected there was 2.63% since (400008/15189461)* 100 but I am getting result as 0.00%.
select
c.poscount as curr,
p.poscount as 'prev',
(p.PosCount - c.PosCount) 'difference',
CONVERT(
decimal(2, 2),
(p.PosCount - c.PosCount)/ NULLIF(p.PosCount, 0)
) as 'Poscountchange'
from
(
select
*
from
Goker.dbo.LocalStatisticsDetail_daily
where
statisticid = '13527'
) c
inner join (
select
*
from
Goker.dbo.LocalStatisticsDetail_daily
where
statisticid = '13373'
) p on c.fieldname = p.fieldname
Why I am getting 0% as percentage change? I didn't wanted to change to decimal at first and my result was 0% before converting it into decimal.
I need a SQL query having WHERE CASE WHEN statement with a in list. I am not sure what part of my query is wrong,
First I need to make a KEY out of coldelist and IDD,IDP and then since my codelists' lenght are 5 or 6 so need to use CASE WHEN to check if their length are 5 or 6 then search in separate lists (each almost 200 or more) because my aim is to compare some tables' data using EXCEPT
My SQL statement is like:
SELECT
codelist = CASE
WHEN LEN(codelist) = 5
THEN CONCAT(CONCAT(SUBSTRING(codelist, 1, 5), IDD), IDP)
WHEN LEN(codelist) = 6
THEN CONCAT(CONCAT(SUBSTRING(codelist, 1, 6), IDD), IDP)
ELSE codelist
END,
codelist
FROM
[table1]
WHERE
(codelist = CASE
WHEN LEN(codelist) = 5
THEN (SELECT 'FEV00', 'FEV64', 'FEV97', 'FEV90')
WHEN LEN(codelist) = 6
THEN (SELECT 'FEV208', 'FEV227', 'FEV308', 'FEV326')
END)
EXCEPT
SELECT
....
This question already has answers here:
SQL SERVER: Get total days between two dates
(9 answers)
Closed 3 years ago.
I have basic query:
SELECT A.Username
,EH.[From]
,EH.[End]
,DATEDIFF(d, [From], [End]) AS HolidaysInDays
FROM EmployeHoliday EH
LEFT JOIN Admin A ON EH.UserId = A.AdminId
WHERE EH.IsActive = 1
If employee apply leave(LeaveStart = "17 Dec 2019" to LeaveEnd = "19 Dec 2019"),
when I execute my query the output is HolidaysInDays = 2 but the actual Days is 3. So, how can I resolve this problem?
My output is as shown in the below image.
When using DATEDIFF, the end date is an exclusive date so is not factored into the calculation:
PRINT DATEDIFF(d, '2019-12-17', '2019-12-19')
Output:
2
To add the final date in, just add 1 to the result:
PRINT DATEDIFF(d, '2019-12-17', '2019-12-17') + 1
PRINT DATEDIFF(d, '2019-12-17', '2019-12-18') + 1
PRINT DATEDIFF(d, '2019-12-17', '2019-12-19') + 1
Output:
1
2
3
Think of it like this:
what is 7 - 5?
Boom.
This question already has answers here:
Is there a Max function in SQL Server that takes two values like Math.Max in .NET?
(31 answers)
Closed 7 years ago.
Is it possible to return zero if a value is less than zero without using a case statement?
e.g. Max(a, 0) <-- 'Max' doesn't exist.
I would like my code to be as succinct as possible.
Just for fun:
DECLARE #a INT = -3
SELECT COALESCE(NULLIF (ABS(#a), -#a), 0)
This post just hints in the direction that a CASE expression is a much better option to get the expected result.
NULLIF is, after all, just a fancy CASE. The example query above expands (in the execution plan) to:
CASE
WHEN
CASE
WHEN abs([#a])=( -[#a])
THEN NULL
ELSE abs([#a])
END IS NOT NULL
THEN
CASE
WHEN abs([#a])=( -[#a])
THEN NULL
ELSE abs([#a])
END
ELSE (0)
END
A suitable CASE expression:
-- All versions
SELECT CASE WHEN #a > 0 THEN #a ELSE 0 END;
-- SQL Server 2012 or later
SELECT IIF(#a > 0, #a, 0);
I have a query that outputs some rows that I need to "filter".
The data I want to filter is like this:
rownum value
1 0
2 0
3 1
4 1
I need the first 2 rows, but only when they have "0" in the value-column.
The structure of the query is like this:
select count(x)
from
(
select row_number() over (partition by X order by y) as rownum, bla, bla
from [bla bla]
group by [bla bla]
) as temp
where
/* now this is where i want the magic to happen */
temp.rownum = 1 AND temp.value = 0
AND
temp.rownum = 2 AND temp.value = 0
So I want x only when row 1 and 2 have "0" in the value-column.
If either rownumber 1 or 2 have a "1" in the value-column, I dont want them.
I basically wrote the where-clause the way I wrote it here, but it's returning data sets that have "1" as value in either row 1 or 2.
How to fix this?
A value in a single row for a given column can never be both 1 and 2 at the same time.
So, first of all, either use OR (which is what I believe you intended):
WHERE (temp.rownum = 1 AND temp.value = 0)
OR (temp.rownum = 2 AND temp.value = 0)
Or simplify the query altogether:
WHERE temp.rownum <= 2 AND temp.value = 0
From here, you will get just the first two rows, and only if value = 0. If you only want rows when both rows are returned (i.e. both rows have value = 0), add
HAVING COUNT(1) = 2
to the query.