I have the below query. This query is supposed to look into my accounts table with my where parameter. That will return all account codes which start with leading 3 letters "DAG". The data return is consistently in the format leading with three letters followed by a number. I then want to get the most largest number. For that I order the string by converting it to Int.
I get this below error:
Msg 207, Level 16, State 1, Line 24
Invalid column name 'AccountCodeWithoutDAG '.
Here is my SQL query.
SELECT TOP 1
REPLACE(AccountCode, 'DAG', '') AS AccountCodeWithoutDAG
FROM
Accounts
WHERE
MangCode = 'ADFG'
ORDER BY
CONVERT(INT, AccountCodeWithoutDAG)
What am I doing wrong?