Search for a specific character length

  • Hello all. Is there a way to search for a string that contains a specific number of characters?

    I have a project that will be inserting barcode values into the db. One requirement is the ability to query the barcode db for barcode entries that are a specific length, 14 characters.

    Any ideas?

    Thank you.

    ks


    Kevin S.

  • You can execute:

    SELECT * FROM TableName

    WHERE LEN(BarCode) = 14

  • quote:


    You can execute:

    SELECT * FROM TableName

    WHERE LEN(BarCode) = 14

    Thank you so much my friend!


    ks


    Kevin S.

  • If the table is very large, and speed becomes a problem, you might want to add a BARCODE_LEN field to the table, and index that field. You could keep the field up to date with insert and update triggers

    RD Francis


    R David Francis

  • quote:


    If the table is very large, and speed becomes a problem, you might want to add a BARCODE_LEN field to the table, and index that field. You could keep the field up to date with insert and update triggers

    RD Francis

    Oh! Excellent. I am fairly new to T-SQL (2 years of mild use) and I appreciate all of your inputs.

    Thank you.


    ks


    Kevin S.

  • quote:


    If the table is very large, and speed becomes a problem, you might want to add a BARCODE_LEN field to the table, and index that field...


    I hate to be a nay-sayer, but this strategy probably won't help much. The selectivity of the data in the BARCODE_LEN field would be so minimal (maybe a variation of 5 or 6 data values?) that an index probably wouldn't help much. Having the data field there as a calculated field will only eliminate the calculation of the LEN function on the BarCode field, which is probably not an extensive use of resources. As a side bar, it might be an interesting test to populate a table with millions of rows and see at what point a calculated field of datalength would significantly improve a query's performance. rdfozz, feel up to it?

  • Is the requirement of n characters the only requirement?

    How about a view and two tables, one for n character length barcodes and the other for the none n length barcodes.

    Insert on view with an instead trigger.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply