sqlsql-serverwhere-clausenvarchardata-connections

SQL query of a string (nvarcahr(256)) doesn't give the data from table


[Table design]:

enter image description here

I inserted to the table using:

INSERT INTO Orders 
VALUES (N'Test', 10, 20)

[Data in the table]:

enter image description here

Now when I try to set a query to get row in the table by user name.

So I used the query:

SELECT * 
FROM Orders 
WHERE User = N'Test'

Trying to get the row by User:

enter image description here

But I get zero results from the query although the table have a row with the User "Test".

For testing I tried to do the same thing but search by the ItemId or ItemAmount and it returns results.

Searching by int returns result

I am new about using SQL Server .mdf database files. But not with .db and SQL query.

So how can I fix the problem or get the unique User row, without adding another col to the table?


Solution

  • You need to escape column name with []:

    SELECT * FROM Orders WHERE [User] = N'Test';
    

    DBFiddle Demo

    In SQL Server User <> [User]:

    SELECT User, [User]
    FROM Orders;
    

    DBFiddle Demo2