Conversion failed when converting the varchar to data type int - sql

I have this query, I need to save Pubname, ISBNname, copiesname and createdname as Integers
DECLARE #a Table(bkID INT, bkname varchar(100), bkpub INT, bkISBN INT, bkcopies INT, bkcreatedby INT)
INSERT INTO #a (bkID, bkname, bkpub, bkISBN, bkcopies, bkcreatedby)
SELECT A.nameID,
A.bkname,
CAST((B.Pubname) AS INT),
CAST((C.ISBNname) AS int),
CAST((D.copiesname) AS int),
CAST((F.createdname) AS int)
FROM #bkname AS A
LEFT JOIN #bkPub AS B
ON (A.nameID = PubID)
LEFT JOIN #bkISBN AS C
ON (A.nameID = C.ISBNID)
LEFT JOIN #bkcopies AS D
ON (A.nameID = D.copiesID)
LEFT JOIN #bkcraeted AS F
ON (A.nameID = F.craetedID)
It returns this error:
Msg 245, Level 16, State 1, Procedure LR_InsertBookArray, Line 46
[Batch Start Line 2] Conversion failed when converting the varchar
value ''3' to data type int.

It seems highly unlikely that columns called name are actually integers in disguise. My guess is that you simply want the ids from the reference table, but your question doesn't have enough information to know if that is true.
In the meantime, you can use TRY_CAST():
TRY_CAST(B.Pubname AS int),
TRY_CAST(C.ISBNname AS int),
TRY_CAST(D.copiesname AS int),
TRY_CAST(F.createdname AS int)
This will return NULL if the values cannot be converted -- but it avoids the error.

Related

Conversion fails when running stored procedure

Aloha,
I have a stored procedure that looks in 3 different columns for a value.
ALTER PROCEDURE [dbo].[sp_CLAIMS_QUERY_FAC_]
#sPLAN varchar(5),
#sTYPE varchar(3),
#nVALUE varchar(10) ,
#begDOS date,
#endDOS date
AS
BEGIN
SET NOCOUNT ON;
-- QUERY BASED ON CAID.............................................................
IF #sPLAN = 'CAID'
IF #sTYPE = 'MBR'
BEGIN
SELECT DISTINCT
PLN, DH, PT, PROVIDER, TAX, PROV_NAME, MEMGRP, MEMSUB, MBR_NAME,
POS, AUDNBR, CLM_ADJ, LN_NR, RCVD_DT, FIRST_DOS, LAST_DOS, CPT, CPT_MOD,
UNITS, DIAG1, PAID_DT, REV_CDE, BILL_TYPE, FIN_LGL_NBR, PAR, OCC_CD5,
AMT_CLAIMED, AMT_DISALLOW, AMT_DISCOUNT, AMT_DEDUCT, AMT_COPAY, AMT_PAID,
DEN_CDE, DSLW_CDE, DENY_DESC, DSLW_DESC, ADJ_RSN_DESC, REV_DESC, POS_DESC,
CPT_DESC, DIAG_DESC
FROM v_QI_CLAIMS_FAC
WHERE (#nVALUE = (MEME_HICN) AND (FIRST_DOS BETWEEN #begDOS AND #endDOS) AND PLN = 'CAID')
OR (#nVALUE = (MEMSUB) AND (FIRST_DOS BETWEEN #begDOS AND #endDOS) AND PLN = 'CAID')
OR (#nVALUE = (MEME_CK) AND (FIRST_DOS BETWEEN #begDOS AND #endDOS) AND PLN = 'CAID')
ORDER BY AUDNBR, LN_NR, CLM_ADJ
END
The data in the memsub and meme_hicn are varchar in the table, meme_ck is an int. The query pulls data okay for memsub and meme_ck, but when the value comes from meme_hicn I get the error:
Conversion failed when converting the varchar value '558901488A' to data type int (where the data value is '558901488A'.
It looks like it is interpreting meme_hicn as an int, but it should be as a varchar.
I have tried all sorts of options, cast, etc. but nothing seems to work.

When create function in SQL, Scalar variable must be INT?

CREATE FUNCTION show_inst_borrowed_out(#bRecordID CHAR(10))
RETURN TABLE
AS
RETURN
(SELECT bRecordID, br.instID, g,instType
FROM borrowRecord br
INNER JOIN gear g ON br.instID = g.instID
WHERE bRecordID = #bRecordID);
SELECT * FROM show_inst_borrowed_out(BR00000004);
When I run the above statement, it shows this:
Msg 207, Level 16, State 1, Line 416
Invalid column name 'BR00000004'.
What should I type if my scalar variable will be something like BR00000004 [CHAR(10)]?
My answer would be
SELECT * FROM show_inst_borrowed_out('BR00000004')
Since you declared it as CHAR, you have to enclose it in apostrophes.

The conversion of the varchar value overflowed an int column in sql server 2012

I'm getting an error with a query to get data... The error message is: Msg 248, Level 16, State 1, Line 1: The conversion of the varchar value overflowed an int column...
I cant resolve this problem, so if any one can help me, thanks in advance, Here is My Sql:
This happens when I inserted 3 new joins in the query, I made them BOLD, otherwise it works perfectly...
SELECT DISTINCT
ACR_ART_ID, DES_TEXTS.TEX_TEXT AS CRITERIA_DES_TEXT,
COALESCE(DES_TEXTS2.TEX_TEXT, ACR_VALUE) AS CRITERIA_VALUE_TEXT,
(DES_TEXTS.TEX_TEXT + ': ' + COALESCE(DES_TEXTS2.TEX_TEXT, ACR_VALUE)) AS CEL_OPIS
FROM
Inventory.dbo.ARTICLE_CRITERIA
LEFT JOIN
Inventory.dbo.DESIGNATIONS AS DESIGNATIONS2 ON DESIGNATIONS2.DES_ID = ACR_KV_DES_ID
LEFT JOIN
Inventory.dbo.DES_TEXTS AS DES_TEXTS2 ON DES_TEXTS2.TEX_ID = DESIGNATIONS2.DES_TEX_ID
LEFT JOIN
Inventory.dbo.CRITERIA ON CRI_ID = ACR_CRI_ID
LEFT JOIN
Inventory.dbo.DESIGNATIONS ON DESIGNATIONS.DES_ID = CRI_DES_ID
LEFT JOIN
Inventory.dbo.DES_TEXTS ON DES_TEXTS.TEX_ID = DESIGNATIONS.DES_TEX_ID
INNER JOIN
Inventory.dbo.ART_LOOKUP al ON ARTICLE_CRITERIA.ACR_ART_ID = al.ARL_SEARCH_NUMBER
AND al.ARL_KIND in (1,3)
INNER JOIN
Inventory.dbo.ARTICLES a ON al.ARL_ART_ID = a.ART_ID and (a.ART_SUP_ID=21 or a.ART_SUP_ID=11091)
INNER JOIN
Inventory.dbo.SUPPLIERS ON SUPPLIERS.SUP_ID = ART_SUP_ID**
WHERE
(DESIGNATIONS.DES_LNG_ID IS NULL OR DESIGNATIONS.DES_LNG_ID = 25)
AND (DESIGNATIONS2.DES_LNG_ID IS NULL OR DESIGNATIONS2.DES_LNG_ID = 25);
As Commented by #T I
The varchar col is implicitly converted to an int and is overflowing (i.e. is >larger than 2,147,483,647). To resolve this try casting the columns to bigint
you can cast column value data type as
select cast( max(columnname) AS bigint)+1 from atable ;
But if you will get a varchar than it will fail,
with error
So it is better to change your DB schema.
Use this approach only if you are sure that only numeric string will be present.

An expression of non-boolean type specified in a context where a condition is expected, near ')'. ERROR

ALTER FUNCTION [dbo].[TabloGetir] (#id INT,
#ay INT,
#yil INT)
RETURNS TABLE
AS
RETURN
(SELECT ARACKM_ARAC_ID,
ARACKM_ID,
ARACKM_TARIHI,
ARACKM_KM,
A_PLAKA,
A_ID
FROM VARACKM
WHERE A_ID = #id
AND MONTH(ARACKM_TARIHI) = #ay
AND YEAR(ARACKM_TARIHI) = #yil)
This is my tabled-valued function and i keep getting that error can someone please help me?
This is where i call it:
comm2 = New SqlCommand(#"select ARACKM_TARIHI, SUM(ARACKM_KM) AS TUTAR
from VARACKM
GROUP BY ARACKM_TARIHI
HAVING [dbo].[TabloGetir](#A_ID,#ay,#yil) ",conn2)
comm2.Parameters.Add("#A_ID", plakaId)
comm2.Parameters.Add("#ay", ay)
comm2.Parameters.Add("#yil", yil)
reader2 = comm2.ExecuteReader()
The problem is the having clause.
HAVING [dbo].[TabloGetir](#A_ID,#ay,#yil)
First, your function is returning a table, and the having clause expects a scalar with some sort of comparison. Second, you have no comparison. Perhaps you mean:
HAVING EXISTS (select 1 from [dbo].[TabloGetir](#A_ID,#ay,#yil) )

Arithmetic overflow error converting expression to data type float

Working on an analysis of bonds. I have attempted to make a payment function that replicates the PMT function of excel. For the bonds, the "Cusip" is their identifier, their "PASS THRU RATE" is their annual interest rate, the "ORIGINAL WA MATURITY" is the total number of periods, and the "ORIGINAL BALANCE" is the original face value of the bond.
The equation for calculating a monthly payment by paper is:
M=[OF(i(1+i)^n)]/[(1+i)^(n-1)]
M=Monthly payment
OF=Original Face
i=annual interest rate/12
n=number of periods
I have a table with all the columns needed for this function, as well as different tables for different months that I will try and use this for. This is what I have so far, creating the function and trying to fix for data types:
if object_id('dbo.PMT') > 0
drop function dbo.PMT
go
create function dbo.PMT(#rate numeric(15,9), #periods smallint, #principal numeric(20,2) )
returns numeric (38,9)
as
begin
declare #pmt numeric (38,9)
select #pmt = #principal
/ (power(1+#rate,#periods)-1)
* (#rate*power(1+#rate,#periods))
return #pmt
end
go
drop function dbo.PMT
go
create function dbo.PMT
(
#rate float,
#periods smallint,
#principal numeric(20,2)
)
returns numeric (38,9)
as
begin
declare #pmt numeric (38,9)
declare #WK_periods float,
#WK_principal float,
#wk_One float,
#WK_power float
select #WK_periods = #periods,
#WK_principal = #principal,
#WK_One = 1
select #pmt =
round(
( #WK_principal * (#rate*power(#WK_One+#rate,#WK_periods)))
/ (power(#WK_One+#rate,#WK_periods)-#WK_One)
,9)
return #pmt
end
go
select ALL [CUSIP NUMBER]
,[PASS THRU RATE]
,[ORIGINAL WA MATURITY]
,[ORIGINAL BALANCE],
dbo.pmt((mbs012013.[PASS THRU RATE]),mbs012013.[ORIGINAL WA MATURITY],mbs012013.[ORIGINAL BALANCE])
FROM
[MBS_STATS].[dbo].[mbs012013]
However, I receive
(502882 row(s) affected)
Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting expression to data type float.
when I attempt to execute it. I cannot figure out what is causing this. Any help would be great!
In the line below, you have #WK_principal as a FLOAT, and you're assigning the value of #principal which is a NUMERIC(20,2).
#WK_principal = #principal,
That seems to be the most likely culprit. We'd need to be able to see your data to help otherwise. Also, I'm not clear on why you're creating the function one way, then dropping it and recreating it differently. Or are you just showing two different attempts?