PLSQL开发实现字符串拆分
在应用程序开发中,会出现单选或多选框条件输入的需求。如输入框的输入值为sz,或sz|nj|zj|nt,在SQL中会这样处理。 select * from tab_1 where col_1=sz ;这是单选框输入。 select * from tab_1 where col_1 =sz|nj ;这是多选框输入。 很明显,多选输入值不
在应用程序开发中,会出现单选或多选框条件输入的需求。如输入框的输入值为'sz',或'sz|nj|zj|nt',在SQL中会这样处理。select * from tab_1 where col_1='sz' ;这是单选框输入。
select * from tab_1 where col_1 ='sz|nj' ;这是多选框输入。
很明显,多选输入值不会查询出结果。
-
如何解决这个问题?
有使用动态SQL实现的方法,如拼装成这样的SQL语句:select * from tab_1 where col_1 in ('sz','nj') ;
还有将'sz|nj'拆分插入到临时表中,再关联该临时表实现,如select * from tab_1 where col_1 in (select a from tt);
临时表涉及到表的创建和维护,还有IO。
我最近想到一个方法:将传入的字符串以嵌套表类型返回,使用表函数调用,实现这类需求。
函数代码如下:
1.create or replace function f_get_unitstring(p_str_all in varchar2,
2. p_str_gap in varchar2) 3. return t_ntb_allstring is 4. --create or replace type t_ntb_allstring is table of varchar2(20); 5. v_ntb_allstring t_ntb_allstring;
6.
7. str_unit varchar2(20);
8. str_char varchar2(1);
9.
10. i_str_length number;
11. i_str_index number;
12.
13.begin 14. /*p_str_city:='nj~wx~sz~cz~zj~nt~yc~';*/ 15. --p_str_all := '1|2|3|'; 16. --p_str_gap := '|'; 17.
18. v_ntb_allstring := t_ntb_allstring();
19.
20. i_str_length := length(p_str_all);
21.
22. i_str_index := 1;
23.
24. while (i_str_index 25. str_char := substr(p_str_all, i_str_index, 1);
26.
27. if (str_char = p_str_gap) then 28.
29. if (str_unit is not null) then 30. v_ntb_allstring.extend(1);
31. v_ntb_allstring(v_ntb_allstring.count) := str_unit; 32. str_unit := null; 33. end if; 34.
35. else 36. str_unit := str_unit || str_char;
37.
38. if (i_str_index = i_str_length) then 39. v_ntb_allstring.extend(1);
40. v_ntb_allstring(v_ntb_allstring.count) := str_unit; 41. str_unit := ''; 42. end if; 43.
44. end if; 45.
46. i_str_index := i_str_index + 1;
47. end loop; 48.
49. return(v_ntb_allstring); 50.end; 测试如下:
1.SQL> select * from table(f_get_unitstring('1aa|2cc|3bb','|')); 2.
3.COLUMN_VALUE
4.-------------------- 5.1aa
6.2cc
7.3bb
8.
9.SQL>
以上解决方法仅供参数,欢迎交流。
再增加一种方法。
使用pipelined函数也能实现这个需求。但在此处性能优势不会体现出来,如果您碰巧碰到大数据量如亿级别的字符串拆分,该方法就能派上用场。
代码如下:
1.create or replace function f_get_unitstring(p_str_all in varchar2,
2. p_str_gap in varchar2) 3. return t_ntb_allstring 4. pipelined is 5. --create or replace type t_ntb_allstring is table of varchar2(20); 6. v_ntb_allstring t_ntb_allstring;
7. str_unit varchar2(20);
8. str_char varchar2(1);
9. i_str_length number;
10. i_str_index number;
11.begin 12. v_ntb_allstring := t_ntb_allstring();
13. i_str_length := length(p_str_all);
14. i_str_index := 1;
15. while (i_str_index 16. str_char := substr(p_str_all, i_str_index, 1);
17. if (str_char = p_str_gap) then 18. if (str_unit is not null) then 19. -- v_ntb_allstring.extend(1); 20. -- v_ntb_allstring(v_ntb_allstring.count) := str_unit; 21. pipe row(str_unit);
22. str_unit := null; 23. end if; 24. else 25. str_unit := str_unit || str_char;
26. if (str_unit is not null) then 27. -- v_ntb_allstring.extend(1); 28. -- v_ntb_allstring(v_ntb_allstring.count) := str_unit; 29. pipe row(str_unit);
30. str_unit := null; 31. end if; 32. end if; 33. i_str_index := i_str_index + 1;
34. end loop; 35. --return(v_ntb_allstring); 36.end;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.
