Vlad
Vlad

Reputation: 145

TIME_FORMAT Returns NULL

I have a time stored in mysql, the column is set as time, so it reads 13:16:00. When I try to run

SELECT TIME_FORMAT ('alarm_time', '%H:%i) FROM `Alarm`;

I get a NULL, I am trying to get a result of 13:06.

Upvotes: 0

Views: 196

Answers (3)

JuveLeo1906
JuveLeo1906

Reputation: 161

This should do the trick.

select time_format(alarm_time,'%h:%m') as alarm_time
  from alarm;

Upvotes: 0

Stefano Zanini
Stefano Zanini

Reputation: 5916

The quotes around alarm_time are causing that to be interpreted as a string. If you remove them, or surround them with backticks, it will be interpreted as the table's column, which contains a date. These should both work

SELECT TIME_FORMAT (alarm_time, '%H:%i' ) FROM `Alarm`;

or

SELECT TIME_FORMAT (`alarm_time`, '%H:%i' ) FROM `Alarm`;

Regarding the question about comparison you asked in the comments, if I understood correctly you can do that with something like

yourDate between SUBTIME(yourOtherDate, '00:00:30') and ADDTIME(yourOtherDate, '00:00:30') 

or any other interval

Upvotes: 1

Yusuf Hassan
Yusuf Hassan

Reputation: 2013

Why don't you simply use left(value,position).

select left(alarm_time,5) from Alarm; with correct schema name and backticks where ever required should suffice.

The function is self explanatory in its syntax :)

Upvotes: 0

Related Questions