Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:13:26+00:00 2026-06-15T12:13:26+00:00

I am working on a C# project which reads values from a MS SQL

  • 0

I am working on a C# project which reads values from a MS SQL Server 2008 or 2008 R2 Server.

The query looks something like this:

string charge="1234567";
string command = string.Format("SELECT * FROM tableX WHERE chargeField = '{0}' ", charge);

chargeField is from type nvarchar(50) – do not ask me why – I cannot change the database.
Also the entries are written to the database by another tool which I also cannot change.
Sometimes the chargeField value will have leading or ending space characters in it, so my
query will return nothing because “1234567” != ” 1234567 “

However, when I write the query like this (without ‘ ‘):

string command = string.Format("SELECT * FROM tableX WHERE chargeField = {0} ", charge);

it works. Why does this even work – don´t i need the ” signs to indicate that the database field is a string? Can I safely change all my queries? Is this a “feature” from MS SQL Server and what exactly happens here.

EDIT

I solved the problem by using:

string command = string.Format("SELECT * FROM tableX WHERE RTRIM(LTRIM(chargeField)) = {0} ", charge);

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:13:27+00:00Added an answer on June 15, 2026 at 12:13 pm

    The SQL Server tries to convert the fields accordingly. Without quotation marks, you’re passing in a number, so SQL Server tries to convert the value in chargeField to a number, which works in spite of leading spaces.

    This won’t work if chargeField contains the value 12345a. This will lead to a conversion error. It is enough for one record in the table to contain such a value to get the error.

    To be on the safe side, change your query to

    ... WHERE LTRIM(chargeField) = '{0}'
    

    but be aware that this leads to a possible index on the field no longer being used. What you could still do is create an additional column that is filled using a trigger with the value of LTRIM(chargeField), put an index on that column and adapt your WHERE clause accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started working on a project which uses Net_URL_Mapper from PEAR (php) as the
I am working on a project which lists file sharing urls from the likes
I'm working on a c++ project that reads MIDI-data from an external USB-device. The
I am currently working on a php/javascript project which retrieves information from a database
I have a project which stores values in SQL and later retrieves them for
Can anyone help me find an up-to-date, working ATL project which has a main
Iam working on a project which involves writing a Mork File (Mork is a
I'm working on a project which contains several slave nodes and one master node.
I'm working on a project which involves two processes, let them be A and
I'm working on a project which needs to use youtube api. The html and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.