Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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 日期转换不使用样式103_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql 日期转换不使用样式103

Sql 日期转换不使用样式103,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我已将日期保存为SQL Server中的nvarchar(50)数据类型。运行此查询时: select [Client_code], Date_of_receipt from [T_Receving] 我得到如下输出: 但是我想按特定日期过滤我的记录,所以我写了一个这样的查询 select convert(date, [Date_of_receipt], 103) as 'Date_of_Receipt' from [T_Receving] where

我已将日期保存为SQL Server中的
nvarchar(50)
数据类型。运行此查询时:

select [Client_code], Date_of_receipt 
from [T_Receving] 
我得到如下输出:

但是我想按特定日期过滤我的记录,所以我写了一个这样的查询

select 
    convert(date, [Date_of_receipt], 103) as 'Date_of_Receipt' 
from  
    [T_Receving]  
where  
    convert(date, [Date_of_receipt], 103) between '2015-03-06' and '2018-05-06'    
但这是一个错误

从字符串转换日期和/或时间时转换失败


你显然有一些坏数据。使用
尝试转换()

在SQLServer2008中,您必须更加努力地查找罪魁祸首。您可以从以下内容开始:

select date_of_receipt
from T_Receving
where date_of_receipt not like '[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9]'

这将发现大多数错误格式的实例。如果它仍然存在,您将不得不深入挖掘以找到错误的日数或月数。

您必须转换为datetime,然后再转换回varchar

 declare @dtv varchar(20) = '2018-17-04'
 declare @dtvdt datetime = convert(datetime, @dtv, 103) 
 select  @dtvdt;
 select convert(varchar(20), convert(datetime, @dtv, 103), 103), @dtv
 where  convert(datetime, @dtv, 103) between '2015-03-06' and '2018-05-06'

我正在使用sql server 2008,try_convert函数不可用there@user3262364 . . . 你应该适当地标记你的问题。SQL Server 2008现在已经很旧了。对不起,先生,我现在能做什么?我已经从[T_receiving]运行了qeury Select*,其中isdate(Date_of u receiving)=0,我得到了70records@user3262364,执行update语句将无效日期更改为有效值。既然您在2008年,为什么要将日期值存储为
nvarchar(50)
而不是
date
,我建议通过[T_receiving]中的Select*识别并更正您的数据,其中isdate(接收日期)=0@DanGuzman我错了,但现在我想用日期过滤我的记录time@JohnCappelletti,在运行该查询时,大约有70条记录。我想用它做些什么records@user3262364数据卫生至关重要。您必须更正、删除、空或筛选70多条记录。此外,这应该被视为使用适当数据类型的人生一课。。。日期应存储为日期,而不是字符串。
 declare @dtv varchar(20) = '2018-17-04'
 declare @dtvdt datetime = convert(datetime, @dtv, 103) 
 select  @dtvdt;
 select convert(varchar(20), convert(datetime, @dtv, 103), 103), @dtv
 where  convert(datetime, @dtv, 103) between '2015-03-06' and '2018-05-06'