Click here to Skip to main content
15,744,304 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

In a datatable I need to save file extension by getting from a filename column within same datatable.

For this i am using sql expression as below

iif(@str like '%.%',reverse(left(reverse(@str), charindex('.', reverse(@str)) - 1)),'')

this need to be added in datacolumn default value

dt.Columns.Add("FileExtension", typeof(string), "iif([Resume Doc Name] like '%.%',reverse(left(reverse([Resume Doc Name]), charindex('.', reverse([Resume Doc Name])) - 1)),'')");

But above code is failing at reverse function.

Any suggestions to achieve this?


What I have tried:

Tried in different ways using sql expressions
Updated 6-Feb-18 5:51am
ZurdoDev 6-Feb-18 11:02am    
What do you mean by "failing"? Do you get an error message?
Richard MacCutchan 6-Feb-18 11:37am    
Why not just use CHARINDEX and RIGHT?

1 solution

As per documentation[^], a reverse function is unavailable. There's only set of function for expression column, i.e.: Convert, Len, IsNull, Iif, Trim and Substring.

If you would like to Reverse text, you may use Linq query. Example:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("CarType", typeof(string)));
dt.Rows.Add(new object[]{"Skoda", null});
dt.Rows.Add(new object[]{"Volkswagen", "Business"});
dt.Rows.Add(new object[]{"Mazda", "Personal"});
dt.Rows.Add(new object[]{"Renault", "Business"});

var result = dt.AsEnumerable()
		Name = x.Field<string>("Name"),
		CarType = x.Field<string>("CarType"),
		ReversedName = string.Join("", x.Field<string>("Name").Reverse())

Name       CarType  ReversedName
Skoda      null     adokS 
Volkswagen Business negawskloV 
Mazda      Personal adzaM 
Renault    Business tluaneR 
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900