SQL Server:如何将当前日期插入数据库?

时间:2016-07-12 18:08:54

标签: sql-server database tsql

SQL Server:如何使用以下格式获取当前日期?

我有以下代码向表中插入日期,但如何获取CURDATE() 以下列格式

 INSERT INTO [MIBOMH] 
   ([bomItem], [bomRev], [rollup], [mult], [autoBuild], [assyLead],[revCmnt],[author],[descr],[qPerLead],[lstMainDt],[revDate],[effStartDate],[ovride] )
 SELECT DISTINCT [bomitem], [bomrev], '1', '1', '1', '3','SYNC ','AUTHORNAME','SYNC','0', '2016-07-12 14:10:19.427','20160712','20160712','0'
FROM [DB].[dbo].[whlmibomh]
  WHERE  [bomitem] IN (SELECT [ItemID] FROM [MIITEM] WHERE type='2')
 AND [bomitem] NOT IN (SELECT [bomItem] FROM [MIBOMH])

所需格式:

  1. '2016-07-12 14:10:19.427'
  2. '20160712'
  3. '20160712'

1 个答案:

答案 0 :(得分:1)

您应该使用FORMAT function

DECLARE @d DATETIME = GETDATE();  
SELECT  FORMAT(@d, 'yyyy-MM-dd HH\:mm\:ss\.fff', 'en-US') AS 'Format#1'  
       ,FORMAT(@d, 'yyyyMMdd'                  , 'en-US') AS 'Format#2'