2

I have a string 010910 in ddMMyy format thatI have to cast this string a SQL Server datetime datatype, like 2010-09-01 00:00:00.000.

How can this be done?

3
  • 3
    Google "sql server cast string to datetime". The first hit is the answer you need. Commented Jul 22, 2011 at 7:27
  • 1
    @Pondlife - I don't think there is an option answer for the format I'm working with. Commented Jul 22, 2011 at 13:08
  • That's correct, but hopefully it's obvious that if you know the supported formats, the only thing you need to do is convert your own format to a supported one. Commented Jul 22, 2011 at 13:39

3 Answers 3

5

How about something like

DECLARE @String VARCHAR(6)
SELECT  @String = '010910'

SELECT  CONVERT(DATETIME,LEFT(@String,2) + '/' + SUBSTRING(@String, 3, 2) + '/' + RIGHT(@String,2),3)

Have a look at SQL Server Date Formats and CAST and CONVERT (Transact-SQL)

Sign up to request clarification or add additional context in comments.

1 Comment

Your's works perfectly but I think t-clausen.dk's answer is more precise and short.
2
SELECT CAST(right(s, 2) + left(s,4) as datetime)
FROM (SELECT '010910' s) a

Comments

1

Pull the bits out using SUBSTRING and put them back together by concatenating.

Comments

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.