Home > Blockchain >  How to select only year from datetimestamp
How to select only year from datetimestamp

Time:05-13

I want to populate my table's column with only year from datetimestamp.

I am using the below query:

date_format(df.startyear,'yyy') as startyear,

But this is returning null values. Can anyone please help?

CodePudding user response:

Given your initial attempt to use DATE_FORMAT, I suspect you're using MySQL. If accurate you can retrieve the YEAR in a couple of ways.

Using YEAR

YEAR(df.startyear) AS startyear

Using EXTRACT

EXTRACT(YEAR FROM df.startyear) AS startyear

CodePudding user response:

You can covert the datetimestamp to datetimeindex and can use timetuple.

If data is your dataframe with time as index.

data.index[0].timetuple().tm_year # for first index;
#or#
year=[data.index[i].timetuple().tm_year for i in range(0,data.shape[0])];

Hope this helps.

  •  Tags:  
  • sql
  • Related