SQL select statement help

  • Hi,

    I have column with alphanumberic fields like:

    trader1.dbpt

    1234.mcxl

    abc243.cexl

    234-lab.cxcl

    There are following characteristics of this data:

    -fifth last place is always decimal

    -always four characters after the decimal

    What I want:

    -only those rows which have ".mcxl" at the end

    -rows should NOT have numbers in them

    -rows should not have "-" in them

    -want to sort according to the length, shortest should come first

    Please guide how to accomplish all this on MS Access in SQL statement

    thanks for the help,

    Silverspring

  • Create an mdb file. Link your table. Create queries as you want.

    .mcxl at end is : Create a column Right([fieldname],5) with criteria ".mcxl "

    finding - is : Create a column Instr([fieldname],"-") with criteria  0

    sort by length is : Create a column  LEN([Fieldname]) and sort ascending

    no numbers : need a bit of code. Create a module with a function

    Public Function fnNumeric(Special) as Boolean
    Dim a as Integer, b as integer
    fnNumeric = False
    a = len(Special)
    for b =1 to a
    if isnumeric(Mid(Special,a,1)) Then
    fnNumeric = True
    exit for
    next
    End Function

    In the query create a column fnNumeric([Special]) with criteria True.

    Most of this can be done in T SQL using most of the same functions. Instr is replaced by CharIndex and you can do the function  using IN(0,1,2,3,4,5,6,7,8,9) instead of IsNumeric.

  • this is completely untried & untested so you need to "play around with it"...

    select * from myTable where right(name, 4) = 'mcxl' and col like replicate('[^0-9-]', datalength(col) - charindex(col, '.'))
    







    **ASCII stupid question, get a stupid ANSI !!!**

  • Used the table of data shown below, in order to provide some rows that pass the tests:

    trader1.dbpt
    1234.mcxl
    abc243.cexl
    234-lab.cxcl
    shouldpass.mcxl
    shouldalsopass.mcxl

    shouldpasstoo.mcxl

    shouldnot-pass.mcxl

    This SQL

    --only those rows which have ".mcxl" at the end

    --rows should NOT have numbers in them

    --rows should not have "-" in them

    --want to sort according to the length, shortest should come first

    SELECT [MyField], len(MyField) as Length

    FROM [AlphaNumericData]

    where MyField Like '*.mcxl'

    and MyField Not like '*[0-9,-]*'

    Order by Len(MyField)

    Produces this result

    MyFieldLength
    shouldpass.mcxl15
    shouldpasstoo.mcxl18
    shouldalsopass.mcxl19

  • Very neat!

    Nice trick with "*[0-9,-]*"!

Viewing 5 posts - 1 through 4 (of 4 total)

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