Results 1 to 7 of 7
  1. #1
    bcurrey is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    9

    Using SQL to extract certain values

    I’ve been able to partially figure out the solution here, but can’t quite get all of it. I have merged 15 spreadsheets into 1 Access DB using SQL. I have one column of data that has a varying number of characters. I need to extract the numbers from it and place in separate columns.

    Here’s the query I’m currently using:

    Select


    [bsc].[Warranty SKU]
    , Mid([bsc].[Warranty SKU], Instr(1,[bsc].[Warranty SKU], "$") + 1) AS MinPrice
    , Mid([bsc].[Warranty SKU], InStr(1,[bsc].[Warranty SKU], "-") + 1) as MaxPrice
    From bsc


    Here’s the data my query returns.

    Warranty Sku MinPrice MaxPrice
    MickeyD Hamburgers SVC 36 MOS $0-99 P 0-99 P 99 P
    MickeyD Hamburgers SVC 36 MOS $10000-14999 P 10000-14999 P 14999 P
    MickeyD Hamburgers SVC 36 MOS $1000-1999 P 1000-1999 P 1999 P
    MickeyD Hamburgers SVC 36 MOS $100-199 P 100-199 P 199 P
    MickeyD Hamburgers SVC 36 MOS $15000-29999 P 15000-29999 P 29999 P
    SMBBB SJ VU LIFETM JWLRY $150-199 P 1500-1999 P 1999 P
    SAMS Burger SVC 36M $500-4999 P 500-4999 P 4999 P
    NEX Hamburgers SVC 24 MOS $0-99 P 0-99 P 99 P
    AAFES Hamburgers SVC 24 MOS $1000-2999 P 1000-2999 P 2999 P
    MickeyD LIFETIME HAMB SV $2000-2999 P 2000-2999 P 2999 P
    MickeyD LIFETIME HAMB SV $200-499 P 200-499 P 499 P




    Here’s the data format that I want to get. Any help would be much appreciated!!!


    Warranty Sku MinPrice MaxPrice
    MickeyD Hamburgers SVC 36 MOS $0-99 P 0 99
    MickeyD Hamburgers SVC 36 MOS $10000-14999 P 10000 14999
    MickeyD Hamburgers SVC 36 MOS $1000-1999 P 1000 1999
    MickeyD Hamburgers SVC 36 MOS $100-199 P 100 199
    MickeyD Hamburgers SVC 36 MOS $15000-29999 P 15000 29999
    SMBBB SJ VU LIFETM JWLRY $150-199 P 1500 1999
    SAMS Burger SVC 36M $500-4999 P 500 4999
    NEX Hamburgers SVC 24 MOS $0-99 P 0 99
    AAFES Hamburgers SVC 24 MOS $1000-2999 P 1000 2999
    MickeyD LIFETIME HAMB SV $2000-2999 P 2000 2999
    MickeyD LIFETIME HAMB SV $200-499 P 200 499
    750 999

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    The Mid() function has another argument you aren't providing, the number of characters. You can use InStr() on both the $ and - and subtract to get the number of characters. The Val() function should help with the second.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    bcurrey is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    9
    Thank you Paul. That helped! I made a bit more progress after your help, but I have no idea how to go any further. Can you tell me how I would apply the VAL function to what I already have? Really appreciate it.


    , Mid([bsc].[Warranty SKU], Instr(1,[bsc].[Warranty SKU], "$") + 1, Len([bsc].[Warranty SKU]) - Instr([bsc].[Warranty SKU], '-')-2)AS MinPrice
    , Mid([bsc].[Warranty SKU], InStr(1,[bsc].[Warranty SKU], "-") + 1) AS MaxPrice



    Warranty Sku MinPrice MaxPrice
    MickeyD Hamburgers SVC 36 MOS $0-99 P 0- 99 P
    MickeyD Hamburgers SVC 36 MOS $10000-14999 P 10000 14999 P
    MickeyD Hamburgers SVC 36 MOS $1000-1999 P 1000 1999 P
    MickeyD Hamburgers SVC 36 MOS $100-199 P 100 199 P
    MickeyD Hamburgers SVC 36 MOS $15000-29999 P 15000 29999 P

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I was thinking

    Val(Mid([bsc].[Warranty SKU], InStr(1,[bsc].[Warranty SKU], "-") + 1)) AS MaxPrice
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Maybe something lilke below.

    Mid([bsc].[Warranty SKU], Instr(1,[bsc].[Warranty SKU], "$") + 1, (Instr(1,[bsc].[Warranty SKU], "-")) - (Instr([bsc].[Warranty SKU], '$') +1)) AS MinPrice

    and pbaldy line for the MaxPrice or:

    Mid([bsc].[Warranty SKU], Instr(1,[bsc].[Warranty SKU], "-") + 1, (Instr(1,[bsc].[Warranty SKU], " ")) - (Instr([bsc].[Warranty SKU], '-') +1)) AS MaxPrice

  6. #6
    bcurrey is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    9
    Sweet! I've never used that before! Another new item to put in the tool belt. Really appreciate it!

  7. #7
    bcurrey is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    9
    Hello again. The SQL worked create and did exactly what I needed. However, I'm not able to filter on the fields that were created. I get an error message that says "Enter a valid value" when I enter a value of > 200. If I enter greater than "200", I get the same message. Any ideas on what I'm doing wrong here? Thanks!


    Here's my code:

    SELECT bsc.*
    , (Val(Mid([bsc].[Warranty SKU],InStr(1,[bsc].[Warranty SKU],"$")+1,Len([bsc].[Warranty SKU])-InStr([bsc].[Warranty SKU],'-')-2))) AS MinPrice
    , (Val(Mid([bsc].[Warranty SKU],InStr(1,[bsc].[Warranty SKU],"-")+1))) AS MaxPrice

    FROM (SELECT* FROM Rogers

    UNION

    Select * from AAFES

    ) AS bsc;

Please reply to this thread with any new information or opinions.

Similar Threads

  1. extract multiple values from a cell to a separate table?
    By benlogo in forum Import/Export Data
    Replies: 4
    Last Post: 08-18-2015, 06:35 AM
  2. Replies: 1
    Last Post: 05-24-2013, 02:44 AM
  3. Replies: 1
    Last Post: 06-08-2012, 02:45 PM
  4. Extract String From Between 2 Values
    By kathleencampbell in forum Queries
    Replies: 5
    Last Post: 03-23-2012, 10:52 AM
  5. Extract certain values from field
    By tylerg11 in forum Programming
    Replies: 19
    Last Post: 09-23-2011, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums