SQL server error "Conversion failed when converting the varchar value 'False' to data type int" - sql-server

Greetings im trying to join these 2 tables to find some data for a report im creating and im getting the error Conversion failed when converting the varchar value 'False' to data type int. Any idea why this is happening? I cant spot where i wrote wrong the code.!! any answer would be welcomed!!!
SELECT L.CUSTNO, CUST_NAME1, CUST_NAME2, CUST_NAME3
,CUST_NAME4, L.BRANCH, REFER_NO
,RIGHT('000000' + CONVERT(VARCHAR, L.CUSTNO), 6) + ' - '
+CAST(LOAN_PRODUCT AS CHAR(4)) + ' - '
+RIGHT('00' + CONVERT(VARCHAR, LOAN_ACC_SEQ_NO), 2) ACC_STR
,[CHECK_PRIN], [L_PRIN], [L_INT], [L_INTLQ], [L_EXP], [L_ACCR]
,[INV_BILL_CHARG_AMT], [P_PRIN], [P_INT], [P_OTH], [P_EXP]
,[P_PRV_S_INT], [P_ACCRUED], [P_OFF_ACCR]
FROM BleckBase1.dbo.[lm-loanacc] AS L
join BleckBase1.dbo.[lm-CUSTOMERMF] AS C ON L.CUSTNO = C.CUSTNO
WHERE ToClose = 0

Related

Conversion failed when converting the varchar value 'YTLQVLBK' to data type int.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException:

I am calling a karate DButil to execute one of the database query in the karate script:
* def test = db.readValue('SELECT * FROM CLIENT C WHERE C.ClientCode = ' + globalAccountID)
Here i', unable to convert the globalAccountID to string.
Please help on this.
My error trace is below:
javascript evaluation failed: db.readRows('SELECT * FROM CLIENT C
WHERE C.ClientCode = ' + globalAccountID), StatementCallback;
uncategorized SQLException for SQL [SELECT * FROM CLIENT C WHERE
C.ClientCode = 707]; SQL state [S0001]; error code [245]; Conversion
failed when converting the varchar value 'YTLQVLBK' to data type int.;
nested exception is com.microsoft.sqlserver.jdbc.SQLServerException:
Conversion failed when converting the varchar value 'YTLQVLBK' to data
type int.
The issue is in this code:
C.ClientCode = 707
More specifically, you are comparing an integer to a string. Based on SQL Server Data type precedence rules, it means that column C.ClientCode is converted to integer before comparison can take place.
The solution is to convert 707 to string (varchar) type. You can do something like this:
'SELECT * FROM CLIENT C WHERE C.ClientCode = ''' + globalAccountID + ''''

Error converting data type nvarchar to numeric. Tried convert and replace

I am trying to add a sum of my forecast using a code
select
sum(cast(replace(replace(ZFREEGOOD, ',', ''), ' ', '') as decimal(22,8)))
from
TEMP_GBR_History_1611
I keep getting an error
Error converting data type nvarchar to numeric.
I used a case statement to figure out which rows failed. These are some of them
Would be of great help to get a solution
You're not handling the blank case, this works with test data:
select sum(cast(case when replace(replace(ZFREEGOOD,',',''),' ','') = '' then '0' else replace(replace(ZFREEGOOD,',',''),' ','') end as decimal(22,8))) from TEMP_GBR_History_1611
One option is to use try_convert(money,...) It tends to be a little more forgiving.
Example
Select AsMoney = try_convert(money,' 27,300') -- works!
,AsInt = try_convert(int,' 27,300')
,AsDec = try_convert(decimal(10,2),' 27,300')
,AsFloat = try_convert(float,' 27,300')
Returns
AsMoney AsInt AsDec AsFloat
27300.00 NULL NULL NULL

Convert String column to DataTime

In my .net winform application i m importing excel data to SQL server. in my excel sheet i have 'STRT_TM' column containing dates in 03/01/2017 21:33:22 format. i want to do explicit conversion because in implicit conversion convert function is unable to determine whether the date is dd/mm/yyyy or mm/dd/yyyy.
In Sql Server i tried to convert a string to date. here is the code
select convert(smalldateTime,SUBSTRING('03/01/2017 14:05:34',7,4)+
SUBSTRING('03/01/2017 14:05:34',1,2) + SUBSTRING('03/01/2017 14:05:34',4,2))
the result is 2017-03-01 00:00:00
but when i try to include time part it gives error.
here is the code.
select convert(smalldateTime,SUBSTRING('03/01/2017 14:05:34',7,4)+
SUBSTRING('03/01/2017 14:05:34',1,2) + SUBSTRING('03/01/2017 14:05:34',4,2) +
Substring('03/01/2017 14:05:34',12,8))
and error is
Msg 295, Level 16, State 3, Line 3
Conversion failed when converting character string to smalldatetime data type.
any help will be appreciated.
You missed a space between date and time:
select convert(smalldateTime,
Substring('03/01/2017 14:05:34',7,4) +
Substring('03/01/2017 14:05:34',1,2) +
Substring('03/01/2017 14:05:34',4,2) + ' ' +
Substring('03/01/2017 14:05:34',12,8))

Query fails on "converting character string to smalldatetime data type"

I've been tasked with fixing some SQL code that doesn't work. The query reads from a view against a predicate. The query right now looks like so.
SELECT TOP (100) Beginn
FROM V_LLAMA_Seminare
//Removal of the following line makes the query successful, keeping it breaks it
where Beginn > (select cast (getdate() as smalldatetime))
order by Beginn desc
When I run the above query, I am greeted with the following error.
Msg 295, Level 16, State 3, Line 1
Conversion failed when converting character string to smalldatetime data type.
I decided to remove the WHERE clause, and now it runs returning 100 rows.
At first, I thought that behind the scenes, SQL Server was somehow including my predicate when bringing back the View . But then I investigated how the View was being created, especially the Beginn field, and at no point does it return a String.
Long story short, the column that becomes the Beginn field is a BIGINT timestamp like 201604201369.... The original user transforms this BIGINT to a smalldatetime using the following magic.
....
CASE WHEN ma.datum_dt = 0
THEN null
ELSE CONVERT(smalldatetime, SUBSTRING(CAST(ma.datum_dt AS varchar(max)),0,5) + '-' +
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),5,2) + '-' +
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),7,2) + ' ' +
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),9,2) +':'+
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),11,2) +':' +
RIGHT(CAST(ma.datum_dt AS varchar(max)),2)) END AS Beginn
...
My last attempt at finding the problem was to query the view and run the function ISDATE over the Beginn column and see if it returned a 0 which it never did.
So my question is two fold, "Why does a predicate break something" and two "Where on earth is this string error coming from when the Beginn value is being formed from a BIGINT".
Any help is greatly appreciated.
This problem is culture related...
Try this and then change the first SET LANGUAGE to GERMAN
SET LANGUAGE ENGLISH;
DECLARE #bi BIGINT=20160428001600;
SELECT CASE WHEN #bi = 0
THEN null
ELSE CONVERT(datetime, SUBSTRING(CAST(#bi AS varchar(max)),0,5) + '-' +
SUBSTRING(CAST(#bi AS varchar(max)),5,2) + '-' +
SUBSTRING(CAST(#bi AS varchar(max)),7,2) + ' ' +
SUBSTRING(CAST(#bi AS varchar(max)),9,2) +':'+
SUBSTRING(CAST(#bi AS varchar(max)),11,2) +':' +
RIGHT(CAST(#bi AS varchar(max)),2)) END AS Beginn
It is a very bad habit to think, that date values look the same everywhere (Oh no, my small application will never go international ...)
Try to stick to culture independent formats like ODBC or ISO
EDIT
A very easy solution for you actually was to replace the blank with a "T"
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),7,2) + 'T' +
Then it's ISO 8601 and will convert...
The solution was found after looking through #Shnugo's comment. When I took my query which contained the Bigint->Datetime conversion logic, and put it into a CTE with "TOP 100000000" to avoid any implicit conversion actions, my query worked. Here is what my view looks like now with some unimportant parts omitted.
---Important part---
CREATE VIEW [dbo].[V_SomeView] AS
WITH CTE AS (
SELECT TOP 1000000000 ma.id AS MA_ID,
---Important part---
vko.extkey AS ID_VKO,
vko.text AS Verkaufsorganisation,
fi.f7000 AS MDM_Nr,
vf.f7105 AS SAPKdnr,
CASE WHEN ma.datum_dt = 0 --Conversion logic
CASE WHEN ma.endedatum_dt = 0 --Conversion logic
CONVERT(NVARCHAR(MAX),art.text) AS Art,
.....
FROM [ucrm].[dbo].[CRM_MA] ma,
[ucrm].[dbo].[CRM_fi] fi,
[ucrm].[dbo].[CRM_vf] vf,
[ucrm].[dbo].[CRM_ka] vko,
[ucrm].[dbo].[CRM_ka] art,
[ucrm].[dbo].[CRM_ka] kat
where ma.loskz = 0
and fi.loskz = 0
and vf.loskz = 0
and fi.F7029 = 0
and vf.F7023 = 0
...
GROUP BY ma.id,
vko.extkey,
vko.text,
fi.f7000 ,
vf.f7105,
ma.datum_dt,
ma.endedatum_dt,
....
)
select * FROM CTE;

update query with xml sql server 2008

I am having problem in updating database from xml and dynamic query.
Exec('UPDATE ' + #DbInstance + 'dbo.tblAcademic
SET tblacademic.RollNo = XMLAcademic.Item.value(''#RollNo'', ''VARCHAR(50)''),
tblacademic.Board = XMLAcademic.Item.value(''#Board'', ''VARCHAR(150)''),
tblacademic.PassingYear = XMLAcademic.Item.value(''#PassingYear'', ''VARCHAR(10)''),
tblacademic.Semester = XMLAcademic.Item.value(''#Semester'', ''VARCHAR(5)''),
tblacademic.MarksObt = XMLAcademic.Item.value(''#MarksObt'', ''varchar(9)''),
tblacademic.MaxMarks = XMLAcademic.Item.value(''#MaxMarks'', ''int'')
FROM ''' + Convert(varchar, #XMLEducationalDetail) + '''.nodes(''/root/row'') AS XMLAcademic(Item)
WHERE tblacademic.AcademicID = XMLAcademic.Item.value(''#AcademicID'', ''int'')')
This is showing error at Convert function and without convert function there is also execution error showing xml to nvarchar error.

Resources