SQL multi-field fuzzy query

Source: Internet
Author: User

Http://user.qzone.qq.com/382164370! APP = 2 & via = qz. hashrefresh & Pos = 1346996967

The code is in VC ++:

Cstring SQL, STR; SQL. format (_ T ("select * From tbpatient where admitdatebetween cdate ('% S % s') and cdate (' % S % s')"), dtstartdate. format (_ T ("% x"), dtstarttime. format (_ T ("% x"), dtenddate. format (_ T ("% x"), dtendtime. format (_ T ("% x"); // fuzzy query of fields in MySQL Concat (email, address) like 'like % DF %' If (! Strkeyword. isempty () {// isnull (expression, replace) in SQL Server has the replacement function, // access needs to be combined with isnull (expression), IIF (condition, value_if_true, value_if_false) function // Str. format (_ T ("and isnull (name,'') & isnull (empid, '') like '% S %'"), // strkeyword. getbuffer (0); Str. format (_ T ("and IIF (isnull (name),'', name) & IIF (isnull (empid), '', empid) like '% S %' "), strkeyword. getbuffer (0); SQL + = STR;} trace1 ("% s \ n", SQL );

A rewrite code

Cstring SQL, STR; SQL. Format (_ T ("select * From tbpatient"); If (! M_bdisplayall) // if not all are displayed, the search condition {Str. format (_ T ("where admitdatebetween cdate ('% S % s') and cdate (' % S % s')"), dtstartdate. format (_ T ("% x"), dtstarttime. format (_ T ("% x"), dtenddate. format (_ T ("% x"), dtendtime. format (_ T ("% x"); SQL + = STR; // fuzzy query of the combined fields MySQL Concat (email, address) like 'like % DF %' If (! Strkeyword. isempty () {// isnull (expression, replace) in SQL Server has the replacement function, // access needs to be combined with isnull (expression), IIF (condition, value_if_true, value_if_false) function // Str. format (_ T ("and isnull (name,'') + isnull (empid, '') like '% S %'"), // strkeyword. getbuffer (0); Str. format (_ T ("and IIF (isnull (name),'', name) & IIF (isnull (empid), '', empid) like '% S %' "), strkeyword. getbuffer (0); SQL + = STR ;}// SQL + = _ T ("order by ASC"); // DESC, add sorting trace1 ("% s \ n", SQL );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.