I'm getting the error: Incorrect syntax near the keyword 'AS'. I'm trying to select a column from my MSSQL database while NOT SELECTING the first character of that field. Below is the code (Within PHP):
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) FROM Table WHERE [Column] ='".$search."' AS Column2";
try this:
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) AS Column2 FROM Table WHERE [Column] ='".$search."' ";
You need to put AS
after the SELECT
part of your query never put it at the end of your query. Try to do it like this
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) AS Column2 FROM Table WHERE [Column] ='".$search."' ";
First of all. Please be careful. Your code looks vulnerable to an SQL injection. Better use paramterized queries.
The second "AS" at the end is not needed.
Try it that way:
SELECT RIGHT(Revlv, LEN(Revlv) - 1) AS Revlv2 FROM table_name WHERE [Objkt] ='".$search."'
Or better yet:
SELECT RIGHT(Revlv, LEN(Revlv) - 1) AS column_name FROM table_name WHERE [Objkt] = ?
Read more about parameterized queries here
Also take care when the Revlv column only contains an empty string. The query will fail in that case.
As all answers simply fix the syntax instead of fixing the logic:
There's no need to use RIGHT + LEN to extract everything but the first character, simply use
substring(Revlv from 2) AS Revlv2 -- Standard SQL to extract everything after the first character
As SQL Server has a slighty different syntax:
substring(Revlv, 2, 8000) AS Revlv2 -- T-SQL to extract everything after the first character
try this.
$sql="SELECT RIGHT(Revlv, LEN(Revlv)-1) AS Revlv2
FROM tablename where['objkt']='$search'";