Home > database >  Convert date to dateTtime format in SQL
Convert date to dateTtime format in SQL

Time:11-05

I am trying to convert a date column (ie. 2012-10-02) to the first day of the year with time (ie. 2012-01-01T00:00:00) in sql.

Is there a way to do so in the SELECT query?

CodePudding user response:

Look at the YEAR() function.

It would allow you to extract just the year, and then just as the date and time you need.

CodePudding user response:

for BigQuery use below

select timestamp_trunc('2012-10-02', year)      

with output

2012-01-01 00:00:00 UTC     

Note - if you column is of date type - the output will be

2012-01-01T00:00:00      

and finally, you can use datetime_trunc instead of timestamp_trunc and you will get expected result - 2012-01-01T00:00:00

  •  Tags:  
  • sql
  • Related