Solved

SQL query using case statement and string comparision for informix DB

Posted on 2008-06-19
6
1,469 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now