Even after many years of SQL development, I still can be surprised sometimes!
Note: the following article is using MS SQL 2008. I have not checked this in SQLite, MySQL, or any other MS SQL version.
Often, I have to write queries to find varchars that are similar to a string. For example:
Select [Name]
From [User]
Where [Name] like '%Theah%'
Would select all Names from the User table that have Theah somewhere in it.
I've rarely needed anything more complicated than that. Recently though, I've come across an issue where I need to query all varchars that are only numbers. They were to be converted and placed in another table.
Here is the query, which will query all [ExampleColumn]s in [ExampleTable] that can be converted into bigints. Then, it will cast them to bigint.
Select CAST([ExampleColumn] as bigint)
From [ExampleTable]
Where [ExampleColumn] not like '%[^0-9]%'
With the ability to add Regular Expressions like [^0-9] into a like filter, you have a very powerful, and in some cases, efficient, method to filter results.
Note: the following article is using MS SQL 2008. I have not checked this in SQLite, MySQL, or any other MS SQL version.
Often, I have to write queries to find varchars that are similar to a string. For example:
Select [Name]
From [User]
Where [Name] like '%Theah%'
Would select all Names from the User table that have Theah somewhere in it.
I've rarely needed anything more complicated than that. Recently though, I've come across an issue where I need to query all varchars that are only numbers. They were to be converted and placed in another table.
Here is the query, which will query all [ExampleColumn]s in [ExampleTable] that can be converted into bigints. Then, it will cast them to bigint.
Select CAST([ExampleColumn] as bigint)
From [ExampleTable]
Where [ExampleColumn] not like '%[^0-9]%'
With the ability to add Regular Expressions like [^0-9] into a like filter, you have a very powerful, and in some cases, efficient, method to filter results.