Solved

How to concatenate literal expression with other fields in T-SQL

Posted on 2004-04-30
2
1,387 Views
Last Modified: 2007-12-19
I'm trying to cancatenate the following: a float field truncated to an int, combined with the literal string ' miles' to form the aliased varchar field distance, but getting a casting exception

t-sql line:
CAST(CAST(z.distance AS INT) +  ' miles' AS varchar(9))AS distance

exception:
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the varchar value 'miles' to a column of data type int.

Any ideas as to what I'm doing wrong?
0
Comment
Question by:LloydMc
2 Comments
 
LVL 7

Expert Comment

by:Lori99
Comment Utility
Try this:

convert(varchar(10),cast(distance as int)) + ' miles' as distance
0
 
LVL 7

Accepted Solution

by:
mikkilineni earned 100 total points
Comment Utility

This should work
CAST(CAST(z.distance AS INT) AS varchar(9) +  ' miles' AS distance
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now