Select (Analysis)

Summary

Extracts features from an input feature class or input feature layer, typically using a select or Structured Query Language (SQL) expression and stores them in an output feature class.

Usage

Syntax

Select_analysis (in_features, out_feature_class, {where_clause})
ParameterExplanationData Type
in_features

The input feature class or layer from which features are selected.

Feature Layer
out_feature_class

The output feature class to be created. If no expression is used, it contains all input features.

Feature Class
where_clause
(Optional)

An SQL expression used to select a subset of features. The syntax for the expression differs slightly depending on the data source. For example, if you're querying file or ArcSDE geodatabases, shapefiles, or coverages, enclose field names in double quotes:

"MY_FIELD"

If you're querying personal geodatabases, enclose fields in square brackets:

[MY_FIELD]

In Python, strings are enclosed in matching single or double quotes. To create a string that contains quotes (as is common with a WHERE clause in SQL expressions), you can escape the quotes (using a backslash) or triple quote the string. For example, if the intended WHERE clause is

"CITY_NAME" = 'Chicago'

you could enclose the entire string in double quotes, then escape the interior double quotes like this:

" \"CITY_NAME\" = 'Chicago' "

Or you could enclose the entire string in single quotes, then escape the interior single quotes like this:

' "CITY_NAME" = \'Chicago\' '

Or you could enclose the entire string in triple quotes without escaping:

""" "CITY_NAME" = 'Chicago' """

For more information on SQL syntax and how it differs between data sources, see the help topic SQL reference for query expressions used in ArcGIS.

SQL Expression

Code Sample

Select Example (Python Window)
import arcpy
from arcpy import env

env.workspace = "c:/basedata/roads.gdb"
arcpy.Select_analysis("nfroads", "paved", '[ROAD_CLASS] = "PAVED"')
Select Example (Python Window)

The following Python Window script demonstrates how to use the Select function in immediate mode.

import arcpy
from arcpy import env

env.workspace = "C:/data"
arcpy.Select_analysis("majorrds.shp", "C:/output/majorrdsClass4.shp", '"CLASS" = \'4\'')
Select Example 2 (stand-alone Python Script)

The following Python script demonstrates how to use the Select function in a stand-alone script.

# Name: Select_Example2.py
# Description: Select roads of Class 4 from major roads tin the gnatcatcher habitat study area
# Author: ESRI

# Import system modules
import arcpy
from arcpy import env

# Set workspace
env.workspace = "C:/data"

# Set local variables
in_features = "majorrds.shp"
out_feature_class = "C:/output/majorrdsClass4.shp"
where_clause = '"CLASS" = \'4\''

# Execute Select
arcpy.Select_analysis(in_features, out_feature_class, where_clause)

Environments

Related Topics

Licensing Information

ArcView: Yes
ArcEditor: Yes
ArcInfo: Yes

4/4/2012