Solved

CAST as money does not work for varchar in SQL 2000

Posted on 2011-03-16
5
448 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

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…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

16 Experts available now in Live!

Get 1:1 Help Now