I generate a view from this:
create or replace view datetoday as
select to_char(dt, 'yyyy-mm-dd') as date, to_char(dt, 'Day') as weekday 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;
It gives me the weekday info as text
type. Then I use:
select date::date, weekday::varchar from datetoday;
Now the table is like
2013-3-1 Friday
2013-3-2 Saturday
If I want to choose the entry:
select * from datetoday where weekday='Friday'
to change it from text
to character varying
.
It seems that the length is fixed is not according to each word length.
For example 'Friday' should have length 6 and Wednesday length 9.
How can I change this, let the length be the actual length of the word?
Because later I will compare this table's weekday
column to another table's weekday
column. Like
where a.weekday=b.weekday
The other weekday is from user from jsp, so the length varies.
Now the length is fixed, the comparison fails.
The pattern
'Day'
is blank-padded to the right, making all days 9 characters long. Use theFM
Template Pattern Modifier to remove any padding:Also demonstrating
generate_series()
for timestamps. One less query level.If you need an actual
date
in the view, make it an actual typedate
, don't convert totext
and back.And don't use the basic type name
date
as column name. Usingday
instead.And I would just use
text
for the text. No point in converting tovarchar
.