I'm using this in a SQL query in SQL Server Management Studio, so that if the column dbo.Material_Trans.Lot has a dash the dash and all the string to the right is removed.
SUBSTRING(dbo.Material_Trans.Lot, 0, CHARINDEX('-', dbo.Material_Trans.Lot))
However, if the dbo.Material_Trans.Lot column does not contain a '-', then it returns nothing. I would like an IIF statement that if the dbo.Material_Trans.Lot does not contain a '-' return the dbo.Material_Trans.Lot column, else do the
SUBSTRING(dbo.Material_Trans.Lot, 0, CHARINDEX('-', dbo.Material_Trans.Lot))
Thank you!
I have tried the IIF and it fails within SQL as invalid statement.
You can create a unique constraint over a generated column. For example:
Result:
See running example at db<>fiddle.