I am trying to count the number of records in my table that make a function I created return 1. Here is what I have:
CREATE FUNCTION isErrorMismatch
(@theType nvarchar(1), @theExCode nvarchar(2))
RETURNS bit
AS
BEGIN
DECLARE @theTypeAsInt int
SET @theTypeAsInt = CAST(@theExCode AS INT)
DECLARE @returnValue bit
SET @returnValue = 0
IF @theType = 'A'
IF @theTypeAsInt >= 10 AND @theTypeAsInt <= 17
SET @returnValue = 0
ELSE
SET @returnValue = 1
ELSE IF @theType = 'B'
IF @theTypeAsInt >= 18 AND @theTypeAsInt <= 26
SET @returnValue = 0
ELSE
SET @returnValue = 1
ELSE IF @theType = 'C'
IF @theTypeAsInt >= 30 AND @theTypeAsInt <= 38
SET @returnValue = 0
ELSE
SET @returnValue = 1
ELSE
SET @returnValue = 1
RETURN @returnValue
END
GO
SELECT (SELECT COUNT(*)
FROM isErrorMismatch(LEFT(Type, 1),LEFT([Exception Code/Category],2))
As MismatchCount
FROM dbo.[All Service Ticket Data 2012_final]
Every record that makes the function return 1, I want to count. I am getting syntax errors in my FROM when I call the function. Anyone have any ideas? Thank you!
***UPDATE:
In order to get the count that make the function return 1:
SELECT COUNT(dbo.isErrorMismatch(LEFT(Type, 1), LEFT([Exception Code/Category],2))) As MismatchCount
FROM dbo.[All Service Ticket Data 2012_final]
WHERE dbo.isErrorMismatch(LEFT(Type, 1), LEFT([Exception Code/Category],2)) = 1
In order to get all of the records that make the function return 1:
SELECT Type, [Exception Code/Category],
dbo.isErrorMismatch(LEFT(Type, 1),LEFT([Exception Code/Category] ,2)) as Mismatch
FROM dbo.[All Service Ticket Data 2012_final]
WHERE dbo.isErrorMismatch(LEFT(Type, 1),LEFT([Exception Code/Category] ,2)) = 1