Bala's Development Blog

SQL Server DateTime formatting


When you working with database one of most often problem you face is date time data. In this post I’m going to post code snippets that do different things with datetime data and links to the original posts!

Snippet 1
Converting datetime value to a given format using built in SQL Sever functions

select convert(varchar, getdate(), 10) -- returns 12-30-06
select convert(varchar, getdate(), 1)  -- returns 12/30/06

--Convert and replacing the divider
select replace(convert(varchar, getdate(),101),'/','')  -- returns 12302006

The source for this snippet
Link for MSDN :

Snippet 2
Determine a value is valid datetime

update myTable
set part2=replace(convert(varchar, (CAST(custref1 AS datetime)),102),'.','')
where isdate(custref1)=1
and sourceorderidpart2='CUSTREF1'

Optional Parameters and Named Arguments in C# 4


C# 4.0 now supports using optional parameters with methods, constructors, and indexers (note: VB has supported optional parameters for awhile).

This is how you declare your method to have optional parameter

public Boolean SendMail( string[] toAddresses, string subject, string body,string sender = "",bool isBodyHTML=false)
                MailMessage mailMessage = new MailMessage();
                mailMessage.From =new MailAddress(sender);
                mailMessage.IsBodyHtml = isBodyHTML;

                foreach (string address in toAddresses)
                    mailMessage.To.Add(new MailAddress(address));

                mailMessage.Subject = subject;
                mailMessage.Body = body;

                SmtpClient smtpClient = new SmtpClient();

                return true;
            catch (SmtpException ex)
                return false;
            catch (Exception ex)
                return false;

And this is how you call your method with optional parameters/named arguments

//I did't passed sender parameter value
mailer.SendMail(toAddresses:new string[] { "" },subject: "File Received", body:Encoding.UTF8.GetString(msg),isBodyHTML:true);

Source :