Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:40:41+00:00 2026-06-17T08:40:41+00:00

I have the following data in mysql table: Spring Texas Corpus Christi Texas Orange

  • 0

I have the following data in mysql table:

Spring Texas
Corpus Christi Texas
Orange California
Los Angeles California

The right of the data will always be the state name. I want to run a sql query that shows only cities in California, and sets $city to an array of the city name portion of the returned string. Some cities have spaces such as los angeles and some don’t.

What is the best method to accomplish this to ensure I only get California cities, and only the city portion assigned to $city

Something like this, but is this the best way from an sql standpoint?

Select
   terms.name
From
  terms
Where
  terms.name Like '%Texas%'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:40:42+00:00Added an answer on June 17, 2026 at 8:40 am

    Try to normalize the data.

    If you can’t, set up a table with state names.

    SELECT c.col,
      TRIM(SUBSTR(c.col, 1, LENGTH(c.col) - LENGTH(c.state))) AS city,
      c.state AS state
    FROM (SELECT a.col,
        CASE
          WHEN b.state IS NULL
            THEN SUBSTRING_INDEX(a.state, ' ', -1)
          ELSE b.state
        END AS state
      FROM
        (SELECT col,
          CASE
            WHEN LENGTH(col) - LENGTH(REPLACE(col, ' ', '')) + 1 = 2
              THEN SUBSTRING_INDEX(col, ' ', -1)
            WHEN LENGTH(col) - LENGTH(REPLACE(col, ' ', '')) + 1 > 2
              THEN SUBSTRING_INDEX(col, ' ', -2)
            ELSE NULL
          END AS state
        FROM bad_data) a
      LEFT JOIN state_names b ON b.state = a.state) c
    

    Result

    |                  COL |           CITY |      STATE |
    ------------------------------------------------------
    |         Spring Texas |         Spring |      Texas |
    | Corpus Christi Texas | Corpus Christi |      Texas |
    |    Orange California |         Orange | California |
    |    New York New York |       New York |   New York |

    See the demo

    If you can’t set up a table, then this query should do it:

    SELECT c.col,
      TRIM(SUBSTR(c.col, 1, LENGTH(c.col) - LENGTH(c.state))) AS city,
      c.state AS state
    FROM (SELECT a.col,
        CASE
          WHEN b.state IS NULL
            THEN SUBSTRING_INDEX(a.state, ' ', -1)
          ELSE b.state
        END AS state
      FROM
        (SELECT col,
          CASE
            WHEN LENGTH(col) - LENGTH(REPLACE(col, ' ', '')) + 1 = 2
              THEN SUBSTRING_INDEX(col, ' ', -1)
            WHEN LENGTH(col) - LENGTH(REPLACE(col, ' ', '')) + 1 > 2
              THEN SUBSTRING_INDEX(col, ' ', -2)
            ELSE NULL
          END AS state
        FROM bad_data) a
      LEFT JOIN (SELECT 'Alabama' AS state 
                      UNION ALL 
                      SELECT 'Arizona' 
                      UNION ALL 
                      SELECT 'Arkansas' 
                      UNION ALL 
                      SELECT 'California' 
                      UNION ALL 
                      SELECT 'Colorado' 
                      UNION ALL 
                      SELECT 'Connecticut' 
                      UNION ALL 
                      SELECT 'Delaware' 
                      UNION ALL 
                      SELECT 'Florida' 
                      UNION ALL 
                      SELECT 'Georgia' 
                      UNION ALL 
                      SELECT 'Guam' 
                      UNION ALL 
                      SELECT 'Hawaii' 
                      UNION ALL 
                      SELECT 'Idaho' 
                      UNION ALL 
                      SELECT 'Illinois' 
                      UNION ALL 
                      SELECT 'Indiana' 
                      UNION ALL 
                      SELECT 'Iowa' 
                      UNION ALL 
                      SELECT 'Kansas' 
                      UNION ALL 
                      SELECT 'Kentucky' 
                      UNION ALL 
                      SELECT 'Louisiana' 
                      UNION ALL 
                      SELECT 'Maine' 
                      UNION ALL 
                      SELECT 'Maryland' 
                      UNION ALL 
                      SELECT 'Massachusetts' 
                      UNION ALL 
                      SELECT 'Michigan' 
                      UNION ALL 
                      SELECT 'Minnesota' 
                      UNION ALL 
                      SELECT 'Mississippi' 
                      UNION ALL 
                      SELECT 'Missouri' 
                      UNION ALL 
                      SELECT 'Montana' 
                      UNION ALL 
                      SELECT 'Nebraska' 
                      UNION ALL 
                      SELECT 'Nevada' 
                      UNION ALL 
                      SELECT 'New Hampshire' 
                      UNION ALL 
                      SELECT 'New Jersey' 
                      UNION ALL 
                      SELECT 'New Mexico' 
                      UNION ALL 
                      SELECT 'New York' 
                      UNION ALL 
                      SELECT 'North Carolina' 
                      UNION ALL 
                      SELECT 'North Dakota' 
                      UNION ALL 
                      SELECT 'Ohio' 
                      UNION ALL 
                      SELECT 'Oklahoma' 
                      UNION ALL 
                      SELECT 'Oregon' 
                      UNION ALL 
                      SELECT 'Pennsylvania' 
                      UNION ALL 
                      SELECT 'Puerto Rico' 
                      UNION ALL 
                      SELECT 'Rhode Island' 
                      UNION ALL 
                      SELECT 'South Carolina' 
                      UNION ALL 
                      SELECT 'South Dakota' 
                      UNION ALL 
                      SELECT 'Tennessee' 
                      UNION ALL 
                      SELECT 'Texas' 
                      UNION ALL 
                      SELECT 'Utah' 
                      UNION ALL 
                      SELECT 'Vermont' 
                      UNION ALL 
                      SELECT 'Virginia' 
                      UNION ALL 
                      SELECT 'Washington' 
                      UNION ALL 
                      SELECT 'West Virginia' 
                      UNION ALL 
                      SELECT 'Wisconsin' 
                      UNION ALL 
                      SELECT 'Wyoming') b ON b.state = a.state) c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
I have to insert some data into a MySQL table. The data will be
So im having a problem (obviously). I have the following MySQL table data 7
I have a category table in MySql database that holds the following data. id
I have following query and data saved in a MySQL database with a timestamp
I have the following code: Imports MySql.Data.MySqlClient Imports MySql.Data.Types Public Class FormMain Private Sub
I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
I have following data in excel table r1 r2 r3 r4 r5 v1 v2
I have a mysql table with over 4 million of data; well the problem
I have the following mysql table called pics, with the following fields and sample

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.