Home > Software engineering >  MYSQL - Incorrect syntax near the keyword 'AS' in CAST function
MYSQL - Incorrect syntax near the keyword 'AS' in CAST function

Time:12-06

I am typing the following query in mysql server

Query the greatest value of the Northern Latitudes (LAT_N) from STATION that is less than 137.2345. Truncate your answer to 4 decimal places.

I keep coming across the following error message when I type the following query: Msg 156, Level 15, State 1, Server dbrank-tsql, Line 3Incorrect syntax near the keyword 'AS'. How do I fix this error?

SELECT
        CAST((MAX(LAT_N) AS DECIMAL(10,4))
FROM    STATION
WHERE   LAT_N < 137.2345
;

I can resolve this query using the CONVERT function as follows:

SELECT
        CONVERT(DECIMAL (10, 4), MAX(LAT_N))
FROM    STATION
WHERE   LAT_N < 137.2345
;

But can someone help me figure out what is incorrect in the above query when I use CAST instead of it?

CodePudding user response:

The issue is that the AS keyword is placed in the wrong location. You need to make it

SELECT CAST(MAX(LAT_N) AS DECIMAL(10, 4))
FROM STATION
WHERE LAT_N < 137.2345;```

CodePudding user response:

You've got an extra open parenthesis, so you are passing (MAX(LAT_N) AS DECIMAL(10,4)) as the first argent to cast (with a missing close parenthesis) instead of having AS after the first parameter.

  • Related