Solved

SQL query using case statement and string comparision for informix DB

Posted on 2008-06-19
6
1,475 Views
Last Modified: 2013-12-24
Hey guys,
Im trying to populate a new column in a database query based on the content of an exisitng column.
Basically if the existing column entry has a certain format, i.e. if its an IP address the new column has a flag set to True.

Can this be done through sql?

Am i heading in the right direction with the following?


update table
set isIPAddress=
case host
when "sql that tests format of host field" then 'Y'
else 'N'
end
0
Comment
Question by:mcgettiu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Milleniumaire
ID: 21829163
"Im trying to populate a new column in a database query"
I assume you mean by this you are trying to update a column in a database table.

"based on the content of an exisitng column"
Is this existing column in the same table or a different table?

If the same table then you can use the case statement as you have identified, however there wouldn't be any need for a nested sql statement as in "sql that tests format of host field" as you would simply be referencing a column in the table being updated.

If you need to reference a column in a different table then your solution is fine.
0
 

Author Comment

by:mcgettiu
ID: 21829621
The existing column is in the same table
I need to tests if the entry in the existing column is an IP Address or a host name.
I need sql that will parse the existing entry,  split it on decimal character, and test that each octet is actually a number in the range 1......255
0
 
LVL 16

Expert Comment

by:Milleniumaire
ID: 21829643
It sounds like you need to use a function to perform the testing of the format.  My experience is with Oracle so I'm not sure if you can do this with informix but I'd be surprised if you couldn't.

If you build a database function which accepts a parameter and returns 'Y' or 'N' then you could use it as follows:

update table
set isIPAddress= myfunction(host)

The function would then perform the parsing and testing of the value of host.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 16

Accepted Solution

by:
Milleniumaire earned 150 total points
ID: 21829664
A quick search of the web has confirmed my theory that you can build stored functions i.e. functions stored in the database.  The following link shows the syntax in case you aren't familiar with this concept:

http://www.dbcenter.cise.ufl.edu/triggerman/infoshelf/sqlt/14.fm2.html#153225
0
 
LVL 2

Assisted Solution

by:TheLastStraw
TheLastStraw earned 150 total points
ID: 21832154
Only way I see you doing this in one pass is by formatting update statement something like below.

update table set isIPAddress= IsAHost(Table.HostFieldColumnName)

Create a Stored procedure in database using DDL something like

CREATE STORED PROCEDURE IsAHost(HostArg AS CHAR(50))
      IF HostArg LIKE "*.*.*" THEN
             RETURN 'Y'
      ELSE
            RETURN 'N'
      END IF
END PROCEDURE

Alternatively, you could just use SQL by creating  a temp table of valid hosts then run two updates one for updating the records with vaues in host table and one where they don't exist in host table. Informix like some other databases does not  allow you update a table with a subquery listing same table.

0
 

Author Closing Comment

by:mcgettiu
ID: 31468781
Hey guys,
Thanks for your help.  No longer need to do this, but you gave me a crash course on DBs which was much appreciated :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

729 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