2014-12-04 160 views
1
declare @FromDate varchar(30) 
set @FromDate= '2014-06-06' 

我需要@FromDate轉換爲datetime轉換爲datetime從varchar

set @fromdate = convert(datetime, @fromdate, 126) 

然後,我需要在'2014-06-06 00:00:00.000'格式輸出。

任何人都可以建議如何執行此轉換?

回答

1
DECLARE @FromDate VARCHAR(30) SET @FromDate = '2014-06-06' 
DECLARE @FromDateDT DATETIME SET @FromDateDT = @FromDate 
-- DECLARE @FromDateDT DATETIME SET @FromDateDT = CONVERT(DATETIME, @FromDate) -- It's the same as line above 
SET @FromDate = CONVERT(VARCHAR, @FromDateDT, 120) 
SELECT @FromDate 
SET @FromDate = CONVERT(VARCHAR, @FromDateDT, 121) 
SELECT @FromDate 

或簡單

SELECT CONVERT(VARCHAR, CONVERT(DATETIME, '2014-06-06'), 121) 

一些其他格式:

https://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/

隨意問。

0

試試這個

DECLARE @FromDate varchar(30) 
SET @FromDate= '2014-06-06' 
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, @fromdate), 21)