SQL conversion failed when converting the varchar

OK just joined stackoverflow today and I am very new to scripting, specially SQL I am trying to run below to get a invoice num

SELECT 
   * 
FROM 
   [Database].[dbo].[Table]
WHERE
    [EPSINO] = 915561
    AND [EPYEA4] = 2017
    AND [EPSPYN] = EPSPYN
    AND [EPCONO] = 100
    AND [EPDIVI] = 400
GO

But I'm getting an error

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'R8463 ' to data type int.

Understand I need a convert line in there somewhere but not sure where

PLEASE HELP

Thanks

Upvotes: 1

Views: 285

Answers (3)

Shyam Vemula
Shyam Vemula

Reputation: 589

First find out the datatypes of that table, then build the WHERE clause. like VARCHAR, NVARCHAR and DATE Type enclose the Search text with Single Quotes. Use Below command for the list of columns and its datatypes and build your Query.

SP_HELP [Database].[dbo].[Table]

Upvotes: 0

Nisarg Shah
Nisarg Shah

Reputation: 14561

When you have a column of type char, you have to compare its value within single quotes. So if column EPSPYN was of type char, ensure that the value against it is written within single quote.

Select * From[Database].[dbo].[Table]
WHERE
    [EPSINO] = 915561
 and [EPYEA4] = 2017
 and [EPSPYN] = 'EPSPYN' -- See this.
 and [EPCONO] = 100
 and [EPDIVI] = 400
GO

Upvotes: 1

LAS
LAS

Reputation: 819

Look at the datatypes defined on your columns. nvarchar columns need to be wrapped in single quotes. For example, EPSPYN is most likely an nvarchar column. I can't tell if the remaining columns are int or nvarchar. You need to look at your table definition and wrap in quotes appropriately.

select * 
from[Database].[dbo].[Table]
where [EPSINO] = 915561
      and [EPYEA4] = 2017
      and [EPSPYN] = 'EPSPYN'
      and [EPCONO] = 100
      and [EPDIVI] = 400
 GO

Upvotes: 2

Related Questions