473,796 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Automatically fudging query results?

Let's say I have a column whose values are all 2-digit integers, e.g.
82 (though it's actually a varchar field).

From now on, the column will be able to have 2-digit as well as 3-digit
integers. In the application that uses these values, a value
of the format x0y is considered to be the same as xy.
E.g. values 82 and 802 are considered to be the same, 45 and 405 are
considered to be the same, etc.

Both formats still have to be supported in order to be compatible with
historical data - I'm not in control of the database and unfortunately
existing 2-digit data won't be converted to 3-digit.

The application has many, many separate places where it reads from that
table, e.g.
select colname from sometable where....
And in many, many separate places it uses the same code (hard-coded)
to split up each value into 2 digits, e.g. for value 82, it will
split it up into the digits 8 and 2, and make use of them.

Yep, that query and that code are scattered all over the place and are
not in a common subroutine :( . So it would take a lot of work to change
all of them.

Question: Is there any way to specify the SQL query so that, when it
sees a digit of the format xy, it automatically returns it as x0y?
(e.g. if one row has the value 82 and another has the value 802, the SQL
query fudges the returned rows so both of them have the value 802.)
Maybe with regular expressions somehow?

Even better, is there any way to do that on the database side without
changing the query itself, e.g. with a trigger perhaps?

_______________ _______________ _______________ _______________ _____
The new MSN 8: advanced junk mail protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #1
2 1501
Alex Scollay wrote:
Let's say I have a column whose values are all 2-digit integers, e.g.
82 (though it's actually a varchar field).
From now on, the column will be able to have 2-digit as well as 3-digit


integers. In the application that uses these values, a value
of the format x0y is considered to be the same as xy.
E.g. values 82 and 802 are considered to be the same, 45 and 405 are
considered to be the same, etc.

Both formats still have to be supported in order to be compatible with
historical data - I'm not in control of the database and unfortunately
existing 2-digit data won't be converted to 3-digit.

The application has many, many separate places where it reads from that
table, e.g.
select colname from sometable where....
And in many, many separate places it uses the same code (hard-coded)
to split up each value into 2 digits, e.g. for value 82, it will
split it up into the digits 8 and 2, and make use of them.

Yep, that query and that code are scattered all over the place and are
not in a common subroutine :( . So it would take a lot of work to change
all of them.

Question: Is there any way to specify the SQL query so that, when it
sees a digit of the format xy, it automatically returns it as x0y?
(e.g. if one row has the value 82 and another has the value 802, the SQL
query fudges the returned rows so both of them have the value 802.)
Maybe with regular expressions somehow?

Even better, is there any way to do that on the database side without
changing the query itself, e.g. with a trigger perhaps?

_______________ _______________ _______________ _______________ _____
The new MSN 8: advanced junk mail protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


temp=# create table temp (string1 varchar(8)) without oids;
CREATE TABLE
temp=# insert into temp values ('82');
INSERT 0 1
temp=# insert into temp values ('802');
INSERT 0 1
temp=# select * from temp;
string1
---------
82
802
(2 rows)

temp=# select string1, case when char_length(str ing1)=3 then string1
when char_length(str ing1)=2 then substring(strin g1 from 1 for 1) || '0'
|| substring(strin g1 from 2 for 1) end from temp;
string1 | case
---------+------
82 | 802
802 | 802

Now you could wrap this lot up in a view named the same as the original
table...

temp=# create table temp_table (string1 varchar(8)) without oids;
CREATE TABLE
temp=# insert into temp values ('82');
INSERT 0 1
temp=# insert into temp values ('802');
INSERT 0 1
temp=# create view temp AS select case when char_length(str ing1)=3 then
string1 when char_length(str ing1)=2 then substring(strin g1 from 1 for 1)
|| '0' || substring(strin g1 from 2 for 1) end as string1 from temp_table;
CREATE VIEW
temp=# select * from temp;
string1
---------
802
802

Hope thats almost clear
Nick


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #2
Alex Scollay wrote:
Let's say I have a column whose values are all 2-digit integers, e.g.
82 (though it's actually a varchar field).
From now on, the column will be able to have 2-digit as well as 3-digit
integers. In the application that uses these values, a value
of the format x0y is considered to be the same as xy.
E.g. values 82 and 802 are considered to be the same, 45 and 405 are
considered to be the same, etc.

Both formats still have to be supported in order to be compatible with
historical data - I'm not in control of the database and unfortunately
existing 2-digit data won't be converted to 3-digit.

The application has many, many separate places where it reads from that
table, e.g.
select colname from sometable where....
And in many, many separate places it uses the same code (hard-coded)
to split up each value into 2 digits, e.g. for value 82, it will
split it up into the digits 8 and 2, and make use of them.


So the application doesn't think they're the same.
Yep, that query and that code are scattered all over the place and are
not in a common subroutine :( . So it would take a lot of work to change
all of them.
You should probably correct that anyway.
Question: Is there any way to specify the SQL query so that, when it
sees a digit of the format xy, it automatically returns it as x0y?
(e.g. if one row has the value 82 and another has the value 802, the SQL
query fudges the returned rows so both of them have the value 802.)
Maybe with regular expressions somehow?
You could write a function make_3_digits(m ycol) that returns the 3 digit
version. Although you said you wanted the 2-digit version above.
Even better, is there any way to do that on the database side without
changing the query itself, e.g. with a trigger perhaps?


If "82" and "802" have the same meaning, but you want "802" to be used
throughout, why not just replace all the "82" values everywhere? Write a
trigger so that all new values get converted to the correct format.

Or, you could rename the base tables, replace them with views and have
those views use a function to canonicalise the format of your type.

Or, build your own type that accepts either format but always returns
the 2-digit version.

But, if you really don't have control of the database you'll have to fix
the broken application.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 23 '05 #3

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

Similar topics

3
3957
by: Mike Cocker | last post by:
Hello, I'm quite weak at PHP, so I was hoping to get some help understanding the below code. First off, I'm trying to create a "query form" that will allow me to display the results on my screen. I grabbed this code from the net hoping that I could tweak it for my needs. I'm using MySQL, PHP and IIS and they all are running fine. As the code is, it will display the form, but it won't display my result(s). Any suggestions? Cheers,
2
3435
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the new values are updated in all corresponding tables (the function of the pages in question). However, on the page that does the DB update, I also want to do some checks on the data before performing the update. Now, the problem that I am...
13
2736
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using the iSeries Client Access Driver ver 10.00.04.00 to connect to the database. The problem is that executing the exact same SQL select statement more than twice int a row stops produces results. The first two instances will always produce the...
5
3204
by: Alan | last post by:
Hi there, Are there Excel charting gurus here?? If so then please read on... Sorry for the cross-post but I'm not familiar with the Excel groups. I've posted to asp.general because if I have to code a solution to this it'll probably be done in ASP on a web server, unless there's a significantly better way. I'm looking for a way to create over 100 Excel *charts* programmatically.
3
12555
by: Christopher Koh | last post by:
how do you stop Access from saving any changed data in your tables and queries? like i just add or change data on the table/query tables,then click on X (exit)because i have no intention of saving it but access still automatically saves it even if I did not press the save command on the menu/toolbar? What is the solution for this? help thanks!
1
1376
by: Dan | last post by:
Hi, I have a requirement where in I need to automatically fill a web form and trigger events on the web form automatically. Firstly can this be done. For example: I have a site say xyz.com which has an order form and I do not want to have a manual intervention to enter data into the form. Can I populate data automatically and click on the ‘Order’ button automatically (Again without manual intervention) and still maintain the cookies...
2
5765
by: NowItsWhatever | last post by:
In query DESIGN view, how do I automatically "fit" the columns in the table/field grid to the lengths of the table and field names (including any functions applied to the fields). I am not talking about the query results. I am talking about sizing parts of the query DESIGN view, so that they are readable (long table and field names that get chopped off, long criteria specifications that get chopped off, ...), and saving the sizing so I...
6
2119
by: joe t. | last post by:
The subject may sound a little cryptic, so i'll try my best to explain. Details are unavailable, as i am under a nondisclosure agreement, but i'm looking for general principles and tips, not necessarily fixes for existing code. There is a website that requires me to log in using a web-form. Obviously, POST vars are sent and verified and on success i'm given a Session and/or Cookie. Within this logged-in area, there are links leading to...
2
2376
by: roadworrier | last post by:
I'm trying to create a new object to use as an associative array automatically. I can programmatically create new variables like this: for (var i; i < 10; i++) { window; window = "moo" + i; } // now I have foo0,foo1, foo2, foo3... foo9
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
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
10457
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
10176
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
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5443
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
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3733
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.