0

I have this column named PODate, with date formated.

  1. "2018-09-15"
  2. "2018-09-16"
  3. "2018-09-17"
  4. "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.

  1. "2018-09-15"
  2. "2018-09-16"
  3. "2018-09-17"

thankyou

MrH
  • 15
  • 6

3 Answers3

0

You can try this.

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

Please check out this discussion

PartTimeNerd
  • 305
  • 1
  • 2
  • 20
0

You can use regular expressions in 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', i.e. in your case it will return all entries from your table where your date is somewhere in September.

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, i.e. .{5} stands for five arbitrary characters. The .* stands for an arbitrary number (possibly zero) of arbitrary characters to follow.

Similar expressions can be crafted for other scenarios.

JMoravitz
  • 722
  • 1
  • 5
  • 11
0

You can use Extract function..

Define :

The EXTRACT() function extracts a part from a given date

Example :

For Month

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

For Week

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

It can extract :

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

You can read here more for documentation Extract() Function

EDIT :

You can use this Extract Function in where condition for your case:

Example :

select
    your_date
from
    your_table
where
    extract(MONTH FROM your_date) = '9'
dwir182
  • 1,539
  • 10
  • 20