SQL get boolean value from varchar column - sql

I am trying to get my result as either 1 or 0 based on if the column note is empty or not
column is a varchar and i want the result from the query to give me a boolean value for that column note
SELECT id, product, note FROM myTABLE
what i tired is,
SELECT id, product, CASE WHEN ISNULL(note) THEN 0 ELSE 1 FROM myTable
but i am getting an error... i am sure there is simple fix to this i am missing so please help. thank you.

Try
SELECT id, product,CASE WHEN note IS NULL THEN 0 ELSE 1 END as SomeColumn
FROM myTable
to check for blank and null as well, use NULLIF
SELECT id, product,
CASE WHEN NULLIF(note,'') IS NULL THEN 0 ELSE 1 END as SomeColumn
FROM myTable

Related

How to identify errors from casting a column?

I have a table that is all NVARCHAR. I am trying to cast a column into a date; however, there appears to be some value in the column that will not cast as date and is throwing an error halfway through.
I am trying to find the rows that are problematic. However, I don't know how to equal a result to an error..
SELECT id,
CASE WHEN CAST(date_of_birth AS DATE) = error
THEN 1
ELSE 0
FROM TABLE1
Table1
id date_of_birth
1 05/05/1934
2 feathers
3 06/06/1843
Expected results
id date_of_birth
1 0
2 1
3 0
Any advice on how to isolate the problematic rows?
Use TRY_CAST():
SELECT id,
(CASE WHEN TRY_CAST(date_of_birth AS DATE) IS NULL AND date_of_birth IS NOT NULL
THEN 1
ELSE 0
END) as is_bad_date_of_birth
FROM TABLE1;
To identify error rows, use the ISDATE function:
SELECT * FROM TABLE WHERE ISDATE(date_of_birth)=0

How to check if all rows validate a predicate

I've a table in my database for which I need to check if all rows have one field not null.
If there are no row or if there is at least 1 row with the field null => true
If there are rows and they are all with the field not null => False
Is there a way to do this in on simple query? Or I need to check if my table is empty first then if it's not check if I've a row with the field value empty ?
This will count how many NULL values you have in a field;
SELECT
SUM(CASE WHEN FieldName IS NULL THEN 1 ELSE 0 END) NullValues
FROM TableName
Will return 0 if there are no NULL values, and will return the number of NULLS if there are any present.
If you actually want to return a value as 'True' or 'False' then do this;
SELECT CASE
WHEN a.NullValues > 0
THEN 'True'
ELSE 'False'
END CheckField
FROM (
SELECT
SUM(CASE WHEN FieldName IS NULL
THEN 1
ELSE 0
END) NullValues
FROM TableName
) a
Use count(*) and count(field) and compare the two:
select
case when count(*) > 0 and count(*) = count(field) then 1 -- not empty and no nulls
else 0 end as isgood
from mytable;
Oracle SQL has no boolean data type , so I use 1 for true and 0 for false. You can replace this with whatever you like (e.g. 'true' instead of 1 and 'false' instead of 0).
As to turning this into a predicate (correlated to a main query), you'd use something along the lines of:
select ...
from main
where exists
(
select 1
from mytable
where mytable.colx = main.coly
having count(*) > 0 and count(*) = count(field)
);
You can do this with aggregation. However, it is difficult to understand what you are asking for. If you want to check that a field has no NULL values, you can do:
select (case when count(*) > 0 then 1 else 0 end) as HasNullValues
from t
where field is null;
Alternate way I found using max with putting null first:
select case when
max(field) keep (dense_rank first order by datfin desc nulls first) is null then 1
else 0 end as flag
from MYTABLE;

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 CASE SUBQUERY COUNT

I have table with 2 col :
UID NAME
-----------
111 AAA
222 BBB
Customer will enter the name and I have to retrieve UID with respective value. If name won't present in the rows, it has to retrieve 000, not like no rows.
I am trying to write query like this:
SELECT
CASE UID
WHEN Count(*) = 0 THEN '000'
ELSE UID
END
FROM table1
WHERE NAME ='XXX'
Please help me in this regard. Thanks in advance...
If UID is an integer, then you need to take casts into account:
select coalesce(cast(max(uid) as char(3)), '000')
from table1
where name = 'XXX'
The cast is intended to be to the type of UID, which seems to be char(3) in your example.
When there are no matching rows, then the max() function returns NULL. The coalesce() turns this into the value you are looking for.
try this
select case
when max(id) is null then
0
else
max(id)
end
from table1
where name = 'b'
You have error in case
SELECT case when count(UID) = 0 THEN '000' ELSE UID end FROM table1 where name = 'XXX'
sqlfiddle :http://www.sqlfiddle.com/#!2/257ea/1

SQL check if numrical value is 0 for a field where id is=333

inside an SQL SELECT CASE STATEMENT how would i check if the a value for some coulmn in sometable is equal to 0 where the id is equal to something?
select
case
when (select column_name from sometable where id = #id) = 0 then
else
end
select
case when( select column from table where id=333)=0 then "your condition"
else
end
My interpretation of your question:
SQL check if numerical value is 0 for a field (Field1) [only] when id is=333
[If id is not 333, don't need to check]
SELECT CASE WHEN isnull(id,0) <> 333 OR Field1=0 THEN 1 ELSE 0 END
,other1, other2
FROM tbl