Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


SQL Convert error

Posted on 2013-11-17
Medium Priority
Last Modified: 2013-11-17
Can you please help,
I'm getting an error when I use below code

Conversion failed when converting the varchar value ' xxxxxx' to data type int.

TransactionDate is a DATETIME

SELECT (TransactionNumber + '-' + CONVERT (varchar, TransactionDate, 1)) AS [TransactionNumber]
FROM AROpentransactions
WHERE transactionType = 1 AND accountNumber = @accountNumber AND (cancelled = 0 OR cancelled IS NULL)
ORDER BY transactionNumber DESC

Any help is appreciated.
Question by:W.E.B
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 66

Expert Comment

by:Jim Horn
ID: 39654599
Some number that is non-numeric is being converted to an int that can't be.

For example, 'banana', 'BR549', and '1-234' cannot be converted to an integer.

So looking at the above...
What data type is @accountNumber and accountNumber?
What data type is cancelled?
What data type is transactionType?

Based on the error message you received, one of these values has an ' xxxxx' in it.

Author Comment

ID: 39654611
AccountNumber    int
cancelled                bit

the error I get xxxxx  
xxxxxxxxxx   is a  date

I just put xxxxxxxxxx in the error because of different dates for different clients.

Example error

Conversion failed when converting the varchar value ' 2013/05/18' to data type int.

LVL 66

Expert Comment

by:Jim Horn
ID: 39654631
Ok.  I'd run the below T-SQL to flush out non-int values, and if there is more than one int column involved with a conversion run it for them as well, changing the column name:  
SELECT AccountNumber
FROM YourTable
WHERE ISNUMERIC(AccountNumber) = 0

Open in new window

Then when you find any values, ask yourself (1) do I want to UPDATE them right away to a correct int value, or (2) do I want to leave the values alone, and exclude them from the SQL in your question?

If (1), handle that on your own with UPDATE statements to change the values to whatever is appropriate.

If (2), add a WHERE ISNUMERIC(AccountNumber) = 1 to the end of the query, which filters it for only numeric values.
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.


Author Comment

ID: 39654676
Appreciate the help,
I still get the same error

SELECT (TransactionNumber + '-' + CONVERT (varchar, TransactionDate, 111)) AS [TransactionNumber]
FROM AROpentransactions
WHERE transactionType = 1 AND accountNumber = 1645 AND (cancelled = 0 OR cancelled IS NULL)
and  ISNUMERIC(AccountNumber) = 1
ORDER BY transactionNumber DESC


Author Comment

ID: 39654712
just to clarify,

if I change 111 to 112,

I get error
Conversion failed when converting the varchar value '_' to data type int.

All I'm trying to do is combine the TransactionNumber to the TransactionDate with a '-' or '_' in between.


or anything similar.

LVL 66

Accepted Solution

Jim Horn earned 1200 total points
ID: 39654725
SELECT (TransactionNumber + '-' + CONVERT (varchar, TransactionDate, 1)) AS [TransactionNumber]

Open in new window

What is the data type of TransactionNumber?  If you're intent is to concatenate it with a varchar, then it also has to be a varchar.  (i.e. apples to apples, oranges to oranges).  So if it's currently not a varchar, you'll need to do this...

SELECT CAST(TransactionNumber as varchar(100))  + '-' + CONVERT (varchar, TransactionDate, 1)) AS [TransactionNumber]

Open in new window


Author Closing Comment

ID: 39654739
SELECT (CAST(TransactionNumber as varchar(100))  + '-' + CONVERT (varchar, TransactionDate, 1)) AS [TransactionNumber]


Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question