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.
Related
As the title states, I am trying to do a pretty simple Insert Into ... Select From ... routine. I am getting this error:
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Error converting data type nvarchar to numeric.
I ran the IsNumeric function on the five columns that are numeric and nothing was returned. Somehow, though, SQL Server is throwing a fit over something here... I just can't tell what the issue is.
I can convert everything to varchar, but that doesn't really solve the problem here. Is there a best practice for debugging something like this? I have Googled this and I have tried a couple different ideas, but now I am stuck.
Here is the SQL code:
INSERT INTO Price_Test2 (ID_RE_GLOBAL, ID_CUSIP,
PX_BID, PX_MID, PX_ASK, PX_CLOSE,
SPREAD, CRNCY, Provider, AsOfDate, rn)
SELECT
x.ID_RE_GLOBAL, x.ID_CUSIP,
x.PX_BID, x.PX_MID, x.PX_ASK, x.PX_CLOSE,
x.PX_SPREAD, x.CRNCY, x.Provider, x.AsOfDate,
ROW_NUMBER() OVER (PARTITION BY x.ID_RE_GLOBAL, x.AsOfDate ORDER BY x.ID_RE_GLOBAL) as rn
FROM
(SELECT DISTINCT
C_LONG.asset_id AS ID_RE_GLOBAL,
C_LONG.cusip AS ID_CUSIP,
CASE
WHEN C_LONG.bid_price LIKE ''
THEN '0'
ELSE C_LONG.bid_price
END AS PX_BID,
CASE
WHEN C_LONG.mean_price LIKE ''
THEN '0'
ELSE C_LONG.mean_price
END AS PX_MID,
CASE
WHEN C_LONG.offer_price LIKE ''
THEN '0'
ELSE C_LONG.offer_price
END AS PX_ASK,
CASE
WHEN C_LONG.closing_price LIKE ''
THEN '0'
ELSE C_LONG.closing_price
END AS PX_CLOSE,
CASE
WHEN C_LONG.bid_ask_sprd LIKE ''
THEN '0'
ELSE C_LONG.bid_ask_sprd
END AS PX_SPREAD,
C_LONG.currency_cd AS CRNCY,
'RE_C_LONG' AS Provider,
C_LONG.themonth + '/' + C_LONG.theday + '/' + C_LONG.theyear As AsOfDate
FROM
RE_C_LONG_2018 AS C_LONG
WHERE
C_LONG.asset_id IS NOT NULL
-- '0x0003f5000d1c2ec9',
-- '0x001005c706310107',
-- '0x0010051a84db01b2',
-- '0x001005c827880293')
) x
---- intermediate --
INSERT INTO Re_Price_Intermediate (ID_RE_GLOBAL,
ID_CUSIP,
PX_BID,
PX_MID,
PX_ASK,
PX_CLOSE,
SPREAD,
CRNCY,
Provider,
AsOfDate,
rn)
select *
from
(
select ID_RE_GLOBAL,
ID_CUSIP,
PX_BID,
PX_MID,
PX_ASK,
PX_CLOSE,
SPREAD,
CRNCY,
Provider,
AsOfDate,
rn = ROW_NUMBER()over(partition by ID_RE_GLOBAL, AsOfDate order by ID_RE_GLOBAL, AsOfDate)
from Price_Test2
) x
where x.rn = 1
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 have the below code
SELECT DISTINCT
S.SCOPE
,convert(nvarchar, getdate(), 112) AS EFFECTIVE_START_TIME
,'99991231' AS EFFECTIVE_END_TIME
,LV.UOM_CODE
,'Gross Price' AS COLUMN_NAME_0
,CONVERT(float,LV2.VALUE_0) * CONVERT(float,LV.VALUE_1) AS VALUE_0
,'Quantity' AS COLUMN_NAME_1
,LV.VALUE_1
FROM
PM_SCOPE S
JOIN
PM_LOOKUP_VALUE LV ON S.SCOPE_ID = LV.SCOPE_ID
JOIN
PM_LOOKUP_DEFINITION LD ON LD.LOOKUP_DEFINITION_ID = LV.LOOKUP_DEFINITION_ID
JOIN
PM_SCOPE S2 ON S2.COMP_PRODUCT = S.COMP_PRODUCT
JOIN
PM_LOOKUP_VALUE LV2 ON LV2.SCOPE_ID = S2.SCOPE_ID
JOIN
PM_LOOKUP_DEFINITION LD2 ON LD2.LOOKUP_DEFINITION_ID = LV2.LOOKUP_DEFINITION_ID
WHERE
LD.LOOKUP_NAME = 'Competitor Bundle Details'
AND LD2.LOOKUP_NAME = 'Competitor Gross Price List'
AND S.GEOGRAPHY = S2.GEOGRAPHY
AND CONVERT(VARCHAR, LV2.UPDATED_AT, 112) > CONVERT(VARCHAR, LV.UPDATED_AT, 112)
When I run it, I get this error
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.
However when I comment out the below line, it works
AND CONVERT(VARCHAR, LV2.UPDATED_AT, 112) > CONVERT(VARCHAR, LV.UPDATED_AT, 112)
I looked at the data and unable to figure out the reason I see this error.
I also looked at the existing posts for this error and they don't seem to help me.
TRY_CONVERT(float,LV2.VALUE_0) * TRY_CONVERT(float,LV.VALUE_1) AS VALUE_0 --This will return NULL since the TRY_CONVERT results in a NULL for one of the columns, and NULL * anything is NULL
CONVERT(float,LV2.VALUE_0) * CONVERT(float,LV.VALUE_1) AS VALUE_0 --This will error out since you can't convert a nonnumeral to a FLOAT
SELECT ISNUMERIC(LV2.VALUE_0), ISNUMERIC(LV.VALUE_1) --This will tell you which column is causing you problems
Here is some test data examples
SELECT TRY_CONVERT(float,'12.2y') * TRY_CONVERT(float,'12.25') --This will return NULL since NULL * anything is NULL
SELECT CONVERT(float,'12.2y') * CONVERT(float,'12.25') --This will error out since you can't convert 12.2y to a FLOAT
SELECT ISNUMERIC('12.2y'), ISNUMERIC('12.2')
I have the following query below that computes a certain amount based on the Date Differences. However, I could not execute the query because of the error:
Msg 8114, Level 16, State 5, Line 7
Error converting data type varchar to numeric.
Query:
declare #var1 datetime
declare #var2 datetime
set #var1 = '2015-07-14 13:31:43.797'
set #var2 = '2015-07-14 13:31:43.797'
select
'Reefer' =
case
when DATEDIFF (hh,#var1,#var2) <= 6 and DATEDIFF (hh, #var1, #var2) > 0
then 429.000
else
case
when (DATEDIFF (hh,#var1,#var2) % 6) > 0
then 429.00 * ((DATEDIFF (hh,#var1,#var2) / 6)+ 1)
else 'wut'
end
end
from
container con
inner join
containerdetail cod on con.containernumber = cod.containernumber
left join
dea on dea.containernumber = con.containernumber
where
con.billofladingnumber = 'IMPJCP07140003'
You can't use multi types in CASE branches:
Edit else 'wut' to else 0
You have problem because you have different datatypes on THEN and ELSE parts, It should be the same. You can fix It by converting THEN part of INT datatype to NVARCHAR() in following:
case
when (DATEDIFF (hh,#var1,#var2) % 6) > 0
then CAST(429.00 * ((DATEDIFF (hh,#var1,#var2) / 6)+ 1) AS VARCHAR(20))
else 'wut'
end
Also this part then 429.000 should be converted to NVARCHAR():
THEN CAST(429.000 AS NVARCHAR(20))
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