473,700 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing dollar amounts stored in a varchar

I need to be selecting records based on the value of a string-format
currency value.

Specifically, I have a table with a field of type varchar(50). Stored
in this field is a dollar-formatted value, complete with the dollar
sign and commas for the thousands separator (and an optional leading
minus sign for negative values). For instance: $1,234.56.

One restriction I have to live with is that I can't change the schema
of the table or field - it's gotta stay as a varchar (unfortunately) .

I've found that the decimal function doesn't like the dollar sign and
commas, so I've also found that using the replace function to strip
them out works. So a query that returns records that have a value
greater than $10 looks like this:

select * from myTable where
decimal(replace (replace(myFiel d,'$',''),',',' '),11,2) > 10

Although this works, it seems that this just can't be efficient, so my
question is whether there is a better way to accomplish this? While
sticking with the restriction of no schema change, naturally. :)

Thank you
Mark

Mar 17 '06 #1
3 2259
add a generated column
compField decimal(11,2) generated always as
(decimal(replac e(replace(myFie ld,'$',''),',', ''),11,2))

or create a new table with that.

Mar 17 '06 #2
If the field always has the same amount fo numbers, we can use
Tonkuma's trick:
VALUES TRANSLATE('BDEF GHI', '$1,234.56', 'ABCDEFGHI')

Either way, you should probably make this a DETERMINISTIC FUNCTION to
return a DECIMAL(6,2) or whatever your requirements are.

Also, if you can add a COLUMN, Hardy's idea of a GENERATED ALWAYS
COLUMN would be an excellent answer to this issue.

B.

Mar 17 '06 #3
I couldn't also imagine another way than use REPLACE.
But, following expression may be a little better.
DECIMAL(REPLACE (SUBSTR(your_co lumn,2),',','') ,31,2)
or, if data is not left justified.
DECIMAL(REPLACE (SUBSTR(LTRIM(y our_column),2), ',',''),31,2)
Because, schema of SUBSTR and LTRIM are SYSIBM that means they are
Built-in functions, schema of REPLACE is SYSFUN that means it is a
User-defined function, according to the description in the first page
of "SQL Reference Volume 1 Chapter 3. Functions".
Another issue is that your column length is 50. So, it will be better
to specify maximum precision of decimal data type, if you don't know
maximum data in the column. Of cause, if you know precisions of all
actual data in the column is less than or equal to 11, you should
specify it.

Mar 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6580
by: Jarrod Morrison | last post by:
Hi all Im relatively new to using stored procedures and im not sure if it is possible to do what I am trying to do so any help here is greatly appreciated. I am using the variable @MachineName which is obviously the local machine name mainly in this procedure. What is loop through from the first character of the variable to the last and use this data in a select statement. I have included the code below for what I have tried so far but I...
0
6699
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft Visual Basic .NET version of this article, see 308049. For a Microsoft Visual C++ .NET version of this article, see 310071. For a Microsoft Visual J# .NET version of this article, see 320627. This article refers to the following Microsoft .NET...
4
1779
by: stjulian | last post by:
I have a stored procedure that is supposed to 1. Increment a counter in Table A via a transaction 2. Use this value as the primary key to add in an address to customers Table B (Referenced as a "DECLARE @CustomerID INT" just after the AS clause) 3. Return the primary key. This works perfectly when being called from Query Analyzer supplying values in an EXEC line, however, accessing it from .ASP (IIS 5.0 on Win2K), the
5
2604
by: rhungund | last post by:
Hi All. My question is this. I have a complex stored procedure in SQL Server which works fine when I run it in Query Analyzer. However, when I call it within my ASP script, it returns nothing, and sometimes locks up. If I change my script to call other existing stored procedures it works fine. It's just with this particular stored proc. I have tried various DB calls in ASP, such as opening the recordset through an ADO connection and...
3
2257
by: jrefactors | last post by:
I want to ask what are the possiblities to truncate data from 1 million dollars to 1 dollar by using formatCurrency and CCur functions? The ASP page has code <%= formatCurrency(rs(14)) %>, the database rs(14) is 100000000. But when it displays it in the screen, it shows $1.00 with code <%= formatCurrency(rs(14)) %> There is another place with code using CCur and formatCurrency together, i am not sure if this is ok.
3
1768
by: Bilbo | last post by:
I have a a headscratcher here: I have a form that when submitted should do 2 things when a user enters data and then clicks the Add button. Here goes: 1. Call a stored procedure called AddCompany to insert the company name from the Company Name textbox into the COMPANY table and return the @@IDENTITY of the company name just input into the database back to a label on the form. THIS IS WORKING.
1
1925
by: RSH | last post by:
Im trying to create a stored procedure of the following code. I want to set it so that I have to send the first 4 variables (@DB, @BackUpFile,@TestDB,@RestoreFile). I am having trouble when i try to save it...SQL Server wants me to decalre the variables. How would I go about making this a stored procedure correctly? Here is the code: USE master GO
1
5120
by: gbstclair | last post by:
I new to c programming and was wondering if someone can please help me to understand how to get the following printf statements to align up the dollar amounts when displayed? printf ("\n\n*** Bank Record ***\n\n"); printf ("\n Starting Balance: $ %2.2f \n\n", initial_balance); for (x = 0; x < number_of_deposits; x++) { printf ("\n Deposit #%i: %2.2f \n\n", x + 1, deposits); }
9
2463
by: Frawls | last post by:
Hi I Am am having problems with a stored Procedure that i wrote. Basically whats happening is that the Stored procedure Runs fine when i EXECUTE it in SQL Query analyzer. But when i debug through the application in Visual Studio .NET 2003 the application an exception when it executes the query.
0
8649
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5904
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4408
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4660
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3097
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.