简体   繁体   中英

Convert varchar column to datetime in sql server

I have a table with following definition and data.

Definition:

CREATE TABLE [dbo].[TestTB]
(
    [CREATEDATE] [nvarchar](50) NULL
) ON [PRIMARY]

Data:

 10/9/2014 
 1/26/2015 
 2/16/2015 

When I run the query:

Select 
    CAST(CREATEDATE AS DATETIME) as CREATEDATE 
FROM 
    [dbo].[TestTB]

It is throwing error:

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.

The above does not work even after running

SET DATEFORMAT dmy

However the following query works fine

  DECLARE @data nvarchar(50)
  SET @data =  ' 10/9/2014 '
  Select CAST(@data as DateTime)

Above query returns: 2014-10-09 00:00:00.000

How do I convert a date string (of mm/dd/yyyy format) stored in a column to datetime?

First, if your table column is "DateTime" type than it will save data in this format "2014-10-09 00:00:00.000" no matter you convert it to date or not. But if not so and if you have SQL Server version 2008 or above than you can use this,

DECLARE @data nvarchar(50)
SET @data =  '10/9/2014'

IF(ISDATE(@data)>0)
BEGIN
    SELECT CONVERT(DATE, @data)
END

Otherwise

DECLARE @data nvarchar(50)
SET @data =  '10/9/2014'

IF(ISDATE(@data)>0)
BEGIN
    SELECT CONVERT(DATETIME, @data)
END

To Insert into table

INSERT INTO dbo.YourTable
SELECT CREATEDATE FROM
(
    SELECT
        (CASE WHEN (ISDATE(@data) > 0) THEN CONVERT(DATE, CREATEDATE) 
        ELSE CONVERT(DATE, '01/01/1900') END) as CREATEDATE 
    FROM 
        [dbo].[TestTB]
) AS Temp
WHERE
    CREATEDATE <> CONVERT(DATE, '01/01/1900')
DECLARE @data nvarchar(50)
  SET @data =  ' 10/9/2014 '
  SELECT CONVERT(datetime, @data, 101)

SELECT CONVERT(datetime, CREATEDATE, 101) as CREATEDATE 
FROM [dbo].[TestTB]

http://www.sqlfiddle.com/#!6/f2103

please check this http://msdn.microsoft.com/en-us/library/aa226054%28SQL.80%29.aspx

Try this out:

SELECT IIF(ISDATE(CREATEDATE) = 1, CONVERT(DATETIME, CREATEDATE, 110), CREATEDATE) as DT_CreateDate
FROM [dbo].[TestTB]

SQL Fiddle Solution

nvarchar to datetime is an implicit conversion, it means you can do this:

declare @strDate nvarchar(50), @myDate datetime
set @strDate = '01/26/2013'
set @myDate = @strDate

But your problem is date format so you have to convert it, but you have to scpecify the format or style and the code of the style you're using is 101 (according to this table ) so the secure way:

 declare @strDate nvarchar(50), @myDate datetime
 set @strDate = '01/26/2013'
 set @myDate = CONVERT(DATETIME, @strDate, 101)

You can skip the code but with diferent configuration(server) that code will break. If you use the ISO style (yyyymmdd) you never would'nt have to worry and the conversion would be implicit:

  declare @strDate nvarchar(50), @myDate datetime
  set @strDate = '20150421' --ISO style (yyyymmdd)
  set @myDate = @strDate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM