How to combine two field data into one field display in SQL Server (field and field add special symbols)

Source: Internet
Author: User

Before, when I was doing statistics, I needed a field to show the order quantity and order amount for a month, and to combine it into a single field in parentheses.

The result of the statistic is probably this, first we create some simulation data

---create order form---CREATE TABLE Omsorder (UidintIdentity1,1) primary key, Organizename varchar ( -) notNULL, OrderCountintNotNULL, OrdermoneyfloatNotNULL)--Add analog data---INSERT INTO Omsorder (Organizename,ordercount,ordermoney) VALUES ('Sync Clinic', -,54231),('Mercy Clinic', About,12123),('Longhua Clinic', the,35212)

This is the result of the statistics, but we need the order quantity (order Amount) to display this way

The composition syntax is converted with the cast () function, with the following syntax

-- Combination Syntax--- Select (Castasvarchar('+ '+ Cast  as varchar ()) + ' )') from    tableName

1. Use parentheses to combine two fields into one and display

--1. Use () to combine two fields--SelectA.organizename, (CAST(OrderCount as varchar( -))+'('+CAST(Ordermoney as varchar( -))+'yuan)') asOrderData fromOmsorder asA

When combined, the returned data is as follows:

2. Use---number to implement two fields combined into one field

--2, with--two field combinations--SelectA.organizename, (CAST(OrderCount as varchar( -))+'--'+CAST(Ordermoney as varchar( -))+'Yuan') asOrderData fromOmsorder asA

When combined, the returned data is as follows:

Ps:

1) The data type of the conversion field is converted directly to the varchar () type, whether int, float, varchar, etc.

2) The cast () function is converted and concatenated with the "+" sign, and the whole as a whole is returned as a field in parentheses.

How to combine two field data into one field display in SQL Server (field and field add special symbols)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.