--SELECT CONVERT(VARCHAR,gradd19,105)FROM gradeddetails

时间:2016-09-27 08:45:06

标签: sql-server sql-server-2008

enter image description here

rom_date列是日期时间格式----- temptable1stockdetails。

gradd19列是varchar格式------ gradeddetails

将gradd19值插入from_date。但错误来了。请告诉。

编辑OP评论中的代码:

insert into TempTable1STOCKDETAILS (PRODUCT_NO,PRODUCT_NAME,AC_NO,FROM_DATE,QTY_IN,B_F) 
SELECT a.PRODUCT_NO PRODUCT_NO
      ,P.PRODUCT_NAME PRODUCT_NAME
      ,a.CATEGORY ProductGroup
      ,CONVERT(VARCHAR(10), gradd19, 105) as ExpiryDate
      ,(SELECT ISNULL(SUM(QTY_IN),0) FROM GradedDETAILS WHERE CATEGORY=a.CATEGORY AND PRODUCT_NO=A.PRODUCT_NO 
                                                          AND STOCK_JR IN ('IN','GR','TR','SA','SR','OG','PO','LT') 
                                                          AND STOCK_DATE BETWEEN '2016-09-01 00:00:00' AND '2016-09-28 00:00:00')
      ,(SELECT ISNULL(SUM(QTY_IN),0) FROM GradedDETAILS WHERE STOCK_DATE < '2016-09-01 00:00:00' 
                                                          AND PRODUCT_NO=A.PRODUCT_NO AND CATEGORY=a.CATEGORY ) 
FROM GradedDETAILS A 
inner join PRODUCT P on A.PRODUCT_NO=P.PRODUCT_NO 
WHERE a.STOCK_JR IN('IN','GR','TR','SA','SR','OG','PO','LT') 
GROUP BY a.PRODUCT_NO,P.PRODUCT_NAME ,a.CATEGORY,a.Gradd19

1 个答案:

答案 0 :(得分:0)

它会给您一个错误,因为varchar无法存储转换后的日期格式,从而导致超出范围的错误。

  SELECT CONVERT(VARCHAR(10), gradd19, 105) FROM gradeddetails

将其转换为VARCHAR(10)而不是VARCHAR