SQL Query - Can I compare using LEN in SELECT clause? - sql

I basically want to do this:
SELECT HasComments = CASE (LEN(Comments) > 1) WHEN 1 THEN 1 ELSE 0 END FROM TableName
In other words, return a boolean telling me whether the length of Comments is greater than 1. This gives me a syntax error.
How can I accomplish this?

SELECT HasComments = CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END
FROM TableName

A better way would be to make Comments NULLable and check for that. Indexes could then be leveraged instead of the table-scan LEN() will cause.

you're missing the when and end
SELECT HasComments = CASE WHEN (LEN(Comments) > 1) WHEN 1 THEN 1 ELSE 0 END
FROM TableName

Since you have no WHERE clause, you're most likely returning a column of data:
SELECT CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END as 'HasComments'
FROM TableName

For newer SQL versions:
SELECT CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END FROM TableName

Related

Count(*) return 1 or zero

In one of the usecase I need a query which should return 1 based on condition also if not match it should return 0
In Descpriont column if the 'SAP' count is exactly 1 then the query should return 1 else it should return 0
Note : There might be a chance that SAP could be present any number of times in Description column.
Could someone help me out here !!
Thanks.
I tried below query :
SELECT 1 from TableName where Description ='SAP' having count(*)>1
It is returning 1 but not return 0 if the count is more than 1 or no match found.
Use CASE WHEN to decide whether to show 0 or 1.
select case when count(*) = 1 then 1 else 0 end as sap_count_is_1
from mytable
where description = 'SAP';
use case when
select
case when sum(case when description='SAP' then 1 else 0 end)=1 then 1 else 0 end from table_name

How to check all records have some value in oracle query?

I have query to check some exists like:
SELECT CASE WHEN (exists (select * from "Customer" where length(CustomerID) >
0) ) then 1 else 0 end val from dual
How do I know all records of Customer table have length of the field(CustomerID) bigger than 3. If all records have value bigger than 3 then 1 or 0 .
Thanks in advance.
Joon
Something like this?
SELECT CASE WHEN MIN(LENGTH(CustomerID)) > 3 THEN 1 ELSE 0 END AS Val
FROM Customer;
Try this.
SELECT CASE
WHEN COUNT(CASE
WHEN LENGTH(CustomerID) > 3
THEN 1
END) = COUNT(*)
THEN 1
ELSE 0
END as res
FROM Customer;
Demo

Return an INT from a Case statement

I am attempting to create a row called Flag that will keep a count of when Value is above 2. Later I will need to sum flag as a count.
I currently have:
CASE
WHEN Value > 2
THEN 1
ELSE 0
END AS 'Flag',
CASE
WHEN 'Flag' = 1
THEN 1
ELSE 0
END AS 'FollowedUpCorrectly'
I receive the error:
Conversion failed when converting the varchar value 'Flag' to data
type int.
How can I force the 1 or 0 to be an INT in order to do later math?
I've looked around and I can't seem to find a way that fits.
To be able to use previously created columns in the select, you'll need to use for example outer apply, with something like this:
select
*
from table1
outer apply (
select CASE WHEN Value > 2 THEN 1 ELSE 0 END AS Flag
) X
outer apply (
select CASE WHEN X.Flag = 1 THEN 1 ELSE 0 END AS FollowedUpCorrectly
) Y
Test this in SQL Fiddle
You could use CTE or a subquery to create a flag and then do your case statement as needed in the outer query like this:
;WITH q1
AS (
SELECT
col1
,col2
,col3
,CASE
WHEN Value > 2
THEN 1
ELSE 0
END AS 'Flag'
FROM your_table --change this to match your table and column name
)
SELECT q1.col1
,q1.col2
,q1.col3
,CASE
WHEN q1.Flag = 1
THEN 1
ELSE 0
END AS 'FollowedUpCorrectly'
FROM q1;
I might misunderstand what you are after.
CASE
WHEN Value > 2
THEN 1
ELSE 0
END AS 'Flag',
CASE
WHEN 'Flag' = 1
THEN 1
ELSE 0
END AS 'FollowedUpCorrectly'
If these two lines are in the same code block, 'Flag' is unknown in the second Case Statement.
Update: As Siyual has pointed out, Flag is a string literal. Try changing the name to something that is not a reserved word.
You are comparing a string ('Flag') to an int (1). Perhaps you meant to refer to the first case that you named 'Flag'. If so, try referring to it without using the single quotes. Then the analyzer will recognize it and accept it as an int, which it is. But 'Flag' is a string. Flag is an int.

SQL: Order by statement with start value

I have a field like this:
1月~3月
12月~1月
3月~12月
4月~12月
9月~8月
6月~7月
How can i sort that column following:
4月~12月
6月~7月
9月~8月
12月~1月
1月~3月
3月~12月
It start by 4 and end by 3 (4-5-6-7-8-9-10-11-12-1-2-3)(month)
This will do it, you need to separate out the numeric portion of your field, and also use a CASE statement:
SELECT *
FROM Table1
ORDER BY CASE WHEN CAST(LEFT(Col1, CHARINDEX('月',Col1)-1)AS INT) >= 4 THEN 1 END DESC
,CAST(LEFT(Col1, CHARINDEX('月',Col1)-1)AS INT)
Demo: SQL Fiddle
SQL Server syntax above, might vary depending on database.
order by case when col < 4 then 1 else 0 end, col
or if it's really a varchar
order by case when convert(int,substring(col,1,1)) < 4 then 1 else 0 end, col

SQL query for displaying record with maximum number of non-empty fields

I am looking for a query/set of SQL queries that will give me the record ID of the record which has the "maximum number of non-empty/non-null fields".
I was looking into count() and max() functions, but they seem to be solving problems for the same column, but not for the same row (which is what I am looking for).
Please help.
You could order by the amount of non-empty fields:
select top 1 Record_ID
from YourTable
order by
case when isnull(col1,'') <> '' then 1 else 0 end +
case when isnull(col2,'') <> '' then 1 else 0 end +
case when isnull(col3,'') <> '' then 1 else 0 end +
...
case when isnull(colN,'') <> '' then 1 else 0 end
This is SQL Server syntax. If you're using another database, please amend your question.