从SQL Server中的varchar列转换12小时到24小时

时间:2021-09-13 10:16:47

In my existing SQL Server database, there is a column like this:

在我现有的SQL Server数据库中,有一个这样的列:

  • Column name and type: meeting_time varchar(22)
  • 列名和类型:meeting_time varchar(22)

  • Values stored: 02:30:PM
  • 存储的值:02:30:PM

Now I want to convert it into 24 hours format. i.e., it should convert to 14.30

现在我想将其转换为24小时格式。即它应该转换为14.30

When I tried this:

当我尝试这个:

SELECT
    CONVERT(VARCHAR(8), meeting_time, 1) 
FROM 
    group_info 
WHERE 
    MGI_Id = 1

It will return the same value which stored in this column. When I tried with converting it into time or datetime, it throws exception.

它将返回存储在此列中的相同值。当我尝试将其转换为时间或日期时,它会抛出异常。

Can anyone please help me!

谁能帮帮我吗!

Thanks in advance.

提前致谢。

1 个解决方案

#1


2  

This should work:

这应该工作:

select convert(time, stuff(meeting_time, 6,  1, ' '))

Not sure if it works with all locales / languages, so you should check that first.

不确定它是否适用于所有语言环境/语言,因此您应首先检查它。

This assumes that your date is always in the same format, that the extra : is in the 6th character.

这假设您的日期始终采用相同的格式,即额外的:在第6个字符中。

#1


2  

This should work:

这应该工作:

select convert(time, stuff(meeting_time, 6,  1, ' '))

Not sure if it works with all locales / languages, so you should check that first.

不确定它是否适用于所有语言环境/语言,因此您应首先检查它。

This assumes that your date is always in the same format, that the extra : is in the 6th character.

这假设您的日期始终采用相同的格式,即额外的:在第6个字符中。