I have a table with a column that contains the entire file name, the data looks like:
FilePath FileSize
------------------------------ --------
dsch2_chs_wyj.rar 694KB
AllInOneHash.rar 19KB
FilePropertyModifier.rar 12KB
jquery_1_7_api_chm_chs.rar 285KB
startupmgr.rar 38KB
JQueryTableExample_Edi.rar 33KB
hdpartioncalc_csharp.rar 49KB
XMLMenuTest.zip 3KB
Now I want to extract the file extension name, like .rar
and insert into a new table.
INSERT INTO Attachment
(
Id,
[FileName],
ExtensionName,
StoredPath,
CreateOn,
UploaderIP,
DataBinary
)
SELECT ba.Id,
ba.Title,
'{Extension Name}',
ba.FilePath,
GETDATE(),
NULL,
NULL
FROM BlogAttachment ba
But T-SQL doesn't have a LastIndexOf()
function. How can I cut the substring in an easy way?
7条答案
按热度按时间tgabmvqs1#
You can get the extension by doing:
However, I would recommend that you check that there is a
'.'
in the name first:ha5z0ras2#
Demo
ufj5ltwl3#
When looking for specific extensions, and dealing with texts that do have many dots, the following provides a more specific approach.
pftdvrlh4#
You can get file extension in this way:
And You can get file name in this way:
bxjv4tth5#
Its too late to answer but i found the solution how can get extension of an image or file from sql column and also get any string after any keyword or "/"or ".".... just replace the "." with your keyword.
4szc88ey6#
This will work with file names and urls (without querystrings).
If you want to do it quick and dirty inline:
I recommend creating a scalar function. That way you can add more business logic to handle query strings or what have you.
To create a scalar function:
and then your query would simply look like this
As a side note, if you are using SSMS, you can press CNTL-M to populate the <Something,,> variables
kfgdxczn7#