Removing part of a string - SQL Server

2

I have written the name of a file with extension (FILE.txt) in a database column. I need to show in a SELECT only the FILE instead of ARCHIVE.txt

Does anyone know how to do it?

    
asked by anonymous 05.01.2017 / 19:05

2 answers

3

If you know exactly which extension, you can use REPLACE

SELECT REPLACE(file, '.txt', '') FROM table;

link

or, if they are multiple extensions, then a SUBSTRING is better:

link

SELECT SUBSTRING(file, -4, 4) FROM table;
    
05.01.2017 / 19:11
1

I've done an example that caters to several cases. Example: file.txt, file.secret.txt, file.jpeg.

Declare @campo varchar(100) = 'Arquivo.txt'
select left(@campo,len(@campo) - charindex('.',reverse(@campo)) ) resultado
    
05.01.2017 / 19:28