开发者

Not able to merge two columns in SQL Server while printing

开发者 https://www.devze.com 2023-03-12 08:45 出处:网络
I want to print two columns of a table in a desired format. I tried with the below format but it\'s giving error saying

I want to print two columns of a table in a desired format. I tried with the below format but it's giving error saying

开发者_运维百科

Error converting data type varchar to bigint.

This is my query:

Select  ClassID + ' .' + ClassSectionID AS Class 
from ClassSectionMaster  
ORDER BY Class

Can anybody tell me how to do this?


SQL Server doesn't like the fact you're trying to combine a column of type bigint (ClassID/ClassSectionID) with a string, ' .'

I'm assuming you want the output to be formatted like "9.6" and that strings are acceptable.

You have to use CAST or CONVERT to change the datatype to VARCHAR(x) to do this sort of combination.

SELECT CAST(ClassID AS VARCHAR(x)) + ' .' + CAST(ClassSectionID AS VARCHAR(y)) 
    AS Class
FROM ClassSectionMaster ORDER BY Class


Select (Convert(nvarchar(10), ClassID) + ' .' + Convert(nvarchar(10), ClassSectionID)) AS Class 
from ClassSectionMaster  
ORDER BY Class
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号