user1235446
user1235446

Reputation: 57

HAVING LENGTH(field) in MySQL

I'm trying to select rows with field timestamp, which has a length shorter than 16 characters. I've tried the following:

SELECT LENGTH(timestamp), id
FROM my_table
HAVING LENGTH(timestamp) < 16

But I get this error:

#1054 - Unknown column 'timestamp' in 'having clause'

Any suggestions?

Upvotes: 2

Views: 8791

Answers (2)

Yuck
Yuck

Reputation: 50835

I think you want:

SELECT LENGTH(`timestamp`), id
FROM my_table
WHERE LENGTH(`timestamp`) < 16

Or if you're actually trying to group the results...

SELECT LENGTH(`timestamp`)
FROM my_table
GROUP BY LENGTH(`timestamp`)
HAVING LENGTH(`timestamp`) < 16

Note the backticks (`) in each example to escape the column name.

Upvotes: 9

silly
silly

Reputation: 7887

I think this will be worked to:

SELECT DISTINCT
    LENGTH(timestamp)
FROM my_table
WHERE LENGTH(timestamp) < 16

Upvotes: 0

Related Questions