Solved

C function that behaves exactly like the SQL LIKE function

Posted on 2004-09-10
7
200 Views
Last Modified: 2010-04-15
Hi,

Does anyone know of a function or can provide an code sample of a C function that behaves exactly like the SQL LIKE function?  The function should accept and properly evaluate all wildcards and special operators used by the SQL LIKE ( %, ^, [], etc ).

Thanks In Advance.
0
Comment
Question by:CUGroup
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12033134
Hi CUGroup,

If you can describe the exact behaviour or SQL LIKE and tell us the platform that you are working on, hopefully we can come up with something
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12043087
Hi,

It should be relatively straight forward to convert from SQL syntax to standard Regular Expression syntax (man regex on Unix)

e.g.

_ -> .
% -> .*

etc.
the write wrapper function(s) on the regex library calls, to convert the LIKE string to the regular exp. and to process from that...

Hope this helps,
C.
0
 

Author Comment

by:CUGroup
ID: 12069571
Hi,

Thanks for the responses.  Unfortunately, I need function to be portable, UNIX and Windows so I'm in search of a c function to mimic this sql function.

This is the best resource I could find regarding the use of the SQL LIKE function.

http://www.sql-tutorial.net/SQL-LIKE.asp

Thank you for your help.

0
 
LVL 2

Accepted Solution

by:
sneeuw_chan earned 125 total points
ID: 12070444
I think it corresponds more to 'glob' than to 'regex'.
(Glob is the way that a dos-prompt or unix shell matches filenames on the commandline.)

You could try looking for a 'glob'  library.
A quick google shows this, for example:  http://www.cs.yorku.ca/~oz/glob.bun
(You could even change that code to parse this 'LIKE' format directly, just change the case '*'  to case '%' and the case '?' to case '_'.)

If that's not to your liking, search for other glob libraries.
0
 
LVL 11

Assisted Solution

by:cjjclifford
cjjclifford earned 125 total points
ID: 12072899
CUGroup, if you are just looking for a portable (Unix and Windows) way of matching patterns in text, then surely the standard Posix regex functionality I listed would be the way to go? I'd be confident that there is a Windows inplementation (defintely if you use a GCC derived compiler)
Either that, or as sneeuw_chan suggested use Glob functionality.
I wouldn't go with the SQL LIKE format, unless you are actually implementing a SQL engine... LIKE is a lot more cumbersome than regex or glob...
0

Featured Post

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

829 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