Wednesday, June 19, 2013

Cleaner code with SQL Coalesce

ver 1.

if @runDate != null
  set @rd = CONVERT(VARCHAR(10),@runDate,101)
else
  set @rd = CONVERT(VARCHAR(10),getdate(),101)

set @rd = replace(@rd,'/','')



ver 2.

set @rd = replace(CONVERT(VARCHAR(10),COALESCE(@runDate,getdate()),101),'/','')


Coalesce takes in a list of values,  it will return the first non null value.

I also wrapped the replace call around the results of the convert statement.


More on MSDN: COALESCE (Transact-SQL)

Thursday, June 6, 2013

Sorting by last modified date of files







 private static void GetFileDates()
        {
            DirectoryInfo di = new DirectoryInfo(_path);
            FileSystemInfo[] files = di.GetFileSystemInfos();
            var orderedFiles = files.OrderBy(f => f.LastWriteTime);
            FileSystemInfo first = (FileSystemInfo)orderedFiles.First();
            FileSystemInfo last = (FileSystemInfo)orderedFiles.Last();

            _startDate = first.LastWriteTime.ToString();
            _stopDate = last.LastWriteTime.ToString();

           
        }