sql create a field from a field

前端 未结 4 923
旧时难觅i
旧时难觅i 2021-01-23 18:48

I need to run a query that would pull information from a field that has 2 types of data .

Field is address and has 123 avenue as data and bb@yahoo.com.

I need to

相关标签:
4条回答
  • 2021-01-23 19:01

    Here is the query that fetches the two different fields:

    select iif(field like '*@*', field, NULL) as email,
           iif(field like '*@*, NULL, field) as address
    from t
    

    The usage of like in Access is a bit different from other databases.

    I would suggest that you create a view with this logic. If you actually want to modify the table, you will have to add columns and populate them with logic like the above.

    0 讨论(0)
  • 2021-01-23 19:14

    Try something like this:

    select *, 
           (case locate('@', address) when 0 then null else address) as email,
           (case locate('@', address) when 0 then address else null) as street
    from table;
    

    You'd probably need to adjust the name of "locate" function - I'm not sure if it is the same in access database.

    0 讨论(0)
  • 2021-01-23 19:15

    Based on this question and your duplicate question, I understand your table has a field which includes both the street address and email address and you want to split those into separate fields.

    So your table includes this ...

    YourField
    ------------------------------
    1234 ave willie haha@yahoo.com
    123 avenue bb@yahoo.com
    

    And you want this ...

    YourField                       street_address  email_address
    ------------------------------  --------------- --------------
    1234 ave willie haha@yahoo.com  1234 ave willie haha@yahoo.com
    123 avenue bb@yahoo.com         123 avenue      bb@yahoo.com
    

    If that is correct, you can use the InstrRev() function to determine the position of the last space in YourField. Everything before the last space is the street address; everything after is the email address.

    SELECT
        y.YourField,
        Left(y.YourField, InstrRev(y.YourField, ' ') -1) AS street_address,
        Mid(y.YourField, InstrRev(y.YourField, ' ') +1) AS email_address
    FROM YourTable AS y;
    

    You may need to add a WHERE clause to ensure the query only tries to evaluate rows which include your expected YourField value patterns.

    0 讨论(0)
  • 2021-01-23 19:19

    If you have a street address and then an email address in the same field, and want to split them, then use this.
    We'll call your original field Addy, and the new fields Street & Email.

    SELECT Left(Addy, InStrRev(Addy, " ", InStr(Addy, "@")) - 1) AS Street, mid(Addy, InStrRev(Addy, " ", InStr(Addy, "@")) + 1) AS Email.
    

    What this does is look for the @, then go backwards and look for the first space. The first function takes every left of that space and calls it Street, while the second function takes everything to the right.

    The function is really 3 functions nested together. To really understand what it's doing, I'll dissect it.

    I = InStr(Addy, "@")
    J = InStrRev(Addy, " ", I)
    Street = Left(Addy,J-1)
    
    0 讨论(0)
提交回复
热议问题