Solved

SQL Convert error

Posted on 2013-11-17
7
398 Views
Last Modified: 2013-11-17
Hello,
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.

note:
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.
thanks,
0
Comment
Question by:Wass_QA
  • 4
  • 3
7 Comments
 
LVL 65

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.
0
 

Author Comment

by:Wass_QA
ID: 39654611
Hello,
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.

thanks,
0
 
LVL 65

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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Wass_QA
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

thanks,
0
 

Author Comment

by:Wass_QA
ID: 39654712
Hello,
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.

Example
1645-2013/11/15
or
1645-2013.11.15

or anything similar.

thanks
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 300 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

0
 

Author Closing Comment

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

worked.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now