0

In my table, I have a string-represented date column in the following format: {^2013/05/29}.

How do I convert this to standard datetime format in SQL Server? This contains string characters that are not part of what a datetime string usually has.

3
  • With substring and then a cast to Date or DateTime or DateTime2 Commented Aug 21, 2018 at 21:06
  • Possible duplicate of Sql Server string to date conversion Commented Aug 21, 2018 at 21:07
  • I found the problem. It is right here. "I have a string-represented date column". sqlblog.org/2009/10/12/… Commented Aug 21, 2018 at 21:27

3 Answers 3

2

That format is recognizable as a strict format by VFP only. Is that stored in SQL Server as text? If so:

Select cast(substring(myColumn, 3, 10) as date) as myDate from myTable;

would do the conversion.

If you mean it is stored like that in a VFP table and you want to convert a date then:

select ctod(myColumn) as myDate from myTable;
Sign up to request clarification or add additional context in comments.

2 Comments

Ah... VFP, my first true love.
@JohnCappelletti, everyone who have seen and used VFP loved it:) Unfortunately one day it was acquired by Microsoft.
0

If the data is always in the format {^yyyy/MM/dd} then you could use:

CONVERT(date,REPLACE(SUBSTRING(YourDateColumn,3,10),'/',''))

Ideally, however, you should be fixing your column to be the correct datatype:

CREATE TABLE Test (DateColumn varchar(13));
INSERT INTO Test VALUES ('{^2013/05/29}');
GO

SELECT *
FROM Test;

UPDATE Test
SET DateColumn = CONVERT(date,REPLACE(SUBSTRING(DateColumn,3,10),'/',''));

SELECT *
FROM Test;

ALTER TABLE test ALTER COLUMN DateColumn date;

SELECT *
FROM Test;
GO

DROP TABLE Test;

Comments

0
SELECT CAST(REPLACE('^2018/05/29','^','') AS DATETIME2)

1 Comment

You also need to remove { and }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.