在PostgreSQL中查找字符串中特定字符的索引

时间:2014-08-26 08:19:37

标签: sql postgresql

以PostgreSQL格式编写给定问题的通用查询?

"901241,924685,924692,924670."
"901067,901069,901071,901071,901168,901168,901886,901891."
"PA,QW,NA,NS,0P,9P,NY,NY.N05-NH,NG,NC,NF.RI8-05,08,13,16."
"KC,K9,LA,L2.N05-K0,K1,K3,K6,K8,KE,LS,LZ,LU.RI8-05,08,13,16."

如何在PostgreSQL中以这种格式编写上述字符串的泛型查询:

(BETWEEN PA AND OW) AND (BETWEEN NA AND NS)........
(BETWEEN 901241 AND 924685) AND (BETWEEN 924692 AND 924679)

到目前为止我试过了:

SELECT CASE
          WHEN SUBSTRING (gl_in2, 1, 1) = 'R'
          THEN
                '_'
             || SUBSTRING (gl_in2, 2, 2)
             || ' BETWEEN '
             || SUBSTRING (gl_in2, 5, POSITION (',' IN gl_in2) - 5)
             || ' AND '
             || SUBSTRING (gl_in2,
                           POSITION (',' IN gl_in2) + 1,
                           POSITION (',' IN gl_in2) - 5)
          ELSE
             NULL
       END
  FROM updatenew RESULT
 "_BC (BETWEEN 902103 AND 902136) AND

1 个答案:

答案 0 :(得分:0)

添加表格代码

CREATE TABLE "codes"(
 "id" Serial NOT NULL,
 "code" Character varying(50),
 "order_1" Integer
)
WITH (OIDS=FALSE)
; 

ALTER TABLE "codes" ADD CONSTRAINT "PK_codes" PRIMARY KEY ("id")
;

ALTER TABLE "codes" ADD CONSTRAINT "order_1_unique" UNIQUE ("order_1")
;

ALTER TABLE "codes" ADD CONSTRAINT "code_unique" UNIQUE ("code")
;

将此代码存储在上面提到的表格中。 使用PK / FK连接到代码表。 在order_1列之间使用(您可以添加更多订单规则 - 通过添加新列)