Why is this throwing an error on the equal sign?
select IIF((SUBSTRING('A1234', 1, 1) = 'A'), TRUE, FALSE) as IsAustraliaUser
Error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '='.
IIF is a SQL Server 2012 feature, you'll need to use CASE
SELECT CASE SUBSTRING('A1234', 1, 1)
WHEN 'A' THEN 'TRUE'
ELSE 'FALSE'
END
You should replace IIF with CASE, also TRUE and FALSE don't exists in SQL Server, you can use VARCHAR or BIT
select CASE WHEN SUBSTRING('A1234', 1, 1) = 'A' THEN 'TRUE' ELSE 'FALSE' END as IsAustraliaUser
I don't think SQL supports this syntax of IIF (unless you're using 2012), replace with case.
SELECT CASE WHEN ( SUBSTRING('A1234', 1, 1) = 'A' ) THEN 'TRUE'
ELSE 'FALSE'
END AS IsAustraliaUser
Related
I am trying to make the TODATETIMEOFFSET work inside a case statement. when I try to do this sql returns me following error. works fine if it's not inside a case statement. what am I doing wrong?
SELECT AP.POR,
AP.POD Path,
TODATETIMEOFFSET(AP.StartTime, '-06:00') as StartTime,
MinimumPrice = (
CASE WHEN ( CHARINDEX('','' + '''+#Provider+''' + '','', '','' + '''+#UserCompanyList+''' + '','') > 0 )
THEN TODATETIMEOFFSET(AP.MinimumPrice, '-06:00')
ELSE ( (
CASE WHEN (getdate() < C.ClearingTime and C.OpenPriceMask = 0)
THEN NULL
WHEN (getdate() > C.ClearingTime and C.ClearedPriceMask = 0)
THEN NULL
ELSE AP.MinimumPrice
END
) )
END
),
AP.ClearingPrice,
AP.PriceUnits
FROM TES_Auction C
INNER JOIN TES_AuctionPrice AP ON AP.AuctionID = C.ID
Msg 206, Level 16, State 2, Line 1
Operand type clash: decimal is incompatible with datetime2
also how can I make 'as' syntax work inside a case statement?
It looks like you have an error on this part of your query:
THEN TODATETIMEOFFSET(AP.MinimumPrice, '-06:00')
AP.MinimumPrice I would expect a decimal type, and SQL can't convert a decimal to datetime2.
Also, to use a column alias on a case statement, you would put it at the very end just before the next comma, for example:
ELSE AP.MinimumPrice END))END) AS myColumnName, -- etc.
I need to select rows in a subquery with an IF condition:
DECLARE #VersionNo varchar(30)
SET #VersionNo = 'Hello'
SELECT (
CASE #VersionNo WHEN 'Hello'
THEN (SELECT '22','22')
ELSE (SELECT '25','22')
END)
But I am getting this error:
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'IF'.
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'Then'.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'Else'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
Can anyone help me to resolve this?
Based on the answers, I have changed my query to:
DECLARE #VersionNo varchar(30)
SET #VersionNo = 'Hello'
SELECT (
CASE #VersionNo WHEN 'Hello'
THEN (SELECT '22','22')
ELSE (SELECT '25','22')
END)
Now I am getting this error:
Msg 116, Level 16, State 1, Line 4
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Use CASE WHEN:
declare #VersionNo varchar(30);
Set #VersionNo = 'Hello';
Select CASE WHEN #VersionNo='Hello' Then 'Yes' Else 'No' END AS SomeCol;
You need to use CASE, you cannot use IF in a select query
select case #VersionNo when 'Hello'
then 'Yes'
else 'No'
end
Alternatively, you can do this to run different select queries based on the value of your variable:
IF #VersionNo = 'Hello'
SELECT 'Yes'
ELSE
SELECT 'No'
Check this one :
DECLARE #VersionNo VARCHAR(30)
SET #VersionNo = 'Hello'
SELECT CASE #VersionNo
WHEN 'Hello' THEN '22'
ELSE '25'
END ,
CASE #VersionNo
WHEN 'Hello' THEN '22'
ELSE '22'
END
I'm developing against MS SQL Server 2008.
I'm working with a SQL query written by another developer with a case statement that looks like this:
CASE
WHEN substring(A,4,3) is null THEN '????'
WHEN substring(A,4,3) in ('000','010','018') THEN '100L'
WHEN substring(A,4,3) = '123' AND substring(A,8,3) = 'abc' THEN 'FOO'
END
My thought here is that it is redundant to use substring(A,4,3) and there ought to be some way to avoid it.
How can I rewrite this clause calling substring(A,4,3) only once?
You could do something like this:
select case
when substr1 is null
then '????'
when substr1 in ('000', '010', '018')
then '100L'
when substr1 = '123'
and substring(A, 8, 3) = 'abc'
then 'xxx'
end
from (
select substring(A, 4, 3) as substr1 from MyTable
) a
I am trying to set 2 declared variable with case when blocks
Here's my code:
DECLARE #like bit,#dislike bit
if exists
( select *
,#like = (CASE WHEN likeordislike = 1 THEN 'true' ELSE 'false' END)
,#dislike=(CASE WHEN likeordislike = 0 THEN 'true' ELSE 'false' END)
from likeordislike
)
But when I execute query throws errors:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '='.
Everything is ok? Couldn't understand
if (select count(*) from likeordislike where user = #user and comment_id = #comment_id) = 0
begin
insert into likeordislike etc
end
else
update likeordislike etc
end
i am getting the following error:
Incorrect syntax near 'cast', expected 'AS'.
on this line:
use SalesDWH
go
if (select isnumeric(result))=1
begin
select max(cast(result) as decimal(10,2)) from testresults
end
testresults table contains about 21 million rows of data.
what am i doing wrong? thanks so much.
thank you everyone for you rhelp.
i've changed the code to:
use SalesDWH
go
if (select isnumeric(result))=1
begin
select max(cast(result as decimal(10,2))) from testresults
end
and now i am getting error:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'result'.
it's definitely a valid column
i took kris' suggestion and did this:
use SalesDWH
go
SELECT MAX( CAST( [Result] AS DECIMAL(9,2) ) )
FROM [testresults]
WHERE ISNUMERIC( [Result] ) = 1
and dbo.isReallyNumeric([Result]) = 1
and dbo.isReallyInteger([Result]) = 1
the functions are here http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html
and the result that i got was NULL!!!!!
i need a numeric result. what am i doing wrong?
here is a sample of the data:
625857 AMPH-AMPHETAMINES 357.1 EBB74CF9-D12D-4FBC-917F-91D9DAC169F3
625858 AMP_C-Amphetamine NEGATIVE EBB74CF9-D12D-4FBC-917F-91D9DAC169F3
625859 BARB-BARBITURATES 7.1 EBB74CF9-D12D-4FBC-917F-91D9DAC169F3
625860 BENZ-BENZODIAZEPINES 1.2 EBB74CF9-D12D-4FBC-917F-91D9DAC169F3
625861 COCN-COCAINE METABOLITES -105.5 EBB74CF9-D12D-4FBC-917F-91D9DAC169F3
625862 CR-CREATININE (CHEMICAL) 57.8 EBB74CF9-D12D-4FBC-917F-91D9DAC169F3
625863 ETOH-ETHANOL 134.5 EBB74CF9-D12D-4FBC-917F-91D9DAC169F3
625864 METAMP_C-Methamphetamine NEGATIVE EBB74CF9-D12D-4FBC-917F-91D9DAC169F3
625865 METD-METHADONE -32.3 EBB74CF9-D12D-4FBC-917F-91D9DAC169F3
thank you for all your help. i think i am getting closer:
select MAX(cast(char_value as decimal(10,2))) from
(SELECT
Char_Number = CASE
WHEN id <= 255 THEN RTRIM(id)
ELSE '-' END,
Char_Value = RTRIM(CASE
WHEN id <= 255 THEN CHAR(id)
ELSE result END),
is_numeric = ISNUMERIC(result),
is_really_numeric = dbo.isReallyNumeric(result),
is_really_integer = dbo.isReallyInteger(result)
FROM
testresults
WHERE
ISNUMERIC(result) = 1
OR dbo.isReallyNumeric(result) = 1
OR dbo.isReallyInteger(result) = 1
)
where is_really_numeric=1
but i am getting this error:
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'where'.
updated based on guess of what you were looking for
SELECT MAX( CAST( [Result] AS DECIMAL(10,2) ) )
FROM [testresults]
WHERE ISNUMERIC( [Result] ) = 1;
Try this one
use SalesDWH
go
SELECT MAX(CAST(Result AS DECIMAL(10,2))) FROM testresults WHERE isnumeric(result)=1
But if you get NULL as a result even with this:
and dbo.isReallyNumeric([Result]) = 1
and dbo.isReallyInteger([Result]) = 1
then this may be the actual result - there is no any numberic values in the column
OR
the really numberic values of column are left- or right-padded with spaces etc...
OR
you stripe all the floats with this dbo.isReallyInteger([Result]) = 1 and you have no pure integers in the table
If you are still getting the error, just give an alias to your inner table.
select MAX(cast(char_value as decimal(10,2))) from
(SELECT
Char_Number = CASE
WHEN id <= 255 THEN RTRIM(id)
ELSE '-' END,
Char_Value = RTRIM(CASE
WHEN id <= 255 THEN CHAR(id)
ELSE result END),
is_numeric = ISNUMERIC(result),
is_really_numeric = dbo.isReallyNumeric(result),
is_really_integer = dbo.isReallyInteger(result)
FROM
testresults
WHERE
ISNUMERIC(result) = 1
OR dbo.isReallyNumeric(result) = 1
OR dbo.isReallyInteger(result) = 1
) myInnerTable
where is_really_numeric=1