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

梦想的初衷 提交于 2019-12-16 18:03:36

问题


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

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

Here is my table:

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

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

回答1:


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

Try this



来源:https://stackoverflow.com/questions/29875174/the-data-types-varchar-and-int-are-incompatible-in-the-concat-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!