简体   繁体   English

DB2选择不同的日期格式

[英]DB2 select with different date format

My problem is that in an ancient database there are two tables that I need to query for matching rows based on date. 我的问题是,在一个古老的数据库中,我需要根据日期查询两个表以匹配行。 Only in one table date is represented as YYYYMM as a decimal(6) and in the other as YYYY-MM-DD as a date. 仅在一个表日期中将YYYYMM表示为十进制(6),而在另一个表中则将YYYY-MM-DD表示为日期。

How do I join these two tables together? 如何将这两个表连接在一起?

I am perfectly happy searching on any day or day 01. 我非常高兴在第一天或第一天进行搜索。

You can format that date as YYYYMM using TO_CHAR or VARCHAR_FORMAT , then join the two tables together. 您可以使用TO_CHARVARCHAR_FORMAT将该日期格式设置为YYYYMM,然后将两个表连接在一起。

Assuming table A has the date field in col1, and table B has the decimal(6) field in col2, it would look like this: 假设表A在col1中具有日期字段,而表B在col2中具有小数(6)字段,则它看起来像这样:

select *
  from A
    join B on dec(varchar_format(a.col1, 'YYYYMM'),6,0) = b.col2

You can perform join on those two tables. 您可以在这两个表上执行联接。 Suppose first table where date is stored as decimal(6) is A in column col1 and another table be B with date stored as column col2.The query would be something like below : 假设第一个将日期存储为小数(6)的表是col1列中的A,另一个表是B,并将日期存储为col2列中的表。

    SELECT * FROM A, B
    WHERE INT(A.col1) = INT(SUBSTR(B.col2,1,4)|| SUBSTR(B.col2,6,2)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM