Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query returns error - Msg 245, Level 16, State 1

Author  Topic 

lanti_p
Starting Member

15 Posts

Posted - 2011-12-14 : 06:34:13
Hello everybody,


I am confused regarding this query of mine.
I keep getting this error
quote:
Msg 245, Level 16, State 1, Line 16
Conversion failed when converting the varchar value 'AA' to data type int.
Warning: Null value is eliminated by an aggregate or other SET operation.


The query is
SELECT instance, 

(CASE WHEN rates_name IN ('AW', 'AW1', 'BW1') THEN 'Konsum vetjak' ELSE
CASE tip_abonenti WHEN '1' THEN 'Privat' WHEN '2' THEN 'Familjare' WHEN '3' THEN 'Buxhetore' WHEN '4' THEN 'Jo Buxhetor'
ELSE 'Pa Percaktuar' END END) AS [Tipi i abonentit] ,

sum(FAT_Kw) as fat_kwh,
sum(total_me_tvsh_pa_tvtax) as total_me_tvsh_pa_tvtax

FROM (


SELECT INSTANCE_NAME as instance, i.INSTCODE, c.zone_name, sc.flag_domestic as tip_abonenti, C.code_contract,C.name,
re.rates_name,
case when sc.validity=1 then 'Normal'
when sc.validity=0 then 'Canceled'
end as Invoice_type, year(sc.nr_month) as Viti, month(sc.nr_month) as Muaji,
consumption + cons_subscribe + cons_specifique + enpik + aforfepik as FAT_Kw,
consumption, cons_subscribe, cons_specifique, aktive_dem, enpik, aforfepik, reaktive, aforfereaktiv, reaktive_dem,
sc.total as total_me_tvsh_pa_tvtax,
b.nr_serial as nr_serial_fature

FROM dbo.Subscribers_consumption as sc
inner join dbo.[Contract] as C on abs(sc.ref_contract) = c.identification

inner join dbo.Rates as R on C.ref_rates = R.numbering_rates
inner join dbo.Bill as B on b.ref_consumption = sc.numbering_subscribers
inner join dbo.INSTANCES as i on c.INSTCODE = i.INSTCODE
inner join (select numbering_rates, rates_name from dbo.SaleStructureTariffs as sst inner join dbo.Rates as r on convert(VARCHAR(255), sst.Tariff) = r.rates_name) as re on c.ref_rates = re.numbering_rates

WHERE (sc.Nr_month BETWEEN @StartDate AND @EndDate)
AND sc.type_index = 'C'
AND sc.total <> 0

and C.VALIDITY = 1
and B.VALIDITY = 1
and c.CODE_CONTRACT not in (select CODE_CONTRACT from dbo.Kontatoret_tp)
) as alldata

GROUP BY instance,
(CASE WHEN rates_name IN ('AW', 'AW1', 'BW1') THEN 'Konsum vetjak' ELSE
CASE tip_abonenti WHEN '1' THEN 'Privat' WHEN '2' THEN 'Familjare' WHEN '3' THEN 'Buxhetore' WHEN '4' THEN 'Jo Buxhetor'
ELSE 'Pa Percaktuar' END END)


quote:
where column are of:
rates_name (varchar, null)
tip_abonenti(int, null)
numbering_rates (pk, int, not null)
sst.Tariff (nvarchar, null)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-14 : 06:39:44
check if all columns involved in below line are of same type and contain only numeric data

consumption + cons_subscribe + cons_specifique + enpik + aforfepik


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

lanti_p
Starting Member

15 Posts

Posted - 2011-12-14 : 08:02:49
Thank you for your time visakh16,

The fields you mention are all of int datatype

quote:
If I go and Union the result of these query with these query again (sql_query UNION ALL sql_query) , I get the error I mention.

quote:
Just for info ... the "...converting the varchar value 'AA' to data type int..."mentioned in the error message, is data stored in rates_name field.
I am affraid, it has sth to do with the CASE statement. I am just missing here the part when it tries to convert the varchar into "intiger".
I do not apply any conversion...!!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-14 : 09:13:01
not sure thats the reason as i cant see anywhere you're making it an int either explicitly or by implicit conversion

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -