I have two columns in a SQL table, fooId
(int) and fooName
(varchar).
Is there a way to select them both as one column with a space between them?
select fooId + ' ' + fooName as fooEntity from mytable
They're different types so I'm getting an error.
This field will be databound directly in a control in the web app.
SQL Server 2008
(I'm a bit of a sql beginner)
In the earlier version of SQL Server, we usually use CONVERT function to convert int into varchar and then concatenate it. Given below is the script. Solution 2: In this solution, we will use CONCAT function (a newly shipped function in SQL Server 2012) to convert int into varchar then concatenate it.
SQL Server's CAST() and CONVERT() methods can be used to convert VARCHAR to INT.
Solution. TSQL provides 2 ways to concatenate data, the + sign and the new CONCAT() function. This tip will cover the differences in the two, so you can achieve the expected behavior in your code. The way most us are used to concatenating data together is using the + sign.
SELECT SOME_OTHER_COLUMN, CONCAT(FIRSTNAME, ',', LASTNAME) AS FIRSTNAME FROM `customer`; Using * means, in your results you want all the columns of the table. In your case * will also include FIRSTNAME . You are then concatenating some columns and using alias of FIRSTNAME .
String concatenation is different between databases, so it helps to know which database because you need to know:
SQL Server doesn't do implicit conversion of numeric into string values:
SELECT CAST(fooid AS VARCHAR(10)) + ' ' + fooname
...so you need to use CAST (or CONVERT) to explicitly change the data type to a text based data type.
For Oracle & PostgreSQL, use the double pipe to concatenate strings:
SELECT fooid || ' ' || fooname
For MySQL, you can use the CONCAT function:
SELECT CONCAT(fooid, ' ', fooname)
Try this:
SELECT Convert( foold, SQL_CHAR ) + ' ' + fooName FROM mytable
or
SELECT Cast( foold AS SQL_CHAR(10) ) + ' ' + fooName FROM mytable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With