TSQL Error converting data type varchar to float. Warning: Null value is eliminated by an aggregate or other SET operation - sql

I am getting the above error, and it hits on my Insert on line 95:
"INSERT INTO #total"
I added the columns topic_name varchar(100), samplegroup_name varchar (50), subtopic varchar(50)
from a working query, and that caused the error. None of these columns are Floats, the above types are correct. If I comment out those columns then the query runs fine, so I'm not really sure what to do from here.

You have an issue with the order of the values you try to insert into #total.
Look at the definition of #total:
DECLARE #total TABLE (
item_id varchar (30),
item_dbkey int,
status varchar (7),
nT float,
nY float ,
sigmaX float,
sigmaSqX float,
sigmaY float,
topic_name varchar(100),
samplegroup_name varchar (50),
subtopic varchar(50)
)
and now note the insertion:
INSERT INTO #total
SELECT
Item_id,
CTR.item_dbkey,
TIP.Status,
tpta.topic_name, -- this tries to go into nT
ttisg.samplegroup_name, -- this tries to go into nY
TIP.subtopic, -- this tries to go into sigmaX
COUNT(CTR.item_dbkey), -- Number of exams which had this item_dbkey
COUNT(CASE WHEN iop.weightage >= 1 THEN CTR.item_dbkey END) C, -- How many times was this answered correctly
SUM (CTAP.raw_score ) AS totalscore, -- Sum of all scores (scaled or raw)
SUM (SQUARE( CTAP.raw_score ) ) AS totalscore2, -- Sum of the Square of all scores (scaled or raw)
SUM (CASE WHEN iop.weightage >= 1 THEN ( CTAP.raw_score ) END ) AS totalscorecorr -- When answered correctly, sum the scores (scaled or raw)
topic_name, for example, cannot be inserted into nT column!

Related

How to resolve Operand type clash: date is incompatible with int

as mentioned in title I'm trying to insert a data to a table from another table below here.
Insert into dbo.result (
resultdate,
examdate
)
Select
ResultDate,
ExamDate
From dbo.examdata
But I'm getting the error like in the title.the data type of columns in result table for both the columns are int but in examdata both columns are date data types and I don't want to change the data type of result table.
How to resolve this issue?
Insert into dbo.result (
resultdate,
examdate
)
Select
CONVERT(INT,FORMAT(ResultDate,'yyyyMMdd')) as ResultDate,
CONVERT(INT,FORMAT(ExamDate,'yyyyMMdd')) as ExamDate
From dbo.examdata

sql converting value error

I am getting this error for ItemCode column:
Conversion failed when converting the nvarchar value '8KAM69631KX' to data type int
I'm using the following code:
CREATE table #SatisStokSDH (
STORECODE nvarchar(50),
STORESTOCK int,
ItemCode nvarchar(50),
ColorCode nvarchar(50),
Sales_Unit_L7 int,
SDH int
)
insert into #SatisStokSDH (STORECODE, STORESTOCK, ItemCode, ColorCode, Sales_Unit_L7, SDH)
select STORECODE,
STORESTOCK,
ItemCode,
ColorCode,
Sales_Unit_L7,
STORESTOCK/NULLIF(Sales_Unit_L7,0)*7 as SDH
from #SatisStok
Why am i getting this error? And how can that be corrected?
You are going to insert nvarchar field to int field. please change the datatype of the relevant column belong to '8KAM69631KX'

Decimal place in select statement sql

I have a perfectly working select statement which I have been using. However I have noticed that it does not return any more than zero decimal places. Which I thought Excel had been factoring out, when copied across, but its not.
I have tried to change the format, but I still get zero decimal places. I have reviewed all posts on here, but as I am using a Case statement as well it is not simple to include.
Round(AVG(case when [Duration] > 0 then [Duration] end), 3) as AVGLOS,
Any help welcomed as always.
Data type is an int
That is your problem.
The avg of integer values is always integer:
declare #t table (col int);
insert into #t values (2), (3);
select avg(col)
from #t;
----
2
So you should manipulate decimals, not integers like this:
declare #t table (col int);
insert into #t values (2), (3);
select avg(col * 1.)
from #t;
---
2.500000
So in your case just use this:
Round(AVG(case when [Duration] > 0 then [Duration]* 1. end), 3) as AVGLOS

Conversion failed when converting the nvarchar value 'ABC113973' to data type int

I am facing TWO MAJOR PROBLEMS!!
PROBLEM 1:
I have two tables and want to show the required data into a specific gridview by using UNION in SQL. Table 1 contains columns {[Date] datetime, [Head] nvarchar(50), [Details] nvarchar(360), [ExpId] int, [Amount] decimal(18,2)}
Table 2 contains columns {[Purchase_Date] datetime, [VendorName] nvarchar(50), [Remarks] nvarchar(50), [Bill_No] nvarchar(50), [AmountPaid] decimal(18,2) }
My stored procedure is;
DECLARE #Ledger TABLE
(
DATE DATETIME,
DESCRIPTION NVARCHAR(350),
REF_NO NVARCHAR (50),
AMOUNT INT
)
INSERT INTO #Ledger
SELECT
[Date], [Head] + ' - ' + [Details], [ExpId], [Amount]
FROM
[dbo].[Table1]
UNION
SELECT
[Purchase_Date], 'PURCHASE' + ' ' + [VendorName] + ' ' + [Remarks], [Bill_No], [AmountPaid]
FROM
[dbo].[Table2]
SELECT * FROM #Ledger
When is execute the query I get an error
Conversion failed when converting the nvarchar value 'ABC113973' to data type int.
I wonder why its throwing this error when I try to execute it without Table1 it's fine. Is is due to the column ExpId with datatype int? If yes then how to deal with it?
PROBLEM 2:
In the above #Ledger table when I change Amount datatype to decimal(18,0) as I want to show the result in decimal figure it throws error
Conversion failed when converting varchar into numeric
sort of error. as the datatype of amount columns of both the actual tables are decimal(18,2).
Can anyone tell me the solution and the reasons of this problem? Thanks
Try this:
DECLARE #Ledger TABLE
(
DATE DATETIME,
DESCRIPTION NVARCHAR(350),
REF_NO NVARCHAR (50),
AMOUNT INT
)
INSERT INTO #Ledger
SELECT [Date], [Head] + ' - ' + [Details], CAST([ExpId] AS NVARCHAR(50)), [Amount] FROM [dbo].[Table1]
UNION
SELECT [Purchase_Date], 'PURCHASE' + ' ' + [VendorName] + ' ' + [Remarks], CAST([Bill_No] AS NVARCHAR(50)), [AmountPaid] FROM [dbo].[Table2]
SELECT * FROM #Ledger
You are getting an error because you are trying to insert bill_no and expid into a varchar column,but the data type of both fields are int. So, you will have to either cast or convert the int values and then insert it into the table.
Edit:
If you want to store the amount in Ledger table as decimal then change the data type of amount column to decimal(18,2) and make sure that columns of both the actual tables are also of the same data type.
DECLARE #Ledger TABLE
(
DATE DATETIME,
DESCRIPTION NVARCHAR(350),
REF_NO NVARCHAR (50),
AMOUNT DECIMAL(18,2)
)

Using SQL CAST with the casting type retrieved from the database

Just wondering if this is even possible, and if so... how?
Table (Foos):
Column Value
Id int pk autoinc
Type varchar(50) not null
Name varchar(50) not null
Value varchar(50)
When I do a SELECT * FROM Foos I get the following:
Id Type Name Value
-------------------------------------------------
1 int MaxScore '100'
2 varchar(50) Greeting 'hello world'
3 datetime FollowupDate '01-01-2012'
I want to be able to create a SP that returns a record (using Id) and casting the record to the appropriate type before returning it. I know that I can easily just return the record as is and use an application to cast the string into the appropriate type... but I guess this is just to know if this is possible to expand knowledge and explore... something along the lines of:
SELECT Id, Name, CAST(Value AS [Type])
FROM Foos
But, I get the error:
Msg 243, Level 16, State 1, Line 1
Type Type is not a defined system type.
Am sure you could do this using dynamic SQL, but I don't expect it would be particularly elegant. Have you looked into storing values as sql_variant?
Something like this might give you what you deserve:
declare #Samples as Table ( Id Int Identity, Type VarChar(32), Name VarChar(32), Value VarChar(32) )
insert into #Samples ( Type, Name, Value ) values
( 'int', 'MaxScore', '100' ),
( 'varchar(50)', 'Greeting', 'hello world' ),
( 'datetime', 'FollowupDate', '01-01-2012' ),
( 'money', 'PriceEach', '99.4' )
select Id, Name, Type, Value, case
when Type = 'DateTime' then Cast( Convert( DateTime, Value, 104 ) as SQL_Variant )
when Type = 'Int' then Cast( Cast( Value as Int ) as SQL_Variant )
when Type = 'Money' then Cast( Cast( Value as Money ) as SQL_Variant )
when Type like 'VarChar(%)' then Cast( Cast( Value as VarChar(8000) ) as SQL_Variant )
else NULL end Variant
from #Samples
The VarChar case should have additional code to parse the length and use SUBSTRING() to return a suitable length.
May dog have mercy on your soul.