Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 时间格式的正则表达式_Sql Server_Regex_Sql Server 2008 - Fatal编程技术网

Sql server 时间格式的正则表达式

Sql server 时间格式的正则表达式,sql-server,regex,sql-server-2008,Sql Server,Regex,Sql Server 2008,我在数据库表中有类似hh:mmss的时间格式。 我编写了一个查询来从列中获取这些值 declare @reg nvarchar(100) set @reg = '^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$' select time from table1 where time = @reg 然而,它并没有向我展示结果。如何获得结果 编辑: 示例数据(以下是我想从colum中找到的内容): 以下是我想在该栏中更改的内容: 23:36:2

我在数据库表中有类似hh:mmss的时间格式。 我编写了一个查询来从列中获取这些值

declare @reg nvarchar(100)
set @reg = '^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$'
 select time from table1 where time = @reg
然而,它并没有向我展示结果。如何获得结果

编辑: 示例数据(以下是我想从colum中找到的内容):

以下是我想在该栏中更改的内容:

23:36:26
08:55:46
检查。SQL SERVER似乎不支持纯正则表达式,您必须将查询更改为以下内容:

declare @reg nvarchar(100)
set @reg = '[01][0-9]:[0-5][0-9]:[0-5][0-9]'
select time from table1 where time like @reg

问题是您使用的是字符串而不是适当的类型,
time
。唯一真正的解决方案是修复字段类型

要转换数据,不需要正则表达式,只需在强制转换之前将
替换为
,例如:

select cast(replace(time,' ',':') as time)
执行:

select cast(replace('08:55 46',' ',':') as time)
返回时间值
08:55:46

您可能会尝试使用相同的命令再次将
time
转换为字符串,例如:
cast(将(time,,,:)转换为时间)转换为nvarchar(8))
,但这不会解决错误的类型问题。每次您要将文本发送到客户端或执行时间算术时,都将被迫再次将文本解析为时间

如果要使用时间变量或时间参数,只需指定适当的类型,例如:

declare @myParam time ='23:12:34'


请发布示例数据和预期结果…
SQL SERVER
不支持
regex
尝试使用类似于reg的时间而不是time=reg?SQL SERVER已具有
time
类型。你为什么要找正则表达式?你想用那句话做什么?为什么不使用
时间
字段,或者用
替换``来存储适当的时间字符串:
?为什么是正则表达式?
time
是否包含非时间字符串?我在列(表中)中有100万条这种格式的时间记录。如何通过硬编码解决所有这些问题?Alos,时间格式类型为charThanks,提示为:更新DPUCID01设置time2=replace(time2,,,:),其中time2类似于“[0-9][0-9]:[0-9][0-9][0-9][0-9]”
declare @myParam time ='23:12:34'
Create Procedure MyProc (@myTimeParam time)
...