将字符串转换为日期时间-SQL

问题描述

我有一个从XML解析的字符串,它表示日期时间。 字符串格式为:'20200915114000'-(YYYYMMDDhhmmss)

sql Server中是否有一个函数将此字符串转换或解析为日期时间,还是应该手动拆分字符串并将其连接为日期时间?

解决方法

如何执行此操作,分3个步骤:

C:\> SQLCMD
1> select convert(datetime,'20200915114000' )
2> go
Msg 241,Level 16,State 1,Server ZES,Line 1
Conversion failed when converting date and/or time from character string.
1> select convert(datetime,'20200915 114000' )
2> go
Msg 242,State 3,Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
1> select convert(datetime,'20200915 11:40:00' )
2> go

-----------------------
2020-09-15 11:40:00.000

(1 rows affected)
1>

结论,您需要在字符串中添加空格和3':'才能将'20200915114000'转换为'20200915 11:40:00'。之后,简单的转换即可解决问题。

,

我的问题的解决方案:

@client.command()
@commands.has_permissions(manage_channels=True)
async def nuke(ctx):
    
    channel = ctx.channel
    channel_position = channel.position
    
    new_channel = await channel.clone()
    await channel.delete()
    await new_channel.edit(position=channel_position,sync_permissions=True)
    return