An universal algorithm design of fixed length substring locating

Stringlocating is a very common operation not only in the SQL design but also in theclient development tools, in which functions are also supplied to realize thelocating operation, such as the ‘LOCATE’ function in DB2 or ‘POS function in DELPHI.

In fact, we maymeet with the special string locating, such as, we have to locate fixed lengthstring locating, in which you may get the wrong result if you use the given ‘LOCATEor ‘POS function. In the essay, I give an algorithm that you can easily transformto other DBMS or client development tools.

There is a fewtables inn the OLTP in my company which contain some fixed coded fields, suchas field ‘roadcomb’ recording all the highways that vehicles have passed by.Each highway is coded in two hex characters, and 17 means highway between Canton and Huizhou.

In one task, Istart to statistic the records in a certain time that passed through Guanghuihighway. So the solution SQL is in the bellow. As for how to create and executea DB2 function please see my essay How to createDB2 user function easily by DB Query Analyzer 6.03

SELECT *

FROMDEVELOP.TB_OUTLISTGW

WHERE LOCATE(ROADCOMB,’11’)>0

FETCH FIRST 100ROW ONLY

WITH UR;

However, it willresult some errors. What will happen if one value of ‘ROADCOMB’ is '01100110011001100110011001100110'.According the SQL above, it is a legal record which will return. In fact, thevehicle has never passed through Guanghui highway. As a result, the propermethod is locate the field every two characters, not one character. So I designa function in the bellow and I will execute them by DB Query Analyzer.

DROP FUNCTIONDEVELOP.F_LOCATE_FIXED

$$

CREATE FUNCTIONDEVELOP.F_LOCATE_FIXED(IN V_SRC VARCHAR(4096),IN V_DES VARCHAR(4096))

RETURNS INTEGER

LANGUAGE SQL

DETERMINISTIC

NO EXTERNALACTION

CONTAINS SQL

BEGIN ATOMIC

DECLARE v_result        INTEGER DEFAULT 0;

DECLARE str_road            VARCHAR(4096);

DECLARE v_len_src           INTEGER;

DECLARE v_len_des           INTEGER;

DECLARE v_pos_src           INTEGER DEFAULT 1;

SET v_len_des=LENGTH(TRIM(V_DES));

SET v_len_src=LENGTH(TRIM(V_SRC));

SET v_result=0;

AUTHLOOP:

WHILE v_pos_src+v_len_src<=v_len_des DO

SET str_road=SUBSTR(V_DES,v_pos_src,v_len_src);

IF ( str_road=V_SRC ) THEN

SET v_result=v_pos_src;  --return the physical position

SET v_result=v_result/v_len_src+1;  -- return the logical position, if not be annotated.

LEAVE AUTHLOOP;

ELSE

SET v_pos_src=v_pos_src+v_len_src;

END IF;

END WHILE;

RETURN v_result;

END

$$

values  DEVELOP.F_LOCATE_FIXED('11','4F0A0B0D231154533F01')

$$

values DEVELOP.F_LOCATE_FIXED('11','01100110011001100110011001100110')

$$

SELECT *

FROMDEVELOP.TB_OUTLISTGW

WHERE DEVELOP. F_LOCATE_FIXED ('11',ROADCOMB)>0

FETCH FIRST 100ROW ONLY

WITH UR;

$$

1  How to create the function in DB2 by DB QueryAnalzyer

Figure 1  SQL scripts to create the function and execute the function

Figure 2  SQl scripts are executed

Figure 3 DEVELOP.F_LOCATE_FIXED('11','4F0A0B0D231154533F01') returns 6

Figure 4

DEVELOP.F_LOCATE_FIXED('11','01100110011001100110011001100110') returns 0

When looking for ‘11’ in '01100110011001100110011001100110',Unlike ‘LOCATE’function in DB2, DEVELOP.F_LOCATE_FIXED return 0, the right result, which showsthat the vehicle has never passed through Guanghui highway.

 

A kind reminder, it is necessary to design your own function when notjust seeking the first position in the string, and it is likely to concern theloop operation.

Finally, I will give you a complex question. How to get all the vehicles fromtableTB_OUTLISTGW which passed from the given start toll station in thecertain highway to the give end toll station in the certain highway? The highways thatvehicles passed by are stored in field ROADCOMB, in tollstations are stored in the fieldROADSSTARTSTATION and the out tollstations are stored in the field ROADESTARTSTATION in table TB_OUTLISTGW. Both ROADSSTARTSTATIONand ROADESTARTSTATION are coded in four-character-length hex string.

 

2  Brief introduction of DB Query Analzyer

DB Query Analyzeris presented by Master Genfeng, Ma from Chinese Mainland. It has Englishversion named ‘DB Query Analyzer’ and Simplified Chinese versionnamed   .

DB Query Analyzeris one of the few excellent Client Tools in the world for its’ powerfulfunction, friendly interface, easy operation and applicability to everyproduction of RDBMS.

 It lets you query ODBC data sources, author SQLscripts and queries, return query results to a grid or free-form text or afile, retrieve ODBC driver information,  execute multiple SQL scripts or stored procedures simultaneously,save the result to files withhigh efficiency which is as quick as tools provided by database system. What is more, from 6.01, DBQuery Analyzer provides SQL Execute Schedule function to execute SQL scripts incertain time. If you select the option ‘SQL Schedule Reconnect’ in windowconfigurations, the Schedule SQL scripts will be executed as long as theDatabase Server is in use, even though the Database Server had been down. ⑤From version 6.03, withoutchanging any Windows OS settings or configurations, DB Query Analyzer can runon any Microsoft Windows OS directly such as Windows 10, Windows 8, Windows 7,Windows Vista, Windows 2003, Windows XP, Windows 2000, Windows NT, Windows ME,Windows 9X.

In the New products& Tools reviews of programmer second issue of 2007, DB Query Analyzer hadbeen strongly recommended.

DB Query Analyzeris one of the few excellent Client Tools in the world for its’ powerfulfunction, friendly interface, easy operation and applicability to everyproduction of RDBMS.

It lets you queryODBC data sources, author SQL scripts and queries, return query results to agrid or free-form text or a file, retrieve ODBC driver information, executemultiple SQL scripts or stored procedures simultaneously, save the result tofiles with high efficiency which is as quick as tools provided by databasesystem. What is more, from 6.01, DB Query Analyzer provides SQL ExecuteSchedule function to execute SQL scripts in certain time. If you select theoption ‘SQL Schedule Reconnect’ in window configurations, the Schedule SQLscripts will be executed as long as the Database Server is in use, even thoughthe Database Server had been down.

In the New products& Tools reviews of programmer second issue of 2007, DB Query Analyzer hadbeen strongly recommended.

Now the SimplifiedChinese version of DB Query Analyzer is the top 50 database applicationsoftware in the famous software website http://xiazai.zol.com.cn/download_order/sub_550.html .In most case it lies the top 10 and it has been downloaded more than 110,000 times.

82 technicalarticles about DB Query Analyzer have been published or been publishing incomputer journal, BAIDU Library, the CSDN resource or my four blog-websites.

 

 

DB Query Analyzer 6.04 download URL: 

http://xiazai.zol.com.cn/detail/43/420901.shtml

http://www.unitedpowersoft.com/UpFile/DBQueryAnalyzer_English_604.rar

 

My blog are http://blog.csdn.net/magenfeng

http://blog.sina.com.cn/magenfeng

http://user.qzone.qq.com/630414817

Interview with CSDN:  http://www.csdn.net/article/2014-08-09/2821124

 

82 technicalarticles about DB Query Analyzer have been published or been publishing incomputer journal, Baidu Library, the CSDN resource or my four blog-websites.

The following 7articles have been published in computer journals.

Guangdong Highway settlement business analysis systembased on Star Schema in Data Warehousing according to national standardization in Computer Era 7th issue of 2015 in Hangzhou.

DBQuery Analyzer to cancel a running SQL statement in Computer Era 12th issue of 2011 in Hangzhou.

DBQuery Analyzer Returns Information in More Detail While Batch SQL Statement Endof Execution in Computer Programming Skill & Maintenance 24th issue of 2011 in Beijing

TheApplication of the Transactions Control in DB2 with DB Query Analyzer inComputer Programming Skill & Maintenance 22nd issue of 2011 in Beijing.

Howdoes DB Query Analyzer cancel the SQL statement committed to DBMS in ComputerEngineering & Software 6th issue of 2011 in Tianjin.

TheApplication of the Transactions Control in Oracle with DB Query Analyzer inMicrocomputer Applications11th issue of 2011 in Shanghai.

    DB Query Analyzer had been stronglyrecommended In the New products & Tools reviews of programmer 2nd issue of2007.

The following 75 articleshave been published or been publishing in Baidu Library, the CSDN resource ormy four blog-websites.

TheDBMS that DB Query Analyzer Users often use cover all kinds of DBMS have beenpublished.

Howto configure ODBC DSN in Client to access remote DB2 for Windows

How to configure ODBC DSN to access localDB2 for Windows

WhichSQL statement is the trump card to the senior software developer

Exepacker prevent DB Query Analyzer from beging debugged have been published.

16articles from The 1st tip of DB Query Analyze to The 16th skills of DB QueryAnalyzer have been published.

Thetable name must be enclosed in double quotation marks or sqare bracket whileaccessing EXCEL by DB Query Analyzer

How to download the installation packageby ZOL Downer

DBQuery Analyzer, the most excellent Universal database Access tools on anyMicrosoft Windows OS

Demonstrationof DB Query Analyzer 6.03 Installation and Running on Microsoft Windows 8

Installand run DB Query Analyzer 6.04 on Microsoft Windows 10

Anuniversal algorithm design of locating fixed length string

An universal algorithm design of fixed length substring locating的更多相关文章

  1. 算法设计手冊(第2版)读书笔记, Springer - The Algorithm Design Manual, 2ed Steven S.Skiena 2008

    The Algorithm Design Manual, 2ed 跳转至: 导航. 搜索 Springer - The Algorithm Design Manual, 2ed Steven S.Sk ...

  2. DicomIoException: Requested 132 bytes past end of fixed length stream.

    今天在用DicomFile.Open(Stream s)这个接口时,遇到一个异常:      DicomIoException: Requested 132 bytes past end of fix ...

  3. AUTOML --- Machine Learning for Automated Algorithm Design.

    自动算法的机器学习: Machine Learning for Automated Algorithm Design. http://www.ml4aad.org/ AutoML——降低机器学习门槛的 ...

  4. Bash String Manipulation Examples – Length, Substring, Find and Replace--reference

    In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable wi ...

  5. 高级算法设计讲义 Lecture Notes for Advanced Algorithm Design

    (Last modification: 2012-12-17) Textbooks: (1) David Williamson, David Shmoys. The Design of Approxi ...

  6. The new powerful SQL executing schedule monthly or weekly in DB Query Analyzer 7.01

    1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainland. ...

  7. How to generate the complex data regularly to Ministry of Transport of P.R.C by DB Query Analyzer

    How to generate the complex data regularly to Ministry of Transport of P.R.C by DB Query Analyzer 1 ...

  8. A N EAR -D UPLICATE D ETECTION A LGORITHM T O F ACILITATE D OCUMENT C LUSTERING——有时间看看里面的相关研究

    摘自:http://aircconline.com/ijdkp/V4N6/4614ijdkp04.pdf In the syntactical approach we define binary at ...

  9. Save results to different files when executing multi SQL statements in DB Query Analyzer 7.01

        1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainl ...

随机推荐

  1. Mybatis之一级缓存,二级缓存

    一级缓存:Mybatis的一级缓存在session上,只要通过session查过的数据,都会放在session上,下一次再查询相同id的数据,都直接冲缓存中取出来,而不用到数据库里去取了. 二级缓存: ...

  2. 分布式改造剧集之Redis缓存采坑记

    Redis缓存采坑记 ​ 前言 ​ 这个其实应该属于分布式改造剧集中的一集(第一集见前面博客:http://www.cnblogs.com/Kidezyq/p/8748961.html),本来按照顺序 ...

  3. Oracle10g以上sysaux表空间的维护和清理

    SYSAUX表空间在Oracle 10g中引入,其作为SYSTEM表空间的辅助表空间.之前,一些使用独立表空间或系统表空间的数据库组件,现在SYSAUX表空间中存在.通过分离这些组件,减轻了SYSTE ...

  4. 修改hosts不必重启 立刻生效

    打开命令提示符窗口执行以下命令: 显示DNS缓存内容 ipconfig /displaydns 删除DNS缓存内容 ipconfig /flushdns ps.电脑卡的话,先关机再开机(别直接重启)

  5. Java类加载器的工作原理

    Java类加载器的作用就是在运行时加载类.Java类加载器基于三个机制:委托.可见性和单一性.委托机制是指将加载一个类的请求交给父类加载 器,如果这个父类加载器不能够找到或者加载这个类,那么再加载它. ...

  6. Oracle监听启动时报TNS-00507问题

    Linux系统中,启动oracle监听的时候报如下错误: [oracle@centos ~]$ lsnrctl start LSNRCTL :: Copyright (c) , , Oracle. A ...

  7. golang 线程与通道

    首先我们来看线程,在golang里面也叫goroutine 在读这篇文章之前,我们需要了解一下并发与并行.golang的线程是一种并发机制,而不是并行.它们之间的区别大家可以上网搜一下,网上有很多的介 ...

  8. Bootstrap3 代码-代码块

    多行代码可以使用 <pre> 标签.为了正确的展示代码,注意将尖括号做转义处理. <p>Sample text here...</p> <pre>< ...

  9. android 获取栈顶activty的方法总结(兼容API 5.0)

    声明:本文为Dujinyang CSDN原创投稿文章,未经许可,禁止任何形式的转载. 最近5.0\6.0\7.0 安卓系统都陆续上岗了,兼容性和代码更新是个很头疼的问题,这次我们来说下TASK的基础和 ...

  10. 剑指Offer——中国银行面试知识储备

    剑指Offer--中国银行面试知识储备+面试内容 事件介绍 时间:2016.11.23 08:30 地点:北京市海淀区永丰路299号南门(中国银行软件中心) 事件:中国银行面试(中英文面试) 注意事项 ...