Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
farmerregistration table as follows


farmerid datatype Varchar(50) in farmerregistration table

farmerid Firstname Region Zone Section Village

1055662 Lacina OUNGAlo Diawala Nord Diwala


transaction table as follows

transactionid datatype Bigint in transaction table

transactionid Qty Price Paid Due

1055662 1 200 200 100


from the above i want output as follows

Firstname Region Zone Section Qty Price Paid

Lacina OUNGALo Diawala Nord 1 200 200


My query as follows

select a.firstname,a.Region,a.Zone,a.Section,b.Qty,b.Price,b.paid
from farmerregistration a,
transaction b where a.transactionid = b.farmerid

Note in farmerregistration table farmerid datatype is varchar(50)
in transaction table transactionid datatype is bigint

when i run the above code shows error as follows

Error converting data type varchar to bigint.

how to solve this error. from my above query what changes i have to made.

What I have tried:

farmerregistration table as follows


farmerid datatype Varchar(50) in farmerregistration table

farmerid Firstname Region Zone Section Village

1055662 Lacina OUNGAlo Diawala Nord Diwala


transaction table as follows

transactionid datatype Bigint in transaction table

transactionid Qty Price Paid Due

1055662 1 200 200 100


from the above i want output as follows

Firstname Region Zone Section Qty Price Paid

Lacina OUNGALo Diawala Nord 1 200 200


My query as follows

select a.firstname,a.Region,a.Zone,a.Section,b.Qty,b.Price,b.paid
from farmerregistration a,
transaction b where a.transactionid = b.farmerid

Note in farmerregistration table farmerid datatype is varchar(50)
in transaction table transactionid datatype is bigint

when i run the above code shows error as follows

Error converting data type varchar to bigint.

how to solve this error. from my above query what changes i have to made.
Posted
Updated 26-Apr-19 9:21am
Comments
Richard MacCutchan 21-Jun-18 3:28am    
Why are you using VARCHAR(50) for a numeric value? You need to make both fields the same type so they can be compared.
Richard MacCutchan 21-Jun-18 3:29am    
I repeat my earlier suggestion: Get some programming books and learn the basics.

IT's pretty simple: You are storing data in the wrong format.
When you use VARCHAR or NVARCHAR instead of a numeric or date based column, you are almost guaranteed that at some point someone will enter bad data - and it won;t get spotted until it's actually used, by which time it's far too late to work out what it should be and correct it.
And it normally shows up as a conversion error when you try to use the data, just as it does in your table.

Always store data in t6eh most appropriate type, or you will continue to have this problem.

Change your DB, convert the values that can be converted, and correct the ones that can't.
 
Share this answer
 
In my opinion the error ocurrs because there are records in the farmerregistration table that conteins 'no number' in the farmerid field (for example: A12334, 213 F... or something like this).

So, the where a.transactionid = b.farmerid treats to convert this 'no numbers' values to a bigint and causes an error.

Try to use:
SQL
where ltrim(rtrim(a.transactionid))=ltrim(rtrim(convert( varchar(50), b.farmerid)))


(Explanation, instead of implicit convert varchar to bigint, convert explicitly bigint to varchar and then compare the alltrim [ltrim, rtrim] of that values)
 
Share this answer
 
I'm not sure if this will help, as I'm very very new to this. I was just attempting to fix what appears to be a similar issue. I created a search query and was getting the same error.

I went into my Database's Design page, changed to design for that field from BigInt to nvarchar(50).

Then I went into the original Query, right clicked on it, and clicked Configure. I had to re-click through everything, but when I clicked on Finish and the Query updated, my program began to work.

Best of luck. I apologize if this is total jibberish.. just a complete newbie here.
 
Share this answer
 
Comments
CHill60 1-May-19 9:19am    
You should never store numeric data in nvarchar columns. You should have changed the (other) varchar column to be a bigint. Always use the most appropriate column type for the data that will be stored in it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900