0

How to to replace ::date and use CAST for below SQL code ?

SELECT to_char(d,   'DD Mon YYYY" to "') || to_char(d+6, 'DD Mon YYYY') AS week 
FROM  (
  SELECT generate_series(d1,d1 + interval '4 weeks',interval '1 week')::date AS d
  FROM  (SELECT date_trunc('week', to_date('January2014', 'MonthYYYY')) AS d1) sub1
  ) sub2

2 Answers 2

1

Casting a value to a date can be done in either of the following ways:

  • value::date
  • CAST(value as date)

If you would like to create your own conversion function you can use the CREATE CAST command.

CREATE CAST documentation

Sign up to request clarification or add additional context in comments.

5 Comments

(CAST(d1,d1 + interval '4 weeks',interval '1 week' AS DATE)) gives error like ERROR: syntax error at or near "," in d1,d
That is because you did not put the cast around a value, but around 3 values: d1, d1 + interval '4 weeks', and interval '1 week'.
Thanks Dwayne , could you please show how this cane be done ?
Try a simpler query and work up to the one you want. Do you know what generate_series(...) does?
got it working as below (CAST (generate_series(d1,d1 + interval '4 weeks',interval '1 week') AS DATE ))
1

Try below commend:

cast(d as date) instead of ::date

For More Reference:http://www.postgresql.org/message-id/[email protected]

1 Comment

(CAST(d1,d1 + interval '4 weeks',interval '1 week' AS DATE)) gives error like ERROR: syntax error at or near "," in d1,d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.