Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the year of a DATE and insert it in a new column?

I have a column in my table which contains DATEs and I need to keep only the YEAR (=> in a NUMBER or INT type)

My idea was to add a column and try something like this :

UPDATE myTable SET newColumn=(
select to_number(to_char(oldColumn.year, 'yyyy')) from myTable)

But I get this error :

single-row subquery returns more than one row

Could you help?!?

like image 889
Piloumpicou Avatar asked Dec 10 '25 21:12

Piloumpicou


1 Answers

You need to change your query to:

UPDATE myTable 
SET newColumn=to_number(to_char(oldColumn.year, 'yyyy'))

This SELECT statement will return all of the rows in the table, from column oldColumn.year, which generates your "single-row subquery returns more than one row" error.

(select to_number(to_char(oldColumn.year, 'yyyy')) from myTable)

like image 133
Radu Gheorghiu Avatar answered Dec 13 '25 14:12

Radu Gheorghiu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!