Improve company productivity with a Business Account.Sign Up

x
?
Solved

SQL query using case statement and string comparision for informix DB

Posted on 2008-06-19
6
Medium Priority
?
1,482 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
  • 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
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
LVL 16

Accepted Solution

by:
Milleniumaire earned 600 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 600 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

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article will show a step by step guide on how to mask column values in Oracle 12c using DBMS_REDACT full redaction option. This option is available on licensed Oracle Enterprise edition as part of Oracle's Advanced Security.
Are you working to mount the dismounted Exchange 2013 database? Then the best course of action is to analyze the causes of Database issue, their probable solutions and decide for the appropriate course of action.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…

602 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