数据类型 varchar 和 int 在 concat 运算符中不兼容 [英] The data types varchar and int are incompatible in the concat operator

查看:34
本文介绍了数据类型 varchar 和 int 在 concat 运算符中不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去两天我一直卡在执行它给出错误:

I've been stuck for the past two days execute it give the error:

数据类型 varchar 和 int 在 concat 运算符中不兼容

The data types varchar and int are incompatible in the concat operator

这是我的桌子:

create table salestable
(
     id int identity(1,1) not null primary key, 
     empid char(5), 
     datesold date, 
     monthonly varchar(50), 
     amount money
)

这段代码将虚拟记录插入到 dbo.salestable 中,用于在 salestable 中工作,调试并进入给出调试和理解代码的代码

This code inserts the dummy record into dbo.salestable for working in salestable, debug and step into the code that give the debug and understanding code

declare @outercounter int = 1
declare @innercounter int = 1

while @outercounter <= (select count(name) from namestable)
begin 
    while @innercounter <= datediff(day, getdate() -datepart(day, getdate()), {fn concat('12/31/',Datepart(year,getdate()))})
    begin 
        insert into salestable (empid, datesold, monthonly, amount) 
        values (@outercounter, 
                getdate() - datepart(day, getdate()) + @innercounter,
                Datename(month, getdate() - datepart(day, getdate()) + @innercounter),
                rand() * 10000)
        set @innercounter =  @innercounter +1           
    end

    set @outercounter = @outercounter + 1
    set @innercounter = 1
end

select * from dbo.salestable

推荐答案

fn concat('12/31/',CAST(Datepart(year,getdate()) AS VARCHAR(10)))

试试这个

这篇关于数据类型 varchar 和 int 在 concat 运算符中不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆