Sql-server – Incorrect syntax near the keyword ‘is’ in SQL server vb6

sql serversql-server-2012t-sql

Set rs = con.Execute("select IIF(MAX(bill_bno) IS NULL,1,MAX(bill_bno)+1) from rec_all      where rw=0 and iif(month(bill_date)>=1 AND month(bill_date)<=3,year(bill_date)-1,year(bill_date))=" & IIf(Month(Date) >= 1 And Month(Date) <= 3, Year(Date) - 1, Year(Date)))

how to change vb6 code (MS access) database to SQL Server 2012?

Best Answer

Since you only gave us the line that generates your recordset, we'll assume that you are able to establish your connection successfully.

First: the following statement should be valid in SQL Server 2012:

select IIF(MAX(bill_bno) IS NULL,1,MAX(bill_bno)+1) from rec_all
 where rw=0
   and  iif(month(bill_date)>=1 AND month(bill_date)<=3,year(bill_date)-1,year(bill_date))
       =iif(Month(GETDATE())>= 1 And Month(GETDATE()) <= 3, Year(GETDATE()) - 1, Year(GETDATE()))

As far as I can tell, this is what you listed above, with the HTML-ification removed, and the SQL Server current date and time function GETDATE() replacing what I believe is the MS Access current date function Date.

I've displayed the query on multiple lines with indenting for human readability; you can put the full query on one line, if that's what you're language requires.

Not knowing the language you're using, I can't tell you how to properly format your actual con.execute command; change > and < to &gt; and &lt; if required.

As the other answers showed, there are other commands in SQL more commonly used to do the things you're doing (I had to check to be sure iif was available in T-SQL), but the major problem query-wise seems to have been how to get the current system date.