-1

我正在使用 SQLCMD 从我的服务器导出一个 SQL 查询。

我要导出的字段之一是日期字段 - 但在导出时它也有时间戳 - 例如:2015-12-15 00:00:00.000

如何从日期中删除时间戳,使其为:2015-12-15

除此之外 - 在其他具有数字数据的导出字段中,它会自动导出到小数点后 6 位。如何将字段格式化为小数点后 2 位?

目前 SQLCMD 看起来像这样:

SQLCMD -S <server name> -d <db name> -U <username> -P <password> -Q 
"set nocount on;
SELECT 
T0.DocNum, 
T0.CardCode, 
T0.CardName, 
T0.DocDate, 
T0.DocTime, 
T0.U_FreightEntry, 
T1.ItemCode, 
T1.Dscription, 
T1.CodeBars, 
T1.Quantity, 
(((100-T0.DiscPrcnt)/100)*T1.Price) as 'Price per unit ex GST', 
((((100-T0.DiscPrcnt)/100)*T1.Price)*1.1) as 'Price per Unit Inc GST', 
(SELECT T6.Price FROM ITM1 T6 where T6.ItemCode = T5.Itemcode and T6.PriceList = '2') as 'RRP inc GST per unit', T0.VatSum, T0.DocTotal as 'Inv Total' 
FROM dbo.OINV  T0 INNER JOIN dbo.INV1  T1 ON T0.DocEntry = T1.DocEntry 
INNER JOIN dbo.OCRD  T2 ON T0.CardCode = T2.CardCode 
INNER JOIN dbo.OCRG  T3 ON T2.GroupCode = T3.GroupCode 
INNER JOIN OPLN T4 ON T2.ListNum = T4.ListNum 
INNER JOIN OITM T5 ON T1.ItemCode = T5.ItemCode 
INNER JOIN ITM1 T6 ON T5.ItemCode = T6.ItemCode and T6.Pricelist = '1'
WHERE T3.GroupName = 'NEWSXPRESS' and T0.DocDate = '20151215'
ORDER BY T0.DocNum" 
-s "," -o "<server address>\NewsxpressInvoices_%DATE:~4,2%_%DATE:~7,2%_%DATE:~-4%.csv" -h-1 -s"~" -W -w 999

任何帮助将不胜感激。

问候瑞克

4

1 回答 1

0

在提问之前搜索现有的答案。

1)如何仅从 SQL Server 日期时间数据类型返回日期部分

SQLServer2008及以上:

select CONVERT(date, getdate())

2)写一个小数点后两位的数字 SQL server

SELECT CONVERT(DECIMAL(10,2),YOURCOLUMN)
于 2015-12-18T03:32:11.777 回答