1

I have a column called tidpunkt in the database that stores the dates in timestamp formats. How do I extract only the year with SQL? I still want the extracted year to be in timestamp.

I've tried like this:

SELECT EXTRACT(YEAR FROM tidpunkt) as tidpunkt FROM ringupp

But it does not work. Am I doing something wrong?

4 Answers 4

4
SELECT YEAR(tidpunkt) as tidpunkt FROM ringupp

Do this

SELECT FROM_UNIXTIME(tidpunkt , '%Y') AS tidpunkt FROM ringupp

MySQL FROM_UNIXTIME() returns a date /datetime from a version of unix_timestamp. The return value is in ‘YYYYY-MM-DD HH:MM:SS’ .

See DOCUMENTATION . Also see this Example

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

3 Comments

@m hasan: It does not work. Nothing is returned.. The timestamp in the database looks like this: 1370601583
ok , so your timestamp is in unix format..check edited answer
@m hasan: Thank you very much. How do I get the rows that has the year 2014? SELECT FROM_UNIXTIME(tidpunkt, '%Y') AS tidpunkt WHERE tidpunkt == "CURRENT YEAR IN TIMESTAMP"; I dont know how to get the current year in the SQL.
1

Try this:

if you require 4 digit year

SELECT DATEPART('yyyy',tidpunkt) as tidpunkt FROM ringupp

and if 2 digit year is required:

SELECT DATEPART('yy',tidpunkt) as tidpunkt FROM ringupp

Documentation here

Comments

0

date_format is your friend with mysql
see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

SELECT DATE_FORMAT( tidpunkt, '%Y' ) as tidpunkt FROM ringupp

Comments

0
SELECT EXTRACT(YEAR FROM tidpunkt) FROM ringupp

Comments

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.