Results 1 to 6 of 6

Thread: SQL Syntax Help

  1. #1
    Join Date
    Oct 2002
    Posts
    92

    SQL Syntax Help

    I have a field in a MS SQL 2000 database table with data type of 'NAME (varchar)', with the length of 64.

    I want to delete all values in this field that are 25 characters or longer.

    What would my syntax be ?

    Thank you !

  2. #2
    Join Date
    Dec 2004
    Posts
    502
    DELETE YourTable
    WHERE LEN(name) >= 25

  3. #3
    Join Date
    Oct 2002
    Posts
    92
    thanks NP...but it looks as if the criteria has changed.

    If the field is longer than 25 characters, then delete all the remaining characters, but keep the first 25 characters.

    Some data in this field has less then 25 characters, which is fine and should be left alone.

    So how do I write the query that only truncates after the 25th character ?...use rtrim ?

    thanks

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    then use update statement

    UPDATE YourTable
    set name = substring(name,1,25)
    WHERE DATALENGTH(name) >= 25

  5. #5
    Join Date
    Oct 2002
    Posts
    92
    Excellent, thank you for your time, I really appreciate it

  6. #6
    Join Date
    Dec 2004
    Posts
    502
    Be careful about using DATALENGTH. First of all, it will count trailing spaces, whereas LEN will not. I don't think this will be a problem with your issue. DATALENGTH will also return the number of bytes that a particular string uses, not necessarily the number of characters. Therefore, if your data is unicode (like nchar, nvarchar, ntext), DATALENGTH will return the number of bytes, which will be twice the number of characters. For example, 'abc' in nvarchar datatype will have a LEN of 3, but a DATALENGTH of 6. If you keep your data at char or varchar, then you won't run into this difference.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •