您好我是sql的新手,我一直得到sql默认值1900-01-01 00:00:00.000。我只想让这个字段变空。我想如果我把''空字符串用来代替默认值。我评论了我认为问题所在的界限。我尝试过其他方式,如ISNULL和Convert,但这不起作用。在此先感谢您,如果您可以解释默认日期的工作原理以及如何覆盖它,那将非常感激。
DECLARE @custAccount nvarchar(20)
SET @custAccount='CUS07869'
SELECT 'SM' as [Origin]
,'SN' as [Action Code]
,ST.PurchOrderFormNum as [Order Number]
,SL.ITEMID AS [LINE ITEM]
,SL.QTYORDERED as [Quantity]
,S.SHIPMENTDATE as [Action Date]
,S.TRACKINGNUMBER as [TRACKING]
,ISNULL(S.SHIPMENTDATE, '') as [DATE_SHIPPED]
,S.SHIPMETHOD as [Carrier]
FROM SalesTable ST
INNER JOIN GBP_Shipping S
ON ST.SalesId=S.SalesId AND ST.DataAreaId=S.DataAreaId
INNER JOIN SalesLine SL
ON ST.SalesId=SL.SalesId AND ST.DataAreaId=SL.DataAreaId
WHERE ST.CUSTACCOUNT=@custAccount
AND S.SHIPMENTDATE = convert(varchar, getdate(), 111)
AND ST.salestype!=4
UNION
SELECT 'SM' as [Origin]
,'AN' as [Action Code]
,ST.PurchOrderFormNum as [Order Number]
,SL.ITEMID AS [LINE ITEM]
,SL.QTYORDERED as [Quantity]
,replace(convert(varchar, getdate(), 111), '/', '') as [Action Date]
,'' as [TRACKING]
,'' as [DATE_SHIPPED] --I think this is the problem
,'' as [Carrier]
FROM SalesTable ST
INNER JOIN SalesLine SL
ON ST.SalesId=SL.SalesId AND ST.DataAreaId=SL.DataAreaId
WHERE ST.CUSTACCOUNT=@custAccount
AND ST.CREATEDDATE = convert(varchar, getdate(), 111)
答案 0 :(得分:2)
在联合的第一部分中设置列中数据的类型。
尝试
,case when S.SHIPMENTDATE is null then ''
else Convert(VarChar(10),S.SHIPMENTDATE)
end as DATE_SHIPPED
代替 的
,ISNULL(S.SHIPMENTDATE, '') as [DATE_SHIPPED]
答案 1 :(得分:0)
SQL没有“空”默认日期 - 你必须在那里设置一些东西。在引擎盖下,它们被存储为二进制值,二进制值0被转换为某种东西;在这里,我猜你正在使用smalldatetime,我相当确定0
= Jan 1, 1900
。 (日期时间为0可以在1753年获得一些东西,当公历开始时。我不确定他们为了SQL 2008的新日期/时间数据类型而翻转了什么硬币。)
如果您想要“无日期”,您可能必须使用NULL值,并在与isnull
函数相关的任何位置检查它们。也许是激怒,但恕我直言,这是SQL中NULL值的最合法用途之一。