Remove empty line from to_char () output

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; 

He gives me weekday information as text . Then I use:

 select date::date, weekday::varchar from datetoday; 

Now the table is similar to

 2013-3-1 Friday 2013-3-2 Saturday 

If I want to select an entry:

 select * from datetoday where weekday='Friday' 

to change it from text to character varying .

It seems that the length is not fixed for each word length.
For example, "Friday" should have a length of 6 and a length of Wednesday 9.
How can I change this, let the length be the actual word length?

Because later I will compare the weekday column with another weekday column. how

 where a.weekday=b.weekday 

Another day of the week is from the user from jsp, so the length varies.
Now the length is fixed, the comparison is not performed.

+2
sql postgresql database-design date-format generate-series
Mar 07 '14 at 5:24
source share
1 answer

The 'Day' template is empty, filled to the right, making all days 9 characters long. Use the FM wildcard template modifier to remove any add-on:

 SELECT d::date AS day , to_char(d, 'yyyy-mm-dd') AS day_text , to_char(d, ' FM Day') AS weekday FROM generate_series('2013-03-01'::date , '2013-03-07'::date , interval '1 day') d; 

Also demonstrates generate_series() for timestamps . Another level of queries.
If you need the actual date in the view, make it the actual type of date , do not convert it to text and vice versa.
And do not use the name of the base type date as the column name. Use day instead.
And I would just use text for the text. It makes no sense to convert to varchar .

+7
Mar 07 '14 at 5:52
source share




All Articles