Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL query using case statement and string comparision for informix DB

Posted on 2008-06-19
6
Medium Priority
?
1,478 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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

810 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