Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date as day of week

I have a table which has amongst others a date column servdate.

I use the following query to get me all jobs from within the past week ( a week starts from monday):

SELECT * FROM tb1 WHERE servdate BETWEEN date('now', 'Weekday 1', '-21 days') AND date('now')

I want the query to work exactly the same but instead to return the servdate fields as their corresponding day of the week. For example, "monday", instead of "2010-11-28".

Is this possible?

like image 991
brux Avatar asked Nov 30 '10 22:11

brux


People also ask

How do I format date with day in Excel?

Select the cells you want to format. Press Control+1 or Command+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.


1 Answers

You can use an ancillary table as wallyk suggested; or you can use a case expression:

select _id, busnum, 
  case cast (strftime('%w', servdate) as integer)
  when 0 then 'Sunday'
  when 1 then 'Monday'
  when 2 then 'Tuesday'
  when 3 then 'Wednesday'
  when 4 then 'Thursday'
  when 5 then 'Friday'
  else 'Saturday' end as servdayofweek
from tb1
where ...
like image 131
Doug Currie Avatar answered Sep 18 '22 15:09

Doug Currie