简体   繁体   中英

How to create a query by using a substring of a field as argument?

I want to create a selection in SQL Server where the zipcodes are between 3000 and 3100. The zipcodes are with numeric and alfanumeric values.

How can you create a select statement like this

SELECT * 
FROM TABLE 
WHERE SUBSTRING(A.Zipcode, 1, 4) as integer is between 3000 and 3100
SELECT *
FROM TABLE
WHERE CAST(SUBSTRING(A.Zipcode, 1, 4) AS INT) BETWEEN 3000
        AND 3100

FuzzyTree's comment using CONVERT will work too. CAST and CONVERT perform very similar functions, but CONVERT offers a more flexible output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM