使用DB2 for z / OS 10,我在一个表中有一列(ADDRESS3),该表在一个列中包含城市,州和邮政编码。但是,格式有些“自由格式”。 (请参见下面的代码)
我可以通过逗号前的所有内容来成功解析城市。仅使用DB2 SQL,如何解析状态和邮政编码?
$(".month").click(function() {
var click = $(this);
var month = click.val();
var year = $("#years").val();
$.ajax({
url: "{{ path('av_platform_saisie') }}",
type: 'POST',
data: { 'month': month,
'year': year
},
success: function (data) {
alert('OK');
},
error : function(jqXHR, textStatus, errorThrown){}
});
});
最终,我希望查询结果看起来像这样:
SELECT ADDRESS3,
TRIM(SUBSTR(ADDRESS3,1,LOCATE(',', ADDRESS3)-1)) AS CITY
FROM MYTABLE
ADDRESS3 CITY
---------------------------------- -------------
GRANADA HILLS ,CA 91344 GRANADA HILLS
SIMI VALLEY ,CA 93065 SIMI VALLEY
BUENA PARK ,CA 90621 BUENA PARK
SHERMAN OAKS ,CA 91423 SHERMAN OAKS
GLENDALE ,CA 91203-2089 GLENDALE
VENTURA ,CA 93002 VENTURA
HAWTHORNE ,CA 90250 HAWTHORNE
PASADENA ,CA 91185-2594 PASADENA
BEVERLY HILLS ,CA 90211 BEVERLY HILLS
ARCADIA ,CA 91007 ARCADIA
WALNUT CREEK ,CA 94596 WALNUT CREEK
INDEPENDANCE ,CA 93526 INDEPENDANCE
LOS ANGELES ,CA 90017 LOS ANGELES
谢谢!
答案 0 :(得分:0)
根据您的代码,如果状态为2个字符,而邮政编码为5个字符:
SELECT
ADDRESS3,
TRIM(SUBSTR(ADDRESS3, 1, LOCATE(',', ADDRESS3)-1)) AS CITY,
TRIM(SUBSTR(ADDRESS3, LOCATE(',', ADDRESS3) + 1), 2) AS STATE,
TRIM(SUBSTR(ADDRESS3, LOCATE(',', ADDRESS3) + 4), 5) AS ZIP
FROM MYTABLE
答案 1 :(得分:0)
我认为您需要假设格式始终相同。
SELECT
TRIM(
SUBSTR(
SUBSTR(MYTABLE.A, LOCATE(',', MYTABLE.A) + 1)
, 1
, POSITION(' ' , SUBSTR(MYTABLE.A, LOCATE(',', MYTABLE.A) + 1))
)
) AS STATE
, TRIM(
SUBSTR(
SUBSTR(MYTABLE.A, LOCATE(',', MYTABLE.A) + 1)
, POSITION(' ' , SUBSTR(MYTABLE.A, LOCATE(',', MYTABLE.A) + 1))
)
) AS ZIP
FROM MYTABLE
将演示结果转换为具有这些值的结果
测试数据
VALUES
('GRANADA HILLS ,CA 91344')
, ('SIMI VALLEY ,CA 93065')
, ('GLENDALE ,CA 91203-2089')
, ('SIMI VALLEY ,CA 93065')
, ('SIMI VALLEY ,CA 93065')
, ('GLENDALE ,CA 91203-2089')
结果
STATE ZIP
CA 91344
CA 93065
CA 91203-2089
CA 93065
CA 93065
CA 91203-2089
请参阅demo
答案 2 :(得分:0)
有趣的是,这也起作用。
SELECT TRIM(SUBSTR(ADDRESS3, 1, locate(',', ADDRESS3) - 1)) AS city,
SUBSTR(TRIM(SUBSTR(ADDRESS3, locate(',', ADDRESS3) + 1)), 1, 2) state,
SUBSTR(TRIM(SUBSTR(TRIM(SUBSTR(ADDRESS3, locate(',', ADDRESS3) + 1)), 3)), 1, 5) zip
FROM MyTable