DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: SQL Server 7 Field length ... newbie question ...

  1. #1
    Robert Dyke Guest

    SQL Server 7 Field length ... newbie question ...

    I've set up a table with a field called "Name" with a default field length
    of 15

    Here's the problem ... when I use ASP to search for the Record where "Name =
    'David'" it doesn't find it.

    But if I search for the Record where "Name = 'David '" (15
    characters total), it finds it.

    The problem is that SQL Server automatically adds the extra spaces at the
    end to bring it up to 15 characters.

    Is there a way I can set up the table where it won't add the extra spaces
    after the name?

    Thanks.



  2. #2
    Dan Guest

    Re: SQL Server 7 Field length ... newbie question ...


    Use varchar instead of char as your data type.

    When you create a "char" field, you are specifying that the field will always
    be a certain length. If data is inserted that is shorter, it is padded with
    spaces on the right. If it is longer, it is truncated.

    Using varchar instead of char does take a little bit more storage space,
    since SQL Server has to do a little extra work to determine where one field
    ends and another begins on the physical hard disk. But I use varchar exclusively
    (for about everything except a state abbreviation field) and have never had
    a problem with it.

    >I've set up a table with a field called "Name" with a default field length
    >of 15
    >
    >Here's the problem ... when I use ASP to search for the Record where "Name

    =
    >'David'" it doesn't find it.
    >
    >But if I search for the Record where "Name = 'David '" (15
    >characters total), it finds it.
    >
    >The problem is that SQL Server automatically adds the extra spaces at the
    >end to bring it up to 15 characters.
    >
    >Is there a way I can set up the table where it won't add the extra spaces
    >after the name?
    >
    >Thanks.
    >
    >



  3. #3
    Robert Dyke Guest

    Re: SQL Server 7 Field length ... newbie question ...

    Works perfectly ... thanks for your help.


    "Dan" <dan.clem@trilogynetworks.com> wrote in message
    news:3b4f175d$1@news.devx.com...
    >
    > Use varchar instead of char as your data type.
    >
    > When you create a "char" field, you are specifying that the field will

    always
    > be a certain length. If data is inserted that is shorter, it is padded

    with
    > spaces on the right. If it is longer, it is truncated.
    >
    > Using varchar instead of char does take a little bit more storage space,
    > since SQL Server has to do a little extra work to determine where one

    field
    > ends and another begins on the physical hard disk. But I use varchar

    exclusively
    > (for about everything except a state abbreviation field) and have never

    had
    > a problem with it.
    >
    > >I've set up a table with a field called "Name" with a default field

    length
    > >of 15
    > >
    > >Here's the problem ... when I use ASP to search for the Record where

    "Name
    > =
    > >'David'" it doesn't find it.
    > >
    > >But if I search for the Record where "Name = 'David '" (15
    > >characters total), it finds it.
    > >
    > >The problem is that SQL Server automatically adds the extra spaces at the
    > >end to bring it up to 15 characters.
    > >
    > >Is there a way I can set up the table where it won't add the extra spaces
    > >after the name?
    > >
    > >Thanks.
    > >
    > >

    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center