【问题标题】:How to find if there is a Date Value in your column如何查找列中是否有日期值
【发布时间】:2020-01-16 22:30:24
【问题描述】:

我有一个要求,我需要在我的列值中找到日期。

该列有大约 300 万条记录,由特殊字符、数字、文本和日期组成。我可以做些什么来找到日期函数并将其从 Teradata 列中删除。 正则表达式函数也可以提供帮助。

我不知道日期函数是什么格式的。

【问题讨论】:

  • 如果你不知道你的数据是什么格式,第一步是不要选择一个工具来找到它。第一步是查看您的数据,以便您知道。
  • 正如 Jai 所写,您如何识别日期?必须有一个特殊的格式。您想从输出中删除日期部分还是屏蔽它?
  • @Jai Jeffryes 同意你的观点,但我有大约 3300 万条记录,日期值可能是大约 50 条记录,这就是挑战,这就是我不知道日期格式的原因
  • 但是你怎么知道有约会?当您查看数据时,您会如何发现该日期?类似于由一个字符分隔的 2/2/4 位数字组?

标签: regex teradata


【解决方案1】:

鉴于您不知道数据的内容,您的工作是探索性的,这意味着您的第一步是询问数据。尽可能多地寻找日期格式。你会摆脱最常见的。我从用 R 编写的方法开始。你可以用你选择的语言做类似的事情。

您永远不会知道有多少日期是您没有想到的格式。但是,您可以尝试一些开放式搜索,这些搜索可能会消除其中的一些,甚至匹配您希望查看但不符合任何格式的有缺陷的数据。我包含了一个搜索,它只在搜索字符串中的任意位置查找月份缩写。

为了制作这个演示,我必须在每次添加新的 RegEx 进行测试时剪切并粘贴到测试用例中。编写测试台将是一个令人满意的策略。这可以是一个表格或向量,用于保存您要执行的每个 RegEx 模式,然后是一个简单的循环来迭代运行它们。

这是我的 R 代码。

library(stringr)

# Exploratory date patterns.
# 04/04/2020
fmt1 <- "\\d{2}/\\d{2}/\\d{4}"
# 4/4/2020
fmt2 <- "\\d{1,2}/\\d{1,2}/\\d{4}"
# Apr. 1, 2020
# If I have a large list, I like building it like this so the part I maintain
# is human readable. You can implement this kind of approach in whatever
# language you use.
month_3ltr <- c("Jan", "Feb", "Mar", "Apr", "May")
month_pat <- paste(month_3ltr, "\\.", sep = "")
month_pat <- paste(month_pat, collapse = "|")
fmt3 <- str_c("^(", month_pat, ")\\s\\d{1,2},\\s\\d{4}")
# Review pattern
fmt3
fmt4 <- str_c(month_pat) # Find anything that just mentions a month.
# Keep going, as many formats as you can think of.

# Test data
str1 <- "04/01/2020"
str2 <- "4/1/2020"
str3 <- "Apr. 1, 2020"
str4 <- "Defective data: Apr. First, 2020"
str5 <- "Fake news"

# Test cases
# Format 1
str_extract(str1, fmt1)
str_extract(str2, fmt1)
str_extract(str3, fmt1)
str_extract(str4, fmt1)
str_extract(str5, fmt1)

# Format 2
str_extract(str1, fmt2)
str_extract(str2, fmt2)
str_extract(str3, fmt2)
str_extract(str4, fmt2)
str_extract(str5, fmt2)

# Format 3
str_extract(str1, fmt3)
str_extract(str2, fmt3)
str_extract(str3, fmt3)
str_extract(str4, fmt3)
str_extract(str5, fmt3)

# Format 4
str_extract(str1, fmt4)
str_extract(str2, fmt4)
str_extract(str3, fmt4)
str_extract(str4, fmt4)
str_extract(str5, fmt4)

我的结果:

> # Test cases
> # Format 1
> str_extract(str1, fmt1)
[1] "04/01/2020"
> str_extract(str2, fmt1)
[1] NA
> str_extract(str3, fmt1)
[1] NA
> str_extract(str4, fmt1)
[1] NA
> str_extract(str5, fmt1)
[1] NA
> 
> # Format 2
> str_extract(str1, fmt2)
[1] "04/01/2020"
> str_extract(str2, fmt2)
[1] "4/1/2020"
> str_extract(str3, fmt2)
[1] NA
> str_extract(str4, fmt2)
[1] NA
> str_extract(str5, fmt2)
[1] NA
> 
> # Format 3
> str_extract(str1, fmt3)
[1] NA
> str_extract(str2, fmt3)
[1] NA
> str_extract(str3, fmt3)
[1] "Apr. 1, 2020"
> str_extract(str4, fmt3)
[1] NA
> str_extract(str5, fmt3)
[1] NA
> 
> # Format 4
> str_extract(str1, fmt4)
[1] NA
> str_extract(str2, fmt4)
[1] NA
> str_extract(str3, fmt4)
[1] "Apr."
> str_extract(str4, fmt4)
[1] "Apr."
> str_extract(str5, fmt4)
[1] NA

【讨论】:

    猜你喜欢
    • 2010-11-20
    • 1970-01-01
    • 2023-02-22
    • 2023-03-27
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    相关资源
    最近更新 更多