Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
fn concat('12/31/',CAST(Datepart(year,getdate()) AS VARCHAR(10)))

Try this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...