userJT
userJT

Reputation: 11934

RedShift: How to cast integer with year into a date?

Assume a user tries to cast a number (year of birth) into a date.

select year_of_birth, cast(year_of_birth as date) from visit_occurrence 

And gets this error :

cannot cast type smallint to date

What is the proper way? e.g., cast(cast(YOB as string)+'-01-01' as date) does not work either.

Upvotes: 1

Views: 5687

Answers (1)

Hasan Ramezani
Hasan Ramezani

Reputation: 5194

use

select year_of_birth, to_date(cast(year_of_birth as text), 'YYYY') from visit_occurrence ;

Upvotes: 5

Related Questions