sql records validation in a table - sql

I like to check the length of a specified fields is x length and available value is a numeric value
i can do this by using two separate case statements like below
(case when len(columnA) = 10 then 0 else 1 end)+
(case when IsNumeric(columnA) = 1 then 0 else 1 end)+
(case when len(columnB) = 8 then 0 else 1 end)+
(case when IsNumeric(columnB) = 1 then 0 else 1 end)
is there any better approach as i need to this for more than 40 columns and their datatype is varchar and each of the column will have specific length.
using some short cut to reduce above two case statements into one line

If you only care that a column is invalid and are less interested in which criteria it failed on then you could just put both criteria into the one case statement as
(case when len(columnA) = 10 and IsNumeric(columnA) = 1 then 0 else 1 end)
In regards to the issue Sean raised, ISNUMERIC only confirms the value can be converted to a numeric datatype so commas and periods are valid too. you could do a check for any single character that isn't in the range of numbers
case when len(columnA) = 10 and ColumnA not like '%[^0-9]%' then 0 else 1 end
It is a little ugly because we have to say is not [not in range], so you might want to change the logic a bit.

Related

How do I set an upper bound to an INT value within a select statement in SQL?

I'm looking to make part of my query more performant by cutting down on the number of case statements I use. I have a select statement as below currently:
SELECT
ID,
CASE WHEN sum(Value1) > 0 THEN 1 ELSE 0 END as [Value1],
CASE WHEN sum(Value2) > 0 THEN 1 ELSE 0 END as [Value2],
CASE WHEN sum(Value3) > 0 THEN 1 ELSE 0 END as [Value3],
CASE WHEN sum(Value4) > 0 THEN 1 ELSE 0 END as [Value4],
....
FROM table
Essentially I want the query to simply produce a boolean, either a 0 or a 1, but the case statements have tripled the runtime for my query which is less ideal. Is there a way I can force a boolean output or set a maximum value for my select and trim away the cases?
As John Cappelletti has provided, the expected output of limiting an INT value into a Boolean 0/1 output is achieved via the SIGN function. This reduces the runtime for the query significantly compared to the case statement.

Two conditions failure in teradata case - NOT NULL AND NOT 0

Trying to return a column, giving 1, when column1 is NOT NULL and different than 0. So far managed to do this:
MAX(CASE WHEN column1 IS NOT NULL
THEN CASE WHEN column1 <> 0 THEN 1 ELSE 0 END
ELSE 0 END)
Getting this error:
SELECT Failed. 2620: The format or data contains a bad character.
It works quite ok with NOT NULL as a single condition, though.
I'm not sure why you need to nest anything, but it looks like you're missing the non-equality sign.
You might try,
CASE
WHEN column1 IS NOT NULL AND column1 <> 0
THEN 1
ELSE 0
END
Alternatively, this will produce the same result as an OR operator, where, but CASE executes the WHEN clauses in order.
CASE
WHEN column1 IS NOT NULL
THEN 1
WHEN column1 <> 0
THEN 1
ELSE 0
END
But in your description, it sounds like you wanted BOTH conditions to be true, so it doesn't make sense to nest or use multiple WHEN statements, because you can just connect them together with AND

SQL case statement error handling when there are non-integer values

I have a field of 5 digit codes, and I am trying to create a new flag field if the 5 digit code is between 2 numbers. That part is easy, but there are also a lot of values that have letters and aren't strictly 5 digits. So I'm trying to put a statement at the beginning of the case statement that says if there's an error then set the flag to zero. Or a statement that says if the value is not a number then set to zero.
Here's a sample of listed values:
36569
38206
J8502
JAA8C
Here is some code I've tried (simplified to get the point across):
case
when not isnumeric([code]) then 'N'
when [code] between 50000 and 50005 then 'Y'
ELSE 'N'
end as NewFlag
Thanks!
How about using try_cast() or try_convert():
(case when try_convert(int, code) between 50000 and 50005
then 'Y' else 'N'
end) as newFlag
Actually for your particular values, you can do the comparison as strings:
(case when code between '50000' and '50005'
then 'Y' else 'N'
end) as newFlag
This is really a special case, because you have 5 digit codes and you are only concerned about the last character.

Returning ISNULL of SUM with CASE for selecting options

I want to return the sum of the values of one column when other column (in the same row speaking) is true. But, I can´t achieve...
I am trying the next ones, but no one looks to be correct.
Can I get a pinch of help here? Thanks mates.
ISNULL(SUM(CASE WHEN dbo.TD_MODULO.FIELD1== true THEN dbo.TD_MODULO.FIELD2 ELSE 0 end),0) AS MY_CALCULATED_FIELD1
isnull(sum(if dbo.td_modulo.FIELD1 == true, dbo.td_modulo.FIELD2, 0), 0) as MY_CALCULATED_FIELD2
ISNULL(SUM(CASE dbo.TD_MODULO.FIELD1 WHEN THEN dbo.TD_MODULO.FIELD2 ELSE 0 end),0) AS MY_CALCULATED_FIELD1
Is FIELD1 a bit (boolean) field ?, then compare it to 1 or 0
select sum(case when FIELD1 = 1 then FIELD2 else 0 end) AS MY_CALCULATED_FIELD1
from dbo.TD_MODULO
dbo.TD_MODULO.FIELD1 == true this raises an eyebrow. There is no boolean values in SQL Server so you can't simply write true or false (if a literal then 'true' or 'false', if a BIT then 1 or 0), and you need just one equal sign.
The correct expression is a SUM with a CASE. Assuming that TD_MODULO.FIELD1 is a VARCHAR.
SUM(CASE WHEN dbo.TD_MODULO.FIELD1 = 'true' THEN dbo.TD_MODULO.FIELD2 END)
You can stack an ISNULL on top of it. No need to do an ELSE 0, as NULL (ELSE's default) won't be added on SUM().

Using SQL to return 6 check box flags into three columns

I am trying to figure out how to turn multiple check box results in differnet fileds into seperate columns.
The current case statement below only tracked the lowest score into a a single filed called 'Activities Registered For (1) – (5)'. I would like to convert them into 5 columns 'a-e' where 'a' is always filled with a result, and if two options are checked the results are in 'a' and 'b'. The form can be filled in with up to all selections checked. The else statement appears to be an error, since there are to be at least one of the five boxes checked.
I am new to SQL and I adopted this from someone else, so I am sorry for not showing my previous attempts to resolve my issue.
,CASE
WHEN [1524#1] = 'Y' THEN '1'
WHEN [1525#1] = 'Y' THEN '2'
WHEN [1526#1] = 'Y' THEN '3'
WHEN [1527#1] = 'Y' THEN '4'
WHEN [1528#1] = 'Y' THEN '5'
ELSE ' ' END AS 'Activities Registered For (1) – (5)'
You could use a PIVOT but multiple CASEs are just as effective, use about the same amount of code and is easier for beginners to decipher.
CASE WHEN [1524#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_1,
CASE WHEN [1525#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_2,
CASE WHEN [1526#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_3,
CASE WHEN [1527#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_4,
CASE WHEN [1528#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_5
I don't think the ELSE is necessarily an error. I often add an ELSE than shouldn't be used in case there is unexpected data (no Y in any field in your example).
Here's some info on PIVOTs if you want to check it out:
http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query