@Mahedi's answer set me off down the right path. I can't use CHARINDEX()
but can use locate()
using this answer https://stackoverflow.com/a/19007026/1268941
a combination of substr() and locate() can be used:
substr(column_name,1,locate('.',column_name))
however because this keeps the trailing .
I can use:
substr(column_name,1,locate('.',column_name)-1)
but this leads to "numeric argument of a built-in string function is out of range" because locate is returning -1
in some intances, of which substr()
apprently does not accept as a position parameter.
using a case statement you can run the above logic on only cells that contain the .
select case when locate('.',column_name) > 0 then
substr(column_name,1,locate('.',column_name)-1) else
column_name end as newcol from mytable;
you can also reduce this slightly with LEFT
select case when locate('-',column_name) > 0 then
left(column_name,locate('.',column_name)-1) else
column_name end as newcol from mytable;