Solved

CAST as money does not work for varchar in SQL 2000

Posted on 2011-03-16
5
459 Views
Last Modified: 2012-05-11
Hello experts:
I have the following SQL command issued from a C# Windows application.

SELECT count(distinct ACCOUNTNUMBER) FROM Extract WHERE (TransactionType = 'CASH_WD' OR (CAST(CASHOUTAMOUNTINSPLITDEPOSIT as money) > 0 and TransactionType = 'WITHDRAWAL' and cast(transactionamount as money) > '3000')) and TaxIdOrSSN = '043305700'

It works fine in SQL Server 2005 but it's failing in SQL Server 2000 with this message:
Implicit conversion from data type varchar to money is not allowed. Use the CONVERT function to run this query.


0
Comment
Question by:lancerxe
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 35151911
try this.
SELECT COUNT(DISTINCT ACCOUNTNUMBER) 
  FROM EXTRACT 
 WHERE (TransactionType = 'CASH_WD' 
         OR (CAST(CASHOUTAMOUNTINSPLITDEPOSIT AS MONEY) > 0 
             AND TransactionType = 'WITHDRAWAL' 
             AND CAST(transactionamount AS MONEY) > 3000)) 
       AND TaxIdOrSSN = '043305700'

Open in new window

0
 

Author Comment

by:lancerxe
ID: 35159751
I found the solution:

SELECT count(distinct ACCOUNTNUMBER) FROM Extract WHERE (TransactionType = 'CASH_WD' OR (CAST(CASHOUTAMOUNTINSPLITDEPOSIT as money) > 0 and TransactionType = 'WITHDRAWAL' and cast((transactionamount as money) > cast( '3000' as money))) and TaxIdOrSSN = '043305700'

Do I get points if I find it myself?
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35159795
Did you try my post. You need not to convert a varchar to money again. You can directly compare by removing the quotes.
0
 

Author Comment

by:lancerxe
ID: 35160028
Sorry Sharath but I did not see any difference bewtween the original script l posted and yours.

Lance
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35160399
The difference is I removed the quotes around 3000. There is no need to take 3000 as varchar and then convert it to numerical. You can directly compare with 3000.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql Data via Excel--performance issues 2 55
PERFORMANCE OF SQL QUERY 13 72
SQL Express connecting form remote error 26 7 43
How to use TOP 1 in a T-SQL sub-query? 14 44
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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