插入" $"在SELECT语句SQL Server中

时间:2014-11-13 21:17:15

标签: sql sql-server

我有一个SELECT声明,显示有关订单的详细信息。

使用以下代码

SELECT Orders.OrderID, Orders.invoiceID, Items.itemName AS 'Item Name', Orders.quantity, DATENAME(mm, Orders.OrderDate) + ' ' + DATENAME(dd, Orders.OrderDate) + ', ' + DATENAME(yyyy, Orders.OrderDate) AS 'Order Date', (Orders.price * Orders.quantity) AS 'Total', Orders.delivered
    FROM Orders
    INNER JOIN Items
    ON Orders.itemID = Items.itemID
    ORDER BY Orders.orderID, Items.itemID ASC

一切正常,但我不确定如何获得" $"显示"总数"中数字的左侧字段。

所有帮助将不胜感激。

谢谢, 布赖恩

3 个答案:

答案 0 :(得分:1)

你可以明确地连接它:

SELECT     Orders.OrderID, 
           Orders.invoiceID, 
           Items.itemName AS 'Item Name', 
           Orders.quantity, 
           DATENAME(mm, Orders.OrderDate) + ' ' + DATENAME(dd, Orders.OrderDate) + ', ' + DATENAME(yyyy, Orders.OrderDate) AS 'Order Date', 
           '$' + (CAST (Orders.price * Orders.quantity) AS VARCHAR(100)) AS 'Total', 
           Orders.delivered
FROM       Orders
INNER JOIN Items ON Orders.itemID = Items.itemID
ORDER BY   Orders.orderID, Items.itemID ASC

答案 1 :(得分:1)

试试这个

SELECT Orders.OrderID, 
       Orders.invoiceID, 
       Items.itemName AS 'Item Name', 
       Orders.quantity, 
       DATENAME(mm, Orders.OrderDate) + ' ' + DATENAME(dd, Orders.OrderDate) + ', ' + DATENAME(yyyy, Orders.OrderDate) AS 'Order Date', 
       '$' + Convert(VARCHAR(50), Orders.price * Orders.quantity) AS 'Total', 
       Orders.delivered
FROM   Orders
INNER JOIN Items ON Orders.itemID = Items.itemID
ORDER BY   Orders.orderID, Items.itemID ASC

答案 2 :(得分:0)

这是未经测试的,但这是相同的答案,但是转换数值。

SELECT     Orders.OrderID, 
           Orders.invoiceID, 
           Items.itemName AS 'Item Name', 
           Orders.quantity, 
    DATENAME(mm, Orders.OrderDate) + ' ' + DATENAME(dd, Orders.OrderDate) + ', ' + DATENAME(yyyy,          Orders.OrderDate) AS 'Order Date', 
    '$' + CAST((Orders.price * Orders.quantity) as varchar(90))
          AS 'Total', 
           Orders.delivered
FROM       Orders
INNER JOIN Items ON Orders.itemID = Items.itemID
ORDER BY   Orders.orderID, Items.itemID ASC