[英]Error converting data type varchar to date in SQL Server 2008 R2
I have the date to check as shown below: 我要检查的日期如下所示:
Input Date: 输入日期:
17-09-2014
For which I am converting in my dynamic script: 我要在我的动态脚本中对其进行转换:
Attempt #1: 尝试1:
CAST((convert(date,@FDate, 105)) AS nvarchar(50))
Error: 错误:
Error converting data type varchar to date.
将数据类型varchar转换为日期时出错。
Attempt #2: 尝试2:
convert(date, @FDate, 105)
Error: 错误:
The data types nvarchar and date are incompatible in the add operator.
数据类型nvarchar和date在add运算符中不兼容。
Attempt #3: 尝试3:
cast(@FDate as varchar(50))
Error: 错误:
Error converting data type varchar to date.
将数据类型varchar转换为日期时出错。
One whole attempt, taken from the sqlfiddle.com/#!3/d41d8/38976 of the comments: 从注释的sqlfiddle.com/#!3/d41d8/38976进行的一次完整尝试:
DECLARE @querys NVARCHAR(max)
DECLARE @Date DATE
SET @Date = '17-09-2014'
SET @querys = 'SELECT' + CAST((convert(date,@Date, 105)) AS nvarchar(50)) + ''
EXEC(@querys)
Try 尝试
convert(Datetime, @FDate,105)
I tried following script and it worked well for SQL Server 2005 and SQL Server 2012: 我尝试了以下脚本,该脚本在SQL Server 2005和SQL Server 2012上运行良好:
Declare @FDate varchar(100);
set @FDate='17-09-2014';
Select convert(Varchar(50), convert(Datetime, @FDate,105) ,105)
Verified your fiddle script, just had small change and it worked as expected. 验证了您的小提琴脚本,只做了很小的更改,它就按预期工作了。
Here is new script that I tested on fiddle: 这是我在小提琴上测试过的新脚本:
DECLARE @qs VARCHAR(max)
Declare @FsDate varchar(100)
set @FsDate = '17-09-2014'
SET @qs = 'Select convert(Varchar(50), convert(Datetime, '''+@FsDate+''',105) ,105) '
EXEC(@qs)
Try like this! 尝试这样!
declare @a varchar(50)
set @a='17-09-2014'
select cast( right(@a,4)+'-'+SUBSTRING(@a,4,2)+'-'+LEFT(@a,2) as date)
There seems to be some confusion with Convert . 与Convert似乎有些混淆。 Comparing your fiddels from the comments and your shown Attempts.
从评论和显示的尝试中比较您的朋友。 Your fiddels are showing
DECLARE @Date DATE
; 您的好友显示
DECLARE @Date DATE
; The first argument is the targettype (Attempt #1) since your @FDate is already of type DATE convert(date,@FDate, 105)
will lead to a conversion from DATE to DATE, your outer cast to nvarchar does not seem to work due to your locales. 第一个参数是targettype(尝试#1),因为您的@FDate已经是DATE类型
convert(date,@FDate, 105)
将导致从DATE到DATE的转换,您的外部convert(date,@FDate, 105)
转换为nvarchar似乎不起作用到您的语言环境。 (Attempt #2) is shown incomplete since the shown part convert(date, @FDate, 105)
does work, even it won't change anything (conversion from DATE to DATE) (尝试#2)显示为不完整,因为显示的部分
convert(date, @FDate, 105)
可以正常工作,即使它什么都不会改变(从DATE到DATE的转换)
(Attempt #3) does not seem to work due to your locales. (尝试#3)由于您的语言环境而似乎不起作用。
Your shown fiddle: 您显示的小提琴:
DECLARE @querys NVARCHAR(max)
DECLARE @Date DATE
SET @Date = '17-09-2014'
SET @querys = 'SELECT' + CAST((convert(date,@Date, 105)) AS nvarchar(50)) + ''
EXEC(@querys)
already is failing here SET @Date = '17-09-2014'
, a save way independed from locales would be to use the format YYYYMMDD SET @Date = '20140917'
. 已经在这里失败了
SET @Date = '17-09-2014'
,一种独立于语言环境的保存方式是使用格式YYYYMMDD SET @Date = '20140917'
。 Since you are trying to buid a varchar your targettype for CONVERT would by VARCHAR not DATE and you wuold have to add quotation marks, a simple PRINT @querys or SELECT @querys would show what you are trying to execute. 由于您正在尝试为varchar构造VARCHAR,因此CONVART的目标类型将不是VARCHAR,而是DATE,并且您必须添加引号,因此,简单的PRINT @querys或SELECT @querys将显示您要执行的内容。
Taken from your fiddle, you are trying to convert a Date to a varchar and then add it to a dynamic SQL which you want to execute, so one way to go would be: 取而代之的是,您尝试将Date转换为varchar,然后将其添加到要执行的动态SQL中,因此一种方法是:
DECLARE @querys NVARCHAR(max)
DECLARE @Date DATE
SET @Date = '20140917'
-- get it as varchar
--SET @querys = ' SELECT ''' + convert(varchar(20),@Date,105) + ''''
--get it as date
SET @querys = ' SELECT convert(date,''' + convert(varchar(20),@Date,105) + ''',105)'
EXEC(@querys)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.