简体   繁体   中英

SQL Server cast string to datetime

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?

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)

SELECT CAST(right(s, 2) + left(s,4) as datetime)
FROM (SELECT '010910' s) a

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

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