user3382017
user3382017

Reputation: 245

Get day of week from time period

I have two date from user input. I want to get from jsp these two dates and create a view which shows each day with their own weekday:

2013-3-1 MONDAY
2013-3-2 TUESDAY
2013-3-3 WEDNESDAY 

User inputs date and gives to jsp, say:

where start='2013-3-1' and end='2013-3-3'

How to do this?

Upvotes: 0

Views: 184

Answers (2)

Erwin Brandstetter
Erwin Brandstetter

Reputation: 658927

This can be simpler:

SELECT to_char(d, 'YYYY-MM-DD') AS day_txt
     , to_char(d, 'FMDay')      AS weekday
FROM   generate_series('2013-03-01'::date
                     , '2013-03-03'::date
                     , interval '1 day') d;

More details under this later question:
Remove blank-padding from to_char() output

Generally it is better to operate with actual date types, not with text representations, but I did not understand the details or your requirements.

Upvotes: 1

ntalbs
ntalbs

Reputation: 29458

Try this:

select to_char(dt, 'yyyy-mm-dd') as date, to_char(dt, 'Day') as day
from (select ('2013-03-01'::date + i) dt
      from generate_series(0,'2013-03-03'::date - '2013-03-01'::date) as t(i)) as t;

You should parameterize the date part of query.

There are a couple of points worth remember in this query.

  • date calculation: subtract two date become the number of dates between two dates.
  • generate_series(0, n) generates 0, 1, ... n

Upvotes: 0

Related Questions