简体   繁体   中英

How to convert nvarchar type to only time type format in sql server

I have a column of type nvarchar ie 170948 I want to store it in time type column

For that I had written

select @CurrentTimeValue=Convert(time, items, 108) from T

but it is throwing error

Conversion failed when converting date and/or time from character string.

Please suggest the correct format

Since none of the supported CONVERT styles match your input, you'll have to tweak your input manually - something like this:

DECLARE @input VARCHAR(10) = '170948'

DECLARE @modified VARCHAR(10)

SET @modified = SUBSTRING(@input, 1, 2) + ':' + SUBSTRING(@input, 3, 2) + ':' + RIGHT(@input, 2)

SELECT CAST(@modified AS TIME)

您也可以尝试

select cast(stuff(stuff('170948', 5,0,':'), 3,0,':') as time)
select cast(stuff(stuff('170948', 3,0,':'), 6,0,':') as time)

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