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
Related
SELECT
LEFT(Replace([SERIAL_NBR],'"',''),34) AS [SERIAL_NBR]
,CONVERT(datetime, Replace([INSERTED_DTM],'"',''), 103) AS [INSERTED_DTM]
,LEFT(Replace([GROUP_NAME],'"',''),1024)
,LEFT(Replace([FIRST_NAME],'"',''),50)
,LEFT(Replace([LAST_NAME],'"',''),50)
,LEFT(Replace([REASON_CODE_ID],'"',''),30)
,CASE isnumeric(Replace([VALUE],'"',''))
when 1
then CAST(Replace([VALUE],'"','') AS float)
else null
END AS [VALUE]
,LEFT(Replace([AUTOLOAD_DELIVERY_STATE_ID],'"',''),15)
,LEFT(Replace([RIDER_CLASS],'"',''), 20)
,LEFT(Replace([ADJUSTMENT_NOTES],'"',''), 1024)
FROM X
This is what I changed it to
SELECT
LEFT(Replace(SERIAL_NBR,'"',''),34) AS SERIAL_NBR
,to_timestamp(Replace(INSERTED_DTM,'"',''), 'DD/MM/YYYY') AS INSERTED_DTM
,LEFT(Replace(GROUP_NAME,'"',''),1024)
,LEFT(Replace(FIRST_NAME,'"',''),50)
,LEFT(Replace(LAST_NAME,'"',''),50)
,LEFT(Replace(REASON_CODE_ID,'"',''),30)
,CASE
WHEN is_Double(Replace(VALUE,'"','')) = 1
THEN CAST(Replace(VALUE,'"','') AS NUMBER)
ELSE NULL
END AS VALUE
,LEFT(Replace(AUTOLOAD_DELIVERY_STATE_ID,'"',''),15)
,LEFT(Replace(RIDER_CLASS,'"',''), 20)
,LEFT(Replace(ADJUSTMENT_NOTES,'"',''), 1024)
FROM stage."TL_A2_Adjustment_Note"
WHERE Replace(SERIAL_NBR,'"','') != 'A2'
It's giving me the error
001044 (42P13): SQL compilation error: error line 9 at position 9
At WHEN is_Double(Replace(VALUE,'"','')) = 1
The case statement is saying "when it can be parsed as a double/numeric do so, else use NULL"
Thus is snowflake this section:
,CASE
WHEN is_Double(Replace(VALUE,'"','')) = 1
THEN CAST(Replace(VALUE,'"','') AS NUMBER)
ELSE NULL
END AS VALUE
can use TRY_TO_DOUBLE which if it fails to parse as a float, will return null.
thus:
,TRY_TO_DOUBLE(Replace(VALUE,'"','')) as value
I want select between 1700300000 to 1700500000 also type of MemberID is nvarchar(50)
select * from tblmember
where cast(MenmberID as bigint) > 1700300000 and cast(MemberID as bigint) < 1700500000
error: Msg 8114, Level 16, State 5, Line 25
Error converting data type nvarchar to bigint.
Use try_cast():
select *
from tblmember
where try_cast(MemberID as bigint) > 1700300000 and
try_cast(MemberID as bigint) < 1700500000
I don't know what your data looks like, but you might be able to use string comparisons:
select *
from tblmember
where MemberID > '1700300000' and
MemberID < '1700500000'
Your problem is that you have non-numerical data in the table.
If the data is non-numerical, it fails the conversion.
In addition to Gordon's fine answers, you could try the following:
select * from tblmember
where
(case when isnumeric(MemberID) = 1 then cast(MemberID as bigint) end) > 1700300000 and
(case when isnumeric(MemberID) = 1 then cast(MemberID as bigint) end) < 1700500000
;
select * from tblmember
where case when isNumeric(MemberID) then cast(MenmberID as bigint)
else 0 end between 1700300000 and 1700500000;
something like that? (if you want/don't want the edge cases then you can play with the between -1/+1 on the values). What about 1700524234.3? Is that ok with you or does it have to be an integer?
I have a stored proc with 2 parameters, both integer
I need to use case statement inside where clause but i could not get it right
where
dbo.StockTransfer.BranchId = #branchId
AND
CASE WHEN IsNumeric(#roleId) = 1
THEN
dbo.StockTransfer.StatusId !=12
ELSE
dbo.StockTransfer.StatusId NOT in (12, 13)
END
order by dbo.StockTransfer.StatusId ASC
I am getting an error in 'dbo.StockTransfer.StatusId !=12'
Msg 102, Level 15, State 1, Procedure GetDeliveryList, Line 44 [Batch Start Line 0]
Incorrect syntax near '!'.
Don't bother. Just use regular logic:
where dbo.StockTransfer.BranchId = #branchId and
( (IsNumeric(#roleId) = 1 and
dbo.StockTransfer.StatusId <> 12
) or
(IsNumeric(#roleId) = 0 and
dbo.StockTransfer.StatusId not in (12, 13)
)
)
This doesn't handle NULL values, but that can easily be added.
Actually, I might simplify this to:
where dbo.StockTransfer.BranchId = #branchId and
dbo.StockTransfer.StatusId <> 12 and
(IsNumeric(#roleId) = 1 or
dbo.StockTransfer.StatusId <> 13
)
Given the newest comments about what you want to return I think it would be something like this. I added lots of white space for clarity on the parenthesis.
where dbo.StockTransfer.BranchId = #branchId
AND
(
(
#roleId = 1
AND
StatusId in (13, 14)
)
OR
(
#roleId <> 1
AND
StatusId = 14
)
)
i need the data which equals to the end of the months in the same table.
what i was struggling so far is :
SELECT * FROM LG_006_01_EMFLINE H
where h.DATE_ in (
declare #start datetime
declare #end datetime
select #Start = (select MIN(mm.date_) as minimum FROM LG_006_01_EMFLINE mm where mm.accountcode like '335%' and mm.DATE_ > '2016-04-01')
select #End = (select MAX(nn.date_) FROM LG_006_01_EMFLINE nn where nn.accountcode like '335%' and nn.DATE_ > '2016-04-01')
;With CTE as
(
Select #Start as Date,Case When DatePart(mm,#Start)<>DatePart(mm,#Start+1) then 1 else 0 end as [Last]
UNION ALL
Select Date+1,Case When DatePart(mm,Date+1)<>DatePart(mm,Date+2) then 1 else 0 end from CTE
Where Date<#End
)
Select date from CTE
where [Last]=1 OPTION ( MAXRECURSION 0 ) )
the error i get is :
Msg 156, Level 15, State 1, Line 7 Incorrect syntax near the keyword
'declare'. Msg 102, Level 15, State 1, Line 26 Incorrect syntax near
')'.
thanks in advance...
You say that the code within your in statement works fine on its own, but as a_horse_with_no_name correctly pointed out, you can't have declare statements or ctes within a sub select.
Consequently you need to rearrange your query a little. As you have not provided your source schema or data I have not been able to test this:
declare #start datetime
declare #end datetime
select #Start = (select MIN(mm.date_) as minimum
FROM LG_006_01_EMFLINE mm
where mm.accountcode like '335%'
and mm.DATE_ > '20160401' -- Remove the hyphens to ensure SQL knows exactly what your date is, avoiding localisation issues.
)
select #End = (select MAX(nn.date_)
from LG_006_01_EMFLINE nn
where nn.accountcode like '335%'
and nn.DATE_ > '20160401' -- Remove the hyphens to ensure SQL knows exactly what your date is, avoiding localisation issues.
)
;With CTE as
(
Select #Start as Date
,Case When DatePart(mm,#Start) <> DatePart(mm,#Start+1) then 1 else 0 end as [Last]
union all
Select Date+1
,Case When DatePart(mm,Date+1) <> DatePart(mm,Date+2) then 1 else 0 end
from CTE
Where Date < #End
)
select *
from LG_006_01_EMFLINE H
where h.DATE_ in (
Select date
from CTE
where [Last]=1
)
option ( maxrecursion 0 )
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.