Converts a string array to SQL in condition with SQL query _mssql

Source: Internet
Author: User
Tags stringbuffer
For example: I want to convert string[] str = {"4", "5", "6"} to "' 4 ', ' 5 ', ' 6 '" string. So I can use SQL query: SELECT * FROM tablename ID in (string).

The source code implemented in the project is as follows:
Copy Code code as follows:

StringBuffer idsstr = new StringBuffer ();
for (int i = 0; i < ids.length; i++) {
if (i > 0) {
Idsstr.append (",");
}
Idsstr.append ("'"). Append (Ids[i]). Append ("'");
}

Another way I thought of it was as follows:
Copy Code code as follows:

public static String Stringarray2strin (string[] str) {

Stringbuffe R sb = new StringBuffer ();
for (int i = 0; i < str.length i++) {
Sb.append ("'). Append (Str[i]). Append (" "). Append (", ");
}
Return sb.tostring (). substring (0, Sb.length ()-1);
}

public static void Main (string[] args) {
string[] str = {"4", "5", "6"};
System.out.println (itemgroupservice.stringarray2string (str));
}

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.