简体   繁体   English

SQL Server日期时间文字-将数据类型varchar转换为float时出错

[英]SQL Server datetime literal - Error converting data type varchar to float

Am sure I'm missing something obvious here, but how do I pass a datetime string literal to SQL server? 确保我在这里缺少明显的东西,但是如何将datetime字符串文字传递给SQL Server?

For instance: 例如:

select * 
from [dbo].temp_rk_table 
where tx_from <= '2015-10-01T06:37:16'  
  and '2015-10-01T06:37:16' < tx_to

gives: 得到:

Error converting data type varchar to float. 将数据类型varchar转换为float时出错。

Also: 也:

select * 
from [dbo].temp_rk_table 
where tx_from <= convert(datetime,'2015-10-01T06:37:16')
  and convert(datetime,'2015-10-01T06:37:16' ) < tx_to

gives: 得到:

GetNextRows failed. GetNextRows失败。 : Arithmetic overflow error converting expression to data type datetime. :将表达式转换为数据类型datetime的算术溢出错误。

?? ??

So (as per the comments) I was indeed missing something obvious! 所以(根据评论)我确实缺少明显的东西! The TX_FROM and TX_TO variables were defined as FLOAT and hence would not accept my converted datetime strings. TX_FROM和TX_TO变量定义为FLOAT,因此将不接受转换后的日期时间字符串。

Be sure your datetimes are indeed defined as datetimes. 确保您的日期时间确实定义为日期时间。 The query below may help ( source ): 下面的查询可能有帮助( source ):

select data_type + 
    case
        when data_type like '%text' or data_type like 'image' or data_type like 'sql_variant' or data_type like 'xml'
            then ''
        when data_type = 'float'
            then '(' + convert(varchar(10), isnull(numeric_precision, 18)) + ')'
        when data_type = 'numeric' or data_type = 'decimal'
            then '(' + convert(varchar(10), isnull(numeric_precision, 18)) + ',' + convert(varchar(10), isnull(numeric_scale, 0)) + ')'
        when (data_type like '%char' or data_type like '%binary') and character_maximum_length = -1
            then '(max)'
        when character_maximum_length is not null
            then '(' + convert(varchar(10), character_maximum_length) + ')'
        else ''
    end as CONDENSED_TYPE
    , *
from information_schema.columns

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM