简体   繁体   English

检查表名是否包含日期

[英]Check if table name contains date

I am trying to find out all tables whose name end with yyyy_mm_dd, eg temp_2017_10_01 , alpha_2016_11_02 . 我试图找出名称以yyyy_mm_dd结尾的所有表,例如temp_2017_10_01alpha_2016_11_02

Currently I am trying to do this: 目前,我正在尝试这样做:

SELECT table_name 
FROM information_Schema.tables
WHERE table_name LIKE '%2017_%'
OR table_name LIKE '%2016_%'
OR table_name LIKE '%2015_%'

What is an elegant way of doing this ? 这样做的一种优雅方法是什么?

You could try using regex like: 您可以尝试使用正则表达式,例如:

SELECT table_name 
FROM information_Schema.tables
WHERE table_name ~* '201[567]_.*'

This would look for years 2015, 2016, and 2017. If you wanted a more generic pattern to find any date you could use this: 这将查找2015、2016和2017年。如果您想使用更通用的模式来查找任何日期,则可以使用以下日期:

SELECT table_name 
FROM information_Schema.tables
WHERE table_name ~* '[0-9]{4}_[0-9]{2}_[0-9]{2}'

Strictly speaking, the above pattern is now too generic, because it allows things like 9999_99_99 which are not valid dates. 严格来说,上述模式现在太通用了,因为它允许9999_99_99日期无效。 But my guess is this is good enough for your use case because you aren't trying to validate table names, just extract them. 但是我的猜测对您的用例来说已经足够了,因为您不是要验证表名,而只是提取它们。

TRY THIS : if following used all the functions are available in the respective database as it's working fine in SQL SERVER OR you can use equivalent function instead 尝试 :如果以下使用的所有功能在相应的数据库中可用,因为它在SQL SERVER可以正常工作, OR您可以使用等效的功能代替

SELECT table_name 
FROM information_Schema.tables
WHERE ISDATE(REPLACE(RIGHT(table_name, 10), '_', '-')) = 1

You can use something like this. 您可以使用类似这样的东西。 using simple substr and to_date functions, will work every where. 使用简单的substr和to_date函数,将可以在任何地方使用。

SELECT table_name
FROM information_Schema.tables WHERE To_Date(SubStr(table_name,Length(table_name)-9,4)||'-'||SubStr(table_name,Length(table_name)-4,2)||'-'||SubStr(table_name,Length(table_name)-1,2),'YYYY-MM-DD') BETWEEN To_Date('01-JAN-2016') AND To_Date('31-DEC-2016');

will give you all the tables between 1st Jan 2016 and 31st Dec 2016 and you can just substitute whatever range you want. 将为您提供2016年1月1日至2016年12月31日之间的所有表格,您可以随意替换任何范围。 If you want all, then you can just give '01-JAN-1950' and '31-DEC-3000' to be sure you can get all of them ;) 如果需要全部,则只需给出“ 01-JAN-1950”和“ 31-DEC-3000”,以确保可以全部获得;)

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

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