[运行SQL将我的数据类型值从varchar
转换为datetime
时,出现以下错误。
Msg 242,级别16,状态3,第1行varchar数据类型到datetime数据类型的转换导致值超出范围。
我已经检查了数据,看不到任何奇怪的东西:进行了以下检查,但均未返回任何结果
SELECT [Date] from table where [DATe] is null
SELECT [Date] from table where [DATe] = ''
SELECT [Date] from table where LEN([date])> 10
SELECT [Date] from table where LEN([date])< 10
SELECT top 100 [Date] , SUBSTRING([date],4,2) from [table where convert(int, SUBSTRING([date],4,2)) < 1 or convert(int, SUBSTRING([date],4,2)) > 12
SELECT top 100 [Date] , SUBSTRING([date],1,2) from table where convert(int, SUBSTRING([date],4,2)) < 1 or convert(int, SUBSTRING([date],4,2)) > 31
还有其他值得一看的东西,也许值得任何指导或帮助解决这个问题?似乎无法深究。
我一周前也遇到过同样的问题。问题出在时区设置上。以其他格式指定,例如mm / dd / yyyy(通常有效)。
将日期指定为30/12/2013对我造成了错误。但是,将其指定为mm / dd / yyyy格式是可行的。
如果需要转换输入,可以尝试查看CONVERT
方法。语法为
CONVERT(VARCHAR,@your_date_Value,103)
CONVERT(VARCHAR, '12/30/2013', 103)
整理103是日期时间格式。
请参阅此链接以获取转换格式和进一步的阅读。https://www.w3schools.com/sql/func_sqlserver_convert.asp
+ this happens because sql sometimes doesn't recognize dd/mm/yyyy format
+ so we should always check if the input string is a valid date or not and the accordingly convert it to mm/dd/yyyy and so , i have shown below how it can be done, i have created a function to rearrange in mm/dd/yyyy from dd/mm/yyyy
select case when isdate('yourdate')=1 then CAST('yourdate' AS datetime)
else (select * from dbo.fn_convertdate(yourdate))
Create function dbo.fn_convertdate( @Stringdate nvarchar(29))
RETURNS @output TABLE(splitdata NVARCHAR(MAX)
)
Begin
Declare @table table(id int identity(1,1), data varchar(255))
Declare @firstpart nvarchar(255)
Declare @tableout table(id int identity(1,1), data varchar(255))
Declare @Secondpart nvarchar(255)
Declare @Thirdpart nvarchar(255)
declare @date datetime
insert into @table
select * from dbo.fnSplitString(@Stringdate,'/')
select @firstpart=data from @table where id=2
select @Secondpart=data from @table where id=1
select @Thirdpart=data from @table where id=3
set @date=@firstpart+'/'+@Secondpart+'/'+@Thirdpart
insert into @output(splitdata) values(
@date)
return
End
我只是将要转换为新表(带有DateTime字段)的varchar字段首先转换为与DateTime兼容的布局,然后SQL会完成从varchar到DateTime的转换而没有问题。
在下面(不是我用这些名称创建的表!),如果需要,我只是使varchar字段看起来像DateTime:
update report1455062507424
set [Move Time] = substring([Move Time], 7, 4) + '-'+ substring([Move Time], 4, 2) + '-'+ substring([Move Time], 1, 2) + ' ' +
substring([Move Time], 12, 5)
Varchar Date Convert to Date and Change the Format
[Nov 12 2016 12:00,21/12/2016,21-12-2016此查询适用于上面更改为此格式dd / MM / yyyy
SELECT [Member_ID],[Name] ,
Convert(varchar(50),Convert(date,[DOB],103),103) as DOB
,[NICNO],[Relation] FROM [dbo].[tbl_FamilMember]
发生此错误是因为我试图直接使用C#代码中的内联查询将最小日期和时间存储在列中。
如果未另外设置,则将C#中的DateTime以此日期和时间初始化的事实,在代码中将date变量设置为01/01/0001 12:00:00 AM。并且MS-SQL 2008 datetime数据类型中允许的最小日期为1753-01-01 12:00:00 AM。
我从代码中更改了日期并将其设置为01/01/1900,没有进一步报告错误。
我在具有mm而不是MM的日期上使用了ToString()。
只需确保您的日期兼容或可以在数据库管理器(例如SQL Server Management Studio)中正确运行。例如,DateTime.Now C#函数在SQL Server中无效,这意味着您的查询必须包含有效的函数,例如SQL Server的GETDATE()。
此更改对我来说非常有效。
由于一个愚蠢的错误,我遇到了这个问题。 确保日期确实存在!
例如:
不存在2015年9月31日。
EXEC dbo.SearchByDateRange @Start = '20150901' , @End = '20150931'
因此,失败并显示以下消息:
Error converting data type varchar to datetime.
要解决此问题,请输入有效日期:
EXEC dbo.SearchByDateRange @Start = '20150901' , @End = '20150930'
它执行得很好。
最近我有类似的问题。在应用程序和数据库服务器中都正确设置了区域设置。但是,SQL的执行导致
“将varchar数据类型转换为datetime数据类型导致超出范围的值”。
问题是数据库用户的默认语言。
要在SSMS中进行检查或更改,请转到安全性->登录名,然后右键单击运行查询的用户的用户名。选择属性->常规,并确保对话框底部的默认语言是您所期望的。
对所有运行查询的用户重复此操作。
您可以使用
Set dateformat <date-format> ;
在您的sp函数或存储过程中,可以完成任务。
Create procedure [dbo].[a]
@examdate varchar(10) ,
@examdate1 varchar(10)
AS
Select tbl.sno,mark,subject1,
Convert(varchar(10),examdate,103) from tbl
where
(Convert(datetime,examdate,103) >= Convert(datetime,@examdate,103)
and (Convert(datetime,examdate,103) <= Convert(datetime,@examdate1,103)))
我遇到了同样的问题,并确定出现此问题是因为SQL Server不会对以相同方式转换为整数的字符进行比较。在我的测试中,我发现某些转换字符比较(例如感叹号)将返回类型转换错误,而其他转换字符比较(例如空格)将被确定超出范围。
此示例代码测试了各种可能的情况,并使用嵌套的REPLACE语句提供了一种解决方案。 REPLACE确定字符串中是否有非数字或斜杠的字符,如果存在,则字符串的长度将大于零,从而表明存在“坏”字符且日期无效。
DECLARE @str varchar(10)
SET @str = '12/10/2012'
IF convert(int, substring(@str,4,2)) <= 31 AND convert(int, substring(@str,4,2)) >= 1
PRINT @str+': Passed Test'
ELSE PRINT @str+': Failed Test'
GO
DECLARE @str varchar(10)
SET @str = '12/10/2012'
PRINT 'Number of characters in ' + @str + ' that are not numerals or a slash (0 means the date is valid; all values greater than 0 indicate a problem): ' + convert(varchar(5),len(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(@str,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''), '8',''),'9',''),'/',''),' ','+'))) --replace space with a + to avoid empty string
PRINT ''
GO
DECLARE @str varchar(10)
SET @str = '12/!0/2012'
IF convert(int, substring(@str,4,2)) <= 31 AND convert(int, substring(@str,4,2)) >= 1
PRINT @str+': Passed Test'
ELSE PRINT @str+': Failed Test'
GO
DECLARE @str varchar(10)
SET @str = '12/!0/2012'
PRINT 'Number of characters in ' + @str + ' that are not numerals or a slash (0 means the date is valid; all values greater than 0 indicate a problem): ' + convert(varchar(5),len(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(@str,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''), '8',''),'9',''),'/',''),' ','+'))) --replace space with a + to avoid empty string
PRINT ''
GO
DECLARE @str varchar(10)
SET @str = '12/ /2012'
IF convert(int, substring(@str,4,2)) <= 31 AND convert(int, substring(@str,4,2)) >= 1
PRINT @str+': Passed Test'
ELSE PRINT @str+': Failed Test'
GO
DECLARE @str varchar(10)
SET @str = '12/ /2012'
PRINT 'Number of characters in ' + @str + ' that are not numerals or a slash (0 means the date is valid; all values greater than 0 indicate a problem): ' + convert(varchar(5),len(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(@str,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''), '8',''),'9',''),'/',''),' ','+'))) --replace space with a + to avoid empty string
输出:
--Output
--12/10/2012: Passed Test
--Number of characters in 12/10/2012 that are not numerals or a slash (0 means the date is valid; all values greater than 0 indicate a problem): 0
--Msg 245, Level 16, State 1, Line 4
--Conversion failed when converting the varchar value '!0' to data type int.
--Number of characters in 12/!0/2012 that are not numerals or a slash (0 means the date is valid; all values greater than 0 indicate a problem): 1
--12/ /2012: Failed Test
--Number of characters in 12/ /2012 that are not numerals or a slash (0 means the date is valid; all values greater than 0 indicate a problem): 2
在将sysdate自动插入列中时,我也遇到了此问题。
我所做的是更改了系统日期格式,使其与SQL Server的日期格式匹配。例如我的SQL格式是mm / dd / yyyy,而我的系统格式设置为dd / mm / yyyy。我将系统格式更改为mm / dd / yyyy,错误消失了
-kb
如您所知,这是英国格式的问题。您可以使用函数间接进行日期转换。
CREATE FUNCTION ChangeDateFormatFromUK
(
@DateColumn varchar(10)
)
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @Year varchar(4), @Month varchar(2), @Day varchar(2), @Result varchar(10)
SET @Year = (SELECT substring(@DateColumn,7,10))
SET @Month = (SELECT substring(@DateColumn,4,5))
SET @Day = (SELECT substring(@DateColumn,1,2))
SET @Result = @Year + '/' @Month + '/' + @Day
RETURN @Result
END
调用此功能
SELECT dbo.ChangeDateFormatFromUK([dates]) from table
通常将其转换为日期时间
SELECT CONVERT(DATETIME,dbo.ChangeDateFormatFromUK([dates])) from table
根据您的情况,您可以做
SELECT [dates] from table where CONVERT(DATETIME,dbo.ChangeDateFormatFromUK([dates])) > GetDate() -- or any date
测试年份> 2079。我发现用户在该年份(10/12/2106)输入了2106而不是2016年的打字错误,并且繁荣。因此,在2016年10月12日,我进行了测试,发现SQL Server最多接受2078年,如果年份为2079年或更高,则开始引发该错误。关于滑动SQL Server的日期类型,我尚未做任何进一步的研究。