The data going in:
的数据:
<F1>000001234</F1>
<F2>133228579</F2>
<F3>2011-05-25</F3>
<F4>21-332211</F4>
<F5>TxtMail</F5>
<F6/>
<F7>26/04/2011</F7>
<F8>00:09:13</F8>
<F9>0:00</F9>
<F10/>
<F11/>
<F12>Text Service</F12>
<F13>0294443333</F13>
<F14>TXT</F14>
<F15>FR</F15>
<F16>0.17</F16>
Relevant parts of the stored procedure :
存储过程的相关部分:
@F1 VARCHAR(24) = NULL, --AccountNumber
@F2 VARCHAR(24) = NULL, --InvoiceNumber
@F3 VARCHAR(24) = NULL, --InvoiceDate
@F4 VARCHAR(24) = NULL, --CallerNumber
@F5 VARCHAR(10) = NULL, --Service
@F6 VARCHAR(10) = NULL, --
@F7 varchar(24) = NULL, --CallDate
@F8 VARCHAR(24) = NULL, --CallTime
@F9 VARCHAR(50) = NULL, --Duration
@F10 VARCHAR(50) = NULL, --
@F11 VARCHAR(10) = NULL, --
@F12 VARCHAR(24) = NULL, --Network
@F13 VARCHAR(24) = NULL, --CallingNumber
@F14 VARCHAR(10) = NULL, --Type
@F15 VARCHAR(10) = NULL, --TypeName
@F16 MONEY = NULL, --Amount
DECLARE @Date VARCHAR(20)
SET @Date = RIGHT(@F7,4)+'/'+SUBSTRING(@F7,4,2)+'/'+LEFT(@F7,2)
-- Combine the date and time into a datetime data type
-- For Time
DECLARE @time DATETIME
SET @time = CONVERT(DATETIME, @Date + ' ' + @F8)
The error: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
错误:将varchar数据类型转换为datetime数据类型会导致值超出范围。
This is the only invocation of CONVERT(DATETIME) and I don't use CAST
这是唯一一次调用CONVERT(DATETIME),我不使用CAST
If I just pass in the data directly, the row gets inserted. If I run the sproc, it goes out-of-range.
如果我直接传入数据,就会插入该行。如果我运行sproc,它会超出范围。
2 个解决方案
#1
4
The safe datetime string formats to use in SQL Server is
在SQL Server中使用的安全的datetime字符串格式是
YYYYMMDD HH:MM:SS
or YYYY-MM-DDTHH:MM:SS
.
名称HH:MM:SS或YYYY-MM-DDTHH:MM:SS。
The conversion you have will fail if SET DATEFORMAT is dmy. SET LANGUAGE will automatically set date format for you. I think both german
and norwegian
use dmy.
如果SET DATEFORMAT是dmy,那么您的转换将失败。设置语言将自动为您设置日期格式。我认为德语和挪威语都使用dmy。
This will fail:
这将会失败:
set language norwegian
declare @F7 varchar(10) = '26/04/2011'
declare @F8 varchar(10) = '00:09:13'
DECLARE @Date VARCHAR(20)
SET @Date = RIGHT(@F7,4)+'/'+SUBSTRING(@F7,4,2)+'/'+LEFT(@F7,2)
DECLARE @time DATETIME
SET @time = CONVERT(DATETIME, @Date + ' ' + @F8)
Do like this instead (using YYYY-MM-DDTHH:MM:SS) to be safe regardless of language/dateformat settings.
这样做(使用yyyyyyy -MM- ddthh:MM:SS)是安全的,而不考虑语言/dateformat设置。
declare @F7 varchar(10) = '26/04/2011'
declare @F8 varchar(10) = '00:09:13'
DECLARE @Date VARCHAR(20)
SET @Date = RIGHT(@F7,4)+'-'+SUBSTRING(@F7,4,2)+'-'+LEFT(@F7,2)
DECLARE @time DATETIME
SET @time = CONVERT(DATETIME, @Date + 'T' + @F8)
#2
1
Try the following and see what you are getting,
试试下面的方法,看看你得到了什么,
DECLARE @Date VARCHAR(20)
SET @Date = RIGHT('26/04/2011',4)+'/'+SUBSTRING('26/04/2011',4,2)+'/'+LEFT('26/04/2011',2)
-- Combine the date and time into a datetime data type
-- For Time
DECLARE @time DATETIME
SET @time = CONVERT(DATETIME, @Date + ' ' + '00:09:13')
print @time
#1
4
The safe datetime string formats to use in SQL Server is
在SQL Server中使用的安全的datetime字符串格式是
YYYYMMDD HH:MM:SS
or YYYY-MM-DDTHH:MM:SS
.
名称HH:MM:SS或YYYY-MM-DDTHH:MM:SS。
The conversion you have will fail if SET DATEFORMAT is dmy. SET LANGUAGE will automatically set date format for you. I think both german
and norwegian
use dmy.
如果SET DATEFORMAT是dmy,那么您的转换将失败。设置语言将自动为您设置日期格式。我认为德语和挪威语都使用dmy。
This will fail:
这将会失败:
set language norwegian
declare @F7 varchar(10) = '26/04/2011'
declare @F8 varchar(10) = '00:09:13'
DECLARE @Date VARCHAR(20)
SET @Date = RIGHT(@F7,4)+'/'+SUBSTRING(@F7,4,2)+'/'+LEFT(@F7,2)
DECLARE @time DATETIME
SET @time = CONVERT(DATETIME, @Date + ' ' + @F8)
Do like this instead (using YYYY-MM-DDTHH:MM:SS) to be safe regardless of language/dateformat settings.
这样做(使用yyyyyyy -MM- ddthh:MM:SS)是安全的,而不考虑语言/dateformat设置。
declare @F7 varchar(10) = '26/04/2011'
declare @F8 varchar(10) = '00:09:13'
DECLARE @Date VARCHAR(20)
SET @Date = RIGHT(@F7,4)+'-'+SUBSTRING(@F7,4,2)+'-'+LEFT(@F7,2)
DECLARE @time DATETIME
SET @time = CONVERT(DATETIME, @Date + 'T' + @F8)
#2
1
Try the following and see what you are getting,
试试下面的方法,看看你得到了什么,
DECLARE @Date VARCHAR(20)
SET @Date = RIGHT('26/04/2011',4)+'/'+SUBSTRING('26/04/2011',4,2)+'/'+LEFT('26/04/2011',2)
-- Combine the date and time into a datetime data type
-- For Time
DECLARE @time DATETIME
SET @time = CONVERT(DATETIME, @Date + ' ' + '00:09:13')
print @time