views:

48

answers:

3

Can someone help me with SQL Server CAST to text datatype? I'm trying to get the long text to display using the CAST function, but it wont work if I am retrieving from more than 1 table. Perhaps I've created the statement incorrectly.

$from = 'RealEstate AS RE, Models AS M, Catalogue AS C';

$select = 'CAST(RE.Details AS TEXT) AS RE.Details, RE.Postcode, RE.Suburb, 
    M.ModelName, M.ModelID, RE.HouseID, RE.ContactName, RE.ContactPhone, RE.PlanName, 
    RE.Address, RE.Street, RE.Price, RE.StreetNo, RE.CatID, RE.Fixed';


$query = mssql_query("SELECT DISTINCT $select 
                      FROM RealEstate AS RE, Models AS M 
                      WHERE RE.HouseID=$hid 
                      AND RE.ModelID=M.ModelID");
A: 

I'm pretty sure aliases can't contain a period (offender: RE.Details). Try calling it Details instead (or enclose the alias in quotes (backticks for mysql) if you want to keep the period, but I'm guessing you don't).

lc
both the $from table Model and RealEstate carries the same "Details" field. That's why I used the period to call only the Details from the RealEstate table.you're right about taking out the period. It works but then again in a single table, there's no need for me to do the RealEstate AS RE call.I can however make this into a double query, but i just want to try to make it as short as possible.
Honshi
@Honshi: Not for the column alias - see my query as an example. If you really need to see a column named "re.details", backticks only work for MySQL - the ANSI standard is to use double quotes.
OMG Ponies
@OMG Ponies I'm trying out your code. still not working. I thought the tables are case-sensitive? so i converted all the codes to the right case but still not working. thanks for your suggestion though
Honshi
Sorry, misread the question and thought it was MySQL. Changed it to say "quotes" instead.
lc
+1  A: 

My understanding is that you can not concatenate strings within the mssql_query block - I don't see the need for it really, based on what you provided. If you want to concatenate a dynamic SQL statement, do it as such before sending the statement to the mssql_query function:

$select = 'SELECT DISTINCT CAST(re.details AS TEXT) as details,
                                         re.postcode,
                                         re.suburb,
                                         m.modelname,
                                         m.modelid,
                                         re.houseid,
                                         re.contactname,
                                         re.contactphone,
                                         re.planname,
                                         re.address,
                                         re.street,
                                         re.price,
                                         re.streetno,
                                         re.catid,
                                         re.fixed
                               FROM REALESTATE re
                               JOIN MODELS m ON m.modelid = re.modelid
                              WHERE re.houseid = $hid"
$query = mssql_query($select);
OMG Ponies
@Honshi: Table names are not case sensitive, uppercase is just my coding style. Without failure details, I can't help you.
OMG Ponies
A: 
$queryDetails = mssql_query("SELECT CAST(Details AS TEXT) AS Details FROM RealEstate WHERE HouseID=$hid");

I made another query from it. Its probably a redundant call. I'm more familiar with mySQL, after using msSQL i realize that it is quite troublesome to manipulate it. especially when the LIMIT function is not available for pagination.

Honshi