SQLCMD - 从日期和时间删除时间戳将字段格式化为2个小数位

时间:2015-12-18 03:26:37

标签: mysql sql-server sqlcmd sapb1

我正在使用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

非常感谢任何帮助。

此致 瑞克

1 个答案:

答案 0 :(得分:0)

在询问之前搜索现有答案。

1)How to return the date part only from a SQL Server datetime datatype

SQlServer2008及以上版本:

select CONVERT(date, getdate())

2)Write a number with two decimal places SQL server

SELECT CONVERT(DECIMAL(10,2),YOURCOLUMN)