Solved

SQL query using case statement and string comparision for informix DB

Posted on 2008-06-19
6
1,474 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…
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…

739 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