简体   繁体   English

如何从格式化的列日期中获取特定的日期月份或年份

[英]how to get specific day month or year from a column date formated

I have this column named PODate, with date formated. 我有名为PODate的列,其日期已格式化。

  1. "2018-09-15" “2018年9月15日”
  2. "2018-09-16" “2018年9月16日”
  3. "2018-09-17" “2018年9月17日”
  4. "2018-10-19" “2018年10月19日”
  5. "2018-10-20" “二〇一八年十月二十○日”

I want to get a spesific day or month or even year from a column date formated. 我想从格式化的列日期中得到一个特殊的日期或月份甚至年份。 But i dont know what function to use for the query. 但是我不知道该查询使用什么功能。

i want to make a query that get from a chosen month. 我想查询一个从选定月份获得的查询。 or from a week based on what day to choose. 或根据选择的日期从一周开始。

The output will be like only if I chose the 9th month then it will return the date where 9th month only, which is. 仅当我选择第9个月时,输出才会像,然后它将返回仅第9个月的日期。

  1. "2018-09-15" “2018年9月15日”
  2. "2018-09-16" “2018年9月16日”
  3. "2018-09-17" “2018年9月17日”

thankyou 谢谢

You can try this. 你可以试试看

SELECT * FROM yourtable
WHERE CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, date))) = '2018-09-15'

Please check out this discussion 请查看此讨论

You can use regular expressions in MySQL. 您可以在MySQL中使用正则表达式 See the documentation here . 请参阅此处的文档。

SELECT * FROM MyTable WHERE date REGEXP '^.{5}09.*'

This will return all entries from your table where the sixth and seventh characters are specifically '09', ie in your case it will return all entries from your table where your date is somewhere in September. 这将返回表中第六和第七个字符分别为'09'的所有条目,即,对于您的情况,它将返回表中日期为9月的所有条目。

Breaking it down, ^ marks that we want to start looking at the beginning of the string. 将其分解, ^表示我们要开始查看字符串的开头。 . stands for an arbitrary character and the {5} means that whatever occurred before it needs to be repeated five times, ie .{5} stands for five arbitrary characters. 代表任意字符, {5}表示在此之前发生的任何事情都需要重复五次,即.{5}代表五个任意字符。 The .* stands for an arbitrary number ( possibly zero ) of arbitrary characters to follow. .*代表要跟随的任意数量的任意字符( 可能为零 )。

Similar expressions can be crafted for other scenarios. 可以为其他情况设计类似的表达式。

You can use Extract function.. 您可以使用Extract功能。

Define : 定义:

The EXTRACT() function extracts a part from a given date EXTRACT()函数从给定日期提取零件

Example : 范例:

For Month Month

SELECT EXTRACT(MONTH FROM "2018-09-15");

For Week Week

SELECT EXTRACT(WEEK FROM "2018-09-16");

It can extract : 它可以提取:

  1. MINUTE 分钟
  2. HOUR 小时
  3. DAY
  4. WEEK
  5. MONTH
  6. QUARTER 25美分硬币
  7. YEAR
  8. SECOND_MICROSECOND SECOND_MICROSECOND
  9. MINUTE_MICROSECOND MINUTE_MICROSECOND
  10. MINUTE_SECOND MINUTE_SECOND
  11. HOUR_MICROSECOND HOUR_MICROSECOND
  12. HOUR_SECOND HOUR_SECOND
  13. HOUR_MINUTE HOUR_MINUTE
  14. DAY_MICROSECOND DAY_MICROSECOND
  15. DAY_SECOND DAY_SECOND
  16. DAY_MINUTE DAY_MINUTE
  17. DAY_HOUR DAY_HOUR
  18. YEAR_MONTH YEAR_MONTH

You can read here more for documentation Extract() Function 您可以在此处阅读有关文档的更多信息Extract()函数

EDIT : 编辑

You can use this Extract Function in where condition for your case: 您可以使用此Extract Functionwhere为你的情况的条件:

Example : 范例:

select
    your_date
from
    your_table
where
    extract(MONTH FROM your_date) = '9'

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

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