2

The following query works fine:

select ProductNumber,Name,Color,ListPrice 
from Production.Product 
where ProductNumber = 'AR-5381'

Where as when, I'm writing my code like following to retrieve value from SQL sever:

declare @ProductNum nvarchar
set @ProductNum = 'AR-5381'
select ProductNumber,Name,Color,ListPrice 
from Production.Product 
where ProductNumber = @ProductNum 

I'm not able to get the values. But ProductNumber('AR-5381') is present in the table. What I'm doing wrong?

1 Answer 1

4

Variable is declared with default length which is 1.

nvarchar without specifying length means nvarchar(1)

so following nvarchar varaible only store first character in the string because its length is one.

declare @ProductNum nvarchar
set @ProductNum = 'AR-5381'
SELECT @ProductNum 

Output
A

declare @ProductNum nvarchar(2)
set @ProductNum = 'AR-5381'
SELECT @ProductNum 

Output
AR

You should specify some length like.

declare @ProductNum nvarchar(255)
set @ProductNum = 'AR-5381'
select ProductNumber,Name,Color,ListPrice 
from Production.Product 
where ProductNumber =@ProductNum 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.