Wednesday, 20 November 2013

IIS Error- Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).


For this fallow the fallowing steps-

 Enable the ASP.NET authentication for default website (in IIS) then create Desktop folder at C:\Windows\System32\config\systemprofile\Desktop.

step1 :  Go to run type dcomcnfg 
Step2:   Click >Component services >Computes >My Computer>Dcom config> and select microsoft Excel Application> 
Step3:   Right Click on Microsoft Excel Application>Properties>Security Tab
Step4-Under-Launch and activation permission->Customise->Edit->Add--type-(IIS_IUSRS) 
-> ok -> Allow Permission-Local Lounch and Local Activation
Step5- Repeate Step4 for Under Access Permission

Reference--http://www.youtube.com/watch?v=ijfb3E_7t3o

IIS-Error Resolve--Microsoft Excel cannot open or save any more documents because there is not enough available memory or disk space. • To make more memory available, close workbooks or programs you no longer need. • To free disk space, delete files you no longer need from the disk you are saving to.


For this fallow the fallowing steps-
step1 :  Go to run type dcomcnfg 
Step2:   Click >Component services >Computes >My Computer>Dcom config> and select micro soft Excel Application> 
Step3:   Right Click on Microsoft Excel Application>Properties>Give Asp.net Permissions 
Step 4: Select Identity table >Select interactive user >select ok

Friday, 15 November 2013

Restrict Ajax Calender future and past date using javascript

<script type="text/ecmascript">

    function checkDates(sender, args) {
       // alert(sender._selectedDate + "    enter    " + new Date());
        if (sender._selectedDate > new Date()) {
            alert('Date must be less than Today');
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }
</script >

//HTML
 <asp:TextBox runat="server"  ID="dtForm"  Enabled="False" ></asp:TextBox>

<asp:CalendarExtender ID="CalendarExtender1" runat="server" PopupButtonID="cal_Surveyor"
TargetControlID="dtForm" OnClientDateSelectionChanged="checkDates"  Enabled="True" Format="dd/MM/yyyy"></asp:CalendarExtender>

Wednesday, 30 October 2013

Print Data Table data into Excel in c#.net

NameSpace--

using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;

Source Code--

 private void printExcelReport(DataTable dtFarmDetail)
        {
            //Declare Excel variable

            object misValue = System.Reflection.Missing.Value;
            Excel.Application xlApp = new Excel.Application();
            Excel.Worksheet xlWorkSheet = new Microsoft.Office.Interop.Excel.Worksheet();
            Excel.Workbook xlWorkBook;
            Excel.Range chartRange;

            //Declare value
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            int Rowcount = dtFarmDetail.Rows.Count;
            int Colcount = dtFarmDetail.Columns.Count;

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            //Write Header
            for (int p = 1; p <= dtFarmDetail.Columns.Count; p++)
            {
                xlWorkSheet.Cells[1, p] = dtFarmDetail.Columns[p - 1].ColumnName;
            }

            //Assing data in array
            object[,] arrFarm = new object[dtFarmDetail.Rows.Count, dtFarmDetail.Columns.Count];
            for (int i = 0; i < dtFarmDetail.Rows.Count; i++)
            {
                for (int j = 0; j < dtFarmDetail.Columns.Count; j++)
                {
                    string cellvalues = dtFarmDetail.Rows[i][j].ToString();
                    string tickcross = "";
                    if (j > 10)
                    {
                        if (cellvalues == "Yes")
                        {
                            tickcross = "ü";
                        }
                        if (cellvalues == "No" || cellvalues == "")
                        {
                            tickcross = "û";
                        }                      
                    }
                    else
                    { tickcross = cellvalues; }

                    arrFarm[i, j] = tickcross;
                }
            }

            //Write data in sheet
            if (dtFarmDetail.Rows.Count > 0)
            {
                xlWorkSheet.Range[xlWorkSheet.Cells[2,1], xlWorkSheet.Cells[dtFarmDetail.Rows.Count, dtFarmDetail.Columns.Count]] = arrFarm;
                dtFarmDetail.Rows.Clear();
            }

            //Color header
            chartRange = xlWorkSheet.get_Range((object)xlWorkSheet.Cells[1, 1], (object)xlWorkSheet.Cells[1, Colcount]);
            chartRange.get_Range((object)xlWorkSheet.Cells[1, 1], (object)xlWorkSheet.Cells[1, Colcount]).Font.Bold = true;
            chartRange.get_Range((object)xlWorkSheet.Cells[1, 1], (object)xlWorkSheet.Cells[1, Colcount]).Font.Size = 12;
            chartRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver);
           
            //Set Alingment
            chartRange = xlWorkSheet.get_Range((object)xlWorkSheet.Cells[1, 1], (object)xlWorkSheet.Cells[2 + Rowcount, Colcount]);
            chartRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;

            //Assign font and size
            chartRange = xlWorkSheet.get_Range((object)xlWorkSheet.Cells[1, 1], (object)xlWorkSheet.Cells[11 + Rowcount, Colcount]);
            chartRange.get_Range((object)xlWorkSheet.Cells[2, 12], (object)xlWorkSheet.Cells[11 + Rowcount, Colcount]).Font.Name = "Wingdings";
            chartRange.get_Range((object)xlWorkSheet.Cells[2, 12], (object)xlWorkSheet.Cells[11 + Rowcount, Colcount]).Font.Bold = true;
            chartRange.get_Range((object)xlWorkSheet.Cells[2, 12], (object)xlWorkSheet.Cells[11 + Rowcount, Colcount]).Font.Size = 12;
            chartRange.get_Range((object)xlWorkSheet.Cells[2, 12], (object)xlWorkSheet.Cells[11 + Rowcount, Colcount]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;

            //write sheet
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlThin;
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideHorizontal).Weight = Excel.XlBorderWeight.xlThin;
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideVertical).Weight = Excel.XlBorderWeight.xlThin;
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeTop).Weight = Excel.XlBorderWeight.xlThin;
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeLeft).Weight = Excel.XlBorderWeight.xlThin;
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeRight).Weight = Excel.XlBorderWeight.xlThin;
            xlWorkSheet.Cells.EntireColumn.AutoFit();

            xlApp.Visible = true;
            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);
            xlWorkSheet = null;
            xlWorkBook = null;
            xlApp = null;
            System.GC.Collect();
       
        }

Saturday, 19 October 2013

Create Log file in asp.net


On Event--
 string thisDir = Server.MapPath("./ExLogFile");
            //String Format : DateTime,Action,Userid,UserRole,Status,Attempt, FileName,empid,empname, username, adminactivity
            string logMessage = DateTime.Now.ToUniversalTime() + "," + "Logout," + Session["UName"] + "," + Session["user_RoleName"] + "," + "logged out" + "," + "," + "," + "," + "," + "," + "," ;
            writeToLogFile(logMessage, thisDir);


Method-


 public static void writeToLogFile(string logMessage, string thisDir)
    {
        StreamWriter swLog=null;
        try
        {
            string strLogMessage = string.Empty;
            string strLogFile = "" + thisDir + "\\logFile.txt";
         
            //strLogMessage = string.Format("{0}: {1}", DateTime.Now, logMessage);


            strLogMessage = string.Format(logMessage);

            if (Directory.Exists(thisDir)) { }
            else { System.IO.Directory.CreateDirectory(thisDir); }

            if (!File.Exists(strLogFile))
            {
                swLog = new StreamWriter(strLogFile);
            }
            else
            {
                swLog = File.AppendText(strLogFile);
            }

            swLog.WriteLine(strLogMessage);
            swLog.WriteLine();
            swLog.Close();
            swLog.Dispose();
        }
        catch (Exception)
        {
            swLog.Dispose();
            throw;
        }
        finally { swLog.Dispose(); }

    }

Single User login at a time any where with cache in asp.net

On Login Page-
Globle Define--  public System.Data.DataTable dtSessionGlobal = new DataTable();

  if (Cache["UserSessions"] != null)
        {
            dtSessionGlobal = (DataTable)Cache["UserSessions"];
        }
        else
        {
            dtSessionGlobal.Columns.Add("UserName");
            dtSessionGlobal.Columns.Add("SessionID");
            Cache["UserSessions"] = dtSessionGlobal;
        }

        EventArgs ee = new EventArgs();
        if (Session["username"] == null)
        {        
            btnsessionout_Click(sender, ee);
            //Response.Redirect("~/SignIn Users", false);
        }


On Logout Button-

 protected void btnsessionout_Click(object sender, EventArgs e)
    {
        string UserName =Session["UName"]==null ? "" : Session["UName"].ToString();
        Application[UserName] = null;
        if (dtSessionGlobal.Rows.Count > 0)
        {

            System.Data.DataRow[] dr = dtSessionGlobal.Select(" UserName='" + Session["UName"] + "'");// AND SessionID='" + Session.SessionID + "'
            if (dr.Length > 0)
            {
                foreach (DataRow drRow in dr)
                {
                    dtSessionGlobal.Rows.Remove(drRow);
                }
            }
        }
     
        Cache["UserSessions"] = dtSessionGlobal;

    }




At All Pages-

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtSessionGlobal = new DataTable();
        if (Cache["UserSessions"] != null)
        {
            dtSessionGlobal = (DataTable)Cache["UserSessions"];
        }
     
        if (!IsPostBack)
        {
            if (dtSessionGlobal.Rows.Count > 0)
            {
                System.Data.DataRow[] dr = dtSessionGlobal.Select(" UserName='" + Session["UName"] + "' AND SessionID<>'" + Session.SessionID + "'");
                if (dr.Length > 0)
                {
                    Response.Redirect("~/SignIn Users");
                }
                else if (dtSessionGlobal.Select(" UserName='" + Session["UName"] + "'").Length == 0)
                {
                    Response.Redirect("~/SignIn Users");
                }
            }
            else
            {
                Session.Abandon();
                Response.Redirect("~/SignIn Users");
            }
}

Restrict to direct access file and folder from url in web.config

<system.webServer>
 <security>
            <requestFiltering>
                <hiddenSegments>
                    <add segment="Folder Name" />
                    <add segment="Folder Name" />
                    <add segment="Folder Name" />
                    <add segment="Folder Name" />
                    <add segment="Folder Name" />
                    <add segment="Folder Name" />
                    <add segment="Folder Name" />
                </hiddenSegments>
            </requestFiltering>
        </security>  

</system.webServer>

Friday, 18 October 2013

Redirect to custom error page when server or application get error 404 or 403 or 405

for Custorm

<system.web>
<customErrors mode="On" defaultRedirect="~/Error-page not found">
     <error statusCode="404"  redirect="~/Error-page not found" />
      <error statusCode="403" redirect="~/Error-page not found" />
      <error statusCode="500" redirect="~/Error-page not found" />
    </customErrors>
</system.web>




for Http-

<system.webServer>
 <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" prefixLanguageFilePath="" path="/RFBij/Error-page not found" responseMode="ExecuteURL" />
      <remove statusCode="403" subStatusCode="-1" />
      <error statusCode="403" prefixLanguageFilePath="" path="/RFBij/Error-page not found" responseMode="ExecuteURL" />
      <remove statusCode="405" subStatusCode="-1" />
      <error statusCode="405" prefixLanguageFilePath="" path="/RFBij/Error-page not found" responseMode="ExecuteURL" />
    </httpErrors>
</system.webServer>

Friday, 4 October 2013

Asp.Net Validation Summary: How to show ValidationSummary using JavaScript

ValidationSummaryOnSubmit(); function exposed at client side.

<script type="text/javascript" language="javascript">

function PerformValidation() {
              ValidatorValidate(document.getElementById('rfvName'));
              ValidationSummaryOnSubmit();
 }



</script>
 
Note:  ValidationSummaryOnSubmit takes an argument as validationGroup. If you want to show ValidationSummary for a particular validation group, then call function passing name of validation group.
If function is called without any arguments, then it will show all ValidationSummary (if error) in page.

Sunday, 29 September 2013

Prevent to web serveice on browser url in asp.net

in web.config file
- <system.web>
    <customErrors mode="On" defaultRedirect="~/CustomeErrorPage.aspx?"/>
<!--<sessionState mode="InProc" cookieless="false" timeout="15"/>-->
    <pages validateRequest="false"></pages>
    <webServices>
      <protocols>
        <remove name="Documentation" />
      </protocols>
    </webServices>
  </system.web>

Prevent show previous pages after logout in asp.net

protected void logout_OnClick(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("login.aspx");
}

on master page -

protected void Page_Init(object sender, EventArgs e
{
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
}

Friday, 27 September 2013

Ajax Please wait image using update panel in asp.net c#

<asp:UpdateProgress ID="UpdateProgress" runat="server">
<ProgressTemplate>
<asp:Image ID="Image1" ImageUrl="~/Images/loading.gif" AlternateText="Processing" runat="server" />
</ProgressTemplate>
</asp:UpdateProgress>

<asp:ModalPopupExtender ID="modalPopup" runat="server" TargetControlID="UpdateProgress"
PopupControlID="UpdateProgress" BackgroundCssClass="ModalPopupBG" />

<script type="text/javascript">
    //*****************************Model popup (Please wait)****************

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_beginRequest(BeginRequestHandler);
    prm.add_endRequest(EndRequestHandler);
    function BeginRequestHandler(sender, args) {
        var popup = $find('<%= modalPopup.ClientID %>');
        if (popup != null) {
            popup.show();
        }
    }

    function EndRequestHandler(sender, args) {
        var popup = $find('<%= modalPopup.ClientID %>');
        if (popup != null) {
            popup.hide();
        }
    }

</script>

Fill dropdownlist using Cashcading style sheet in asp.net

HTML-- without parent id

<asp:DropDownList ID="ddl_block" runat="server"  >  </asp:DropDownList>

<asp:CascadingDropDown ID="Cascadingddl_block" runat="server" Category="SubDistrict" TargetControlID="ddl_block" LoadingText="Loading SubDistrict..." PromptText="---Select---" ServiceMethod="BindSubDitstrictdropdown" ServicePath="~/DropdownWebService.asmx"> </asp:CascadingDropDown>

WebService-

 [WebMethod(EnableSession = true)]
    public CascadingDropDownNameValue[] BindStatedropdown(string knownCategoryValues, string category)
    {

        StringDictionary countrydetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        DataTable dtSelectMethod = new DataTable();
        dtSelectMethod = Select_All_Data("mstAState", "*", "Active='1'", "StateName", "ASC");
        int Index = 0;
        List<CascadingDropDownNameValue> statedetails = new List<CascadingDropDownNameValue>();
        foreach (DataRow dtstaterow in dtSelectMethod.Rows)
        {
            string StateCode = dtstaterow["StateCode"].ToString();
            string StateName = dtstaterow["StateName"].ToString();
            statedetails.Add(new CascadingDropDownNameValue(StateName, StateCode));
        }
        return statedetails.ToArray();
    }                    


HTML-- with parent id

<asp:DropDownList ID="ddl_block" runat="server" ></asp:DropDownList>

<asp:CascadingDropDown ID="Cascadingddl_block" runat="server" Category="SubDistrict" TargetControlID="ddl_block" LoadingText="Loading SubDistrict..." PromptText="---Select---" ServiceMethod="BindSubDitstrictdropdown" ServicePath="~/DropdownWebService.asmx"   ParentControlID="ddl_district"> </asp:CascadingDropDown>


[WebMethod(EnableSession = true)]
    public CascadingDropDownNameValue[] BindClusterdropdown(string knownCategoryValues, string category)
    {
        string StateCode;
        StringDictionary StateCodedetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        StateCode = Convert.ToString(StateCodedetails["State"]);
        
        string clusternames = null;
        string clus = Session["clustercheck"].ToString();
        if (clus != "")
        {
            if (clus.Length == 6)
            {
                clusternames = "'" + clus + "'";
            }
            else
            {
                // clusternames = clus.Substring(0, clus.Length - 1);
                clusternames = clus;
            }
        }

        DataTable dtSelectMethod = new DataTable();
        string condition = "";
        if (clusternames == null)
        { condition = "StateCode='" + StateCode + "'"; }
        else { condition = " StateCode='" + StateCode + "' and ClusterCode in (" + clusternames + ") "; }
         
       // string 
        dtSelectMethod = Select_All_Data("mstBCluster", "*", condition, "ClusterName", "ASC");

        List<CascadingDropDownNameValue> regiondetails = new List<CascadingDropDownNameValue>();
        foreach (DataRow dtregionrow in dtSelectMethod.Rows)
        {
            string ClusterCode = dtregionrow["ClusterCode"].ToString();
            string ClusterName = dtregionrow["ClusterName"].ToString();
            regiondetails.Add(new CascadingDropDownNameValue(ClusterName, ClusterCode));

        }
        return regiondetails.ToArray();
    }

             

Check string array [] value with Javascript

using script-
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">

function checkusername(value) {
 
    var codes = ['admin', 'adm', 'administrator', 'cluster', 'clu'];
     
        if ($.inArray(value.toLowerCase(), codes) > -1) {          
            alert("Invalid userid ! Please try another");
            return false;
        }

Password strong with Jquery (Atleast-1 numeric ,1 lower case,1 upper case, 1 special character)

function checkPwd(str) {
       // alert("enter");
        var msg = "";
        if (str.length < 8) {
            msg += 'Minimum 8 character'; //  for min length
        } else if (str.length > 20) {
            msg += 'Maximum 20 character'; //  for max length
        } else if (str.search(/\d/) == -1) {
            msg += '- Atleast one numeric'; // for numeric
        } else if (str.search(/[a-z]/) == -1) {
            msg += '- Atleast one Lower case letter'; // for character
        } else if (str.search(/[A-Z]/) == -1) {
            msg += '- Atleast one upper case letter'; // for character
        } else if (str.search(/[^a-zA-Z0-9\<\>\@\#\=\.\_\-\!\$]/) != -1) {
            msg += '- Password invalid ! Atleast one special character also(<,>,@,#,=,.,_,-,!,$])'; // for special character;
        }
        if (msg != "") {
         
            alert(msg);
            return false;
        }
        else { return true; }
    }

Strong Password with regular expression in asp.net c#

Atleast 1-lower,1-upper,1-numeric,1-special character

public static string checkpasswordCharacters(string password)
    {
        string msg = "";      
        Regex len = new Regex("^.{6,15}$");
        Regex num = new Regex("\\d");
       // Regex alphaL = new Regex("\\D");
        Regex alphaL = new Regex("[a-z]");
        Regex alphaU = new Regex("[A-Z]");
        Regex special = new Regex("[><#@=._!$-]");

        if (!len.IsMatch(password) )
        {
            msg += "-Minimum password length is 6 characters \\n";
        }
        if (!num.IsMatch(password))
        {
            msg += "-Please enter atleast one numeric value \\n";
        }
        if (!alphaL.IsMatch(password))
        {
            msg += "-Please enter atleast one lower case alphabet \\n";
        }
        if (!alphaU.IsMatch(password))
        {
            msg += "-Please enter atleast one upper case alphabet \\n";
        }
        if (!special.IsMatch(password))
        {
            msg += "-Please enter atleast one special Character \\n";
        }
       
        return msg;      

    }

Thursday, 5 September 2013

sql server format and convert format

– Microsoft SQL Server T-SQL date and datetime formats
– Date time formats – mssql datetime 
– MSSQL getdate returns current system date and time in standard internal format
SELECT convert(varchar, getdate(), 100) – mon dd yyyy hh:mmAM (or PM)
                                        – Oct  2 2008 11:01AM          
SELECT convert(varchar, getdate(), 101) – mm/dd/yyyy 10/02/2008                  
SELECT convert(varchar, getdate(), 102) – yyyy.mm.dd – 2008.10.02           
SELECT convert(varchar, getdate(), 103) – dd/mm/yyyy
SELECT convert(varchar, getdate(), 104) – dd.mm.yyyy
SELECT convert(varchar, getdate(), 105) – dd-mm-yyyy
SELECT convert(varchar, getdate(), 106) – dd mon yyyy
SELECT convert(varchar, getdate(), 107) – mon dd, yyyy
SELECT convert(varchar, getdate(), 108) – hh:mm:ss
SELECT convert(varchar, getdate(), 109) – mon dd yyyy hh:mm:ss:mmmAM (or PM)
                                        – Oct  2 2008 11:02:44:013AM   
SELECT convert(varchar, getdate(), 110) – mm-dd-yyyy
SELECT convert(varchar, getdate(), 111) – yyyy/mm/dd
SELECT convert(varchar, getdate(), 112) – yyyymmdd
SELECT convert(varchar, getdate(), 113) – dd mon yyyy hh:mm:ss:mmm
                                        – 02 Oct 2008 11:02:07:577     
SELECT convert(varchar, getdate(), 114) – hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 120) – yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 121) – yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 126) – yyyy-mm-ddThh:mm:ss.mmm
                                        – 2008-10-02T10:52:47.513
– SQL create different date styles with t-sql string functions
SELECT replace(convert(varchar, getdate(), 111), ‘/’, ‘ ‘) – yyyy mm dd
SELECT convert(varchar(7), getdate(), 126)                 – yyyy-mm
SELECT right(convert(varchar, getdate(), 106), 8)          – mon yyyy
————
– SQL Server date formatting function – convert datetime to string
————
– SQL datetime functions
– SQL Server date formats
– T-SQL convert dates
– Formatting dates sql server
CREATE FUNCTION dbo.fnFormatDate (@Datetime DATETIME, @FormatMask VARCHAR(32))
RETURNS VARCHAR(32)
AS
BEGIN
    DECLARE @StringDate VARCHAR(32)
    SET @StringDate = @FormatMask
    IF (CHARINDEX (‘YYYY’,@StringDate) > 0)
       SET @StringDate = REPLACE(@StringDate, ‘YYYY’,
                         DATENAME(YY, @Datetime))
    IF (CHARINDEX (‘YY’,@StringDate) > 0)
       SET @StringDate = REPLACE(@StringDate, ‘YY’,
                         RIGHT(DATENAME(YY, @Datetime),2))
    IF (CHARINDEX (‘Month’,@StringDate) > 0)
       SET @StringDate = REPLACE(@StringDate, ‘Month’,
                         DATENAME(MM, @Datetime))
    IF (CHARINDEX (‘MON’,@StringDate COLLATE SQL_Latin1_General_CP1_CS_AS)>0)
       SET @StringDate = REPLACE(@StringDate, ‘MON’,
                         LEFT(UPPER(DATENAME(MM, @Datetime)),3))
    IF (CHARINDEX (‘Mon’,@StringDate) > 0)
       SET @StringDate = REPLACE(@StringDate, ‘Mon’,
                                     LEFT(DATENAME(MM, @Datetime),3))
    IF (CHARINDEX (‘MM’,@StringDate) > 0)
       SET @StringDate = REPLACE(@StringDate, ‘MM’,
                  RIGHT(’0′+CONVERT(VARCHAR,DATEPART(MM, @Datetime)),2))
    IF (CHARINDEX (‘M’,@StringDate) > 0)
       SET @StringDate = REPLACE(@StringDate, ‘M’,
                         CONVERT(VARCHAR,DATEPART(MM, @Datetime)))
    IF (CHARINDEX (‘DD’,@StringDate) > 0)
       SET @StringDate = REPLACE(@StringDate, ‘DD’,
                         RIGHT(’0′+DATENAME(DD, @Datetime),2))
    IF (CHARINDEX (‘D’,@StringDate) > 0)
       SET @StringDate = REPLACE(@StringDate, ‘D’,
                                     DATENAME(DD, @Datetime))   
RETURN @StringDate
END
GO

– Microsoft SQL Server date format function test
– MSSQL formatting dates
SELECT dbo.fnFormatDate (getdate(), ‘MM/DD/YYYY’)           – 01/03/2012
SELECT dbo.fnFormatDate (getdate(), ‘DD/MM/YYYY’)           – 03/01/2012
SELECT dbo.fnFormatDate (getdate(), ‘M/DD/YYYY’)            – 1/03/2012
SELECT dbo.fnFormatDate (getdate(), ‘M/D/YYYY’)             – 1/3/2012
SELECT dbo.fnFormatDate (getdate(), ‘M/D/YY’)               – 1/3/12
SELECT dbo.fnFormatDate (getdate(), ‘MM/DD/YY’)             – 01/03/12
SELECT dbo.fnFormatDate (getdate(), ‘MON DD, YYYY’)         – JAN 03, 2012
SELECT dbo.fnFormatDate (getdate(), ‘Mon DD, YYYY’)         – Jan 03, 2012
SELECT dbo.fnFormatDate (getdate(), ‘Month DD, YYYY’)       – January 03, 2012
SELECT dbo.fnFormatDate (getdate(), ‘YYYY/MM/DD’)           – 2012/01/03
SELECT dbo.fnFormatDate (getdate(), ‘YYYYMMDD’)             – 20120103
SELECT dbo.fnFormatDate (getdate(), ‘YYYY-MM-DD’)           – 2012-01-03
– CURRENT_TIMESTAMP returns current system date and time in standard internal format
SELECT dbo.fnFormatDate (CURRENT_TIMESTAMP,‘YY.MM.DD’)      – 12.01.03
GO
————

/***** SELECTED SQL DATE/DATETIME FORMATS WITH NAMES *****/

– SQL format datetime
– Default format: Oct 23 2006 10:40AM
SELECT [Default]=CONVERT(varchar,GETDATE(),100)

– US-Style format: 10/23/2006
SELECT [US-Style]=CONVERT(char,GETDATE(),101)

– ANSI format: 2006.10.23
SELECT [ANSI]=CONVERT(char,CURRENT_TIMESTAMP,102)

– UK-Style format: 23/10/2006
SELECT [UK-Style]=CONVERT(char,GETDATE(),103)

– German format: 23.10.2006
SELECT [German]=CONVERT(varchar,GETDATE(),104)

– ISO format: 20061023
SELECT ISO=CONVERT(varchar,GETDATE(),112)

– ISO8601 format: 2008-10-23T19:20:16.003
SELECT [ISO8601]=CONVERT(varchar,GETDATE(),126)
————

– SQL Server datetime formats
– Century date format MM/DD/YYYY usage in a query
– Format dates SQL Server 2005
SELECT TOP (1)
      SalesOrderID,
      OrderDate = CONVERT(char(10), OrderDate, 101),
      OrderDateTime = OrderDate
FROM AdventureWorks.Sales.SalesOrderHeader
/* Result

SalesOrderID      OrderDate               OrderDateTime
43697             07/01/2001          2001-07-01 00:00:00.000
*/

– SQL update datetime column
– SQL datetime DATEADD
UPDATE Production.Product
SET ModifiedDate=DATEADD(dd,1, ModifiedDate)
WHERE ProductID = 1001

– MM/DD/YY date format
– Datetime format sql
SELECT TOP (1)
      SalesOrderID,
      OrderDate = CONVERT(varchar(8), OrderDate, 1),
      OrderDateTime = OrderDate
FROM AdventureWorks.Sales.SalesOrderHeader
ORDER BY SalesOrderID desc
/* Result

SalesOrderID      OrderDate         OrderDateTime
75123             07/31/04          2004-07-31 00:00:00.000
*/

– Combining different style formats for date & time
– Datetime formats
– Datetime formats sql
DECLARE @Date DATETIME
SET @Date = ’2015-12-22 03:51 PM’
SELECT CONVERT(CHAR(10),@Date,110) + SUBSTRING(CONVERT(varchar,@Date,0),12,8)
– Result: 12-22-2015  3:51PM

– Microsoft SQL Server cast datetime to string
SELECT stringDateTime=CAST (getdate() as varchar)
– Result: Dec 29 2012  3:47AM
————
– SQL Server date and time functions overview
————
– SQL Server CURRENT_TIMESTAMP function
– SQL Server datetime functions
– local NYC – EST – Eastern Standard Time zone
– SQL DATEADD function – SQL DATEDIFF function
SELECT CURRENT_TIMESTAMP                        – 2012-01-05 07:02:10.577
– SQL Server DATEADD function
SELECT DATEADD(month,2,’2012-12-09′)            – 2013-02-09 00:00:00.000
– SQL Server DATEDIFF function
SELECT DATEDIFF(day,’2012-12-09′,’2013-02-09′)  – 62
– SQL Server DATENAME function
SELECT DATENAME(month,   ’2012-12-09′)          – December
SELECT DATENAME(weekday, ’2012-12-09′)          – Sunday
– SQL Server DATEPART function
SELECT DATEPART(month, ’2012-12-09′)            – 12
– SQL Server DAY function
SELECT DAY(’2012-12-09′)                        – 9
– SQL Server GETDATE function
– local NYC – EST – Eastern Standard Time zone
SELECT GETDATE()                                – 2012-01-05 07:02:10.577
– SQL Server GETUTCDATE function
– London – Greenwich Mean Time
SELECT GETUTCDATE()                             – 2012-01-05 12:02:10.577
– SQL Server MONTH function
SELECT MONTH(’2012-12-09′)                      – 12
– SQL Server YEAR function
SELECT YEAR(’2012-12-09′)                       – 2012


————
– T-SQL Date and time function application
– CURRENT_TIMESTAMP and getdate() are the same in T-SQL
————
– SQL first day of the month
– SQL first date of the month
– SQL first day of current month – 2012-01-01 00:00:00.000
SELECT DATEADD(dd,0,DATEADD(mm, DATEDIFF(mm,0,CURRENT_TIMESTAMP),0))
– SQL last day of the month
– SQL last date of the month
– SQL last day of current month – 2012-01-31 00:00:00.000
SELECT DATEADD(dd,-1,DATEADD(mm, DATEDIFF(mm,0,CURRENT_TIMESTAMP)+1,0))
– SQL first day of last month
– SQL first day of previous month – 2011-12-01 00:00:00.000
SELECT DATEADD(mm,-1,DATEADD(mm, DATEDIFF(mm,0,CURRENT_TIMESTAMP),0))
– SQL last day of last month
– SQL last day of previous month – 2011-12-31 00:00:00.000
SELECT DATEADD(dd,-1,DATEADD(mm, DATEDIFF(mm,0,DATEADD(MM,-1,GETDATE()))+1,0))
– SQL first day of next month – 2012-02-01 00:00:00.000
SELECT DATEADD(mm,1,DATEADD(mm, DATEDIFF(mm,0,CURRENT_TIMESTAMP),0))
– SQL last day of next month – 2012-02-28 00:00:00.000
SELECT DATEADD(dd,-1,DATEADD(mm, DATEDIFF(mm,0,DATEADD(MM,1,GETDATE()))+1,0))
GO
– SQL first day of a month – 2012-10-01 00:00:00.000
DECLARE @Date datetime; SET @Date = ’2012-10-23′
SELECT DATEADD(dd,0,DATEADD(mm, DATEDIFF(mm,0,@Date),0))
GO
– SQL last day of a month – 2012-03-31 00:00:00.000
DECLARE @Date datetime; SET @Date = ’2012-03-15′
SELECT DATEADD(dd,-1,DATEADD(mm, DATEDIFF(mm,0,@Date)+1,0))
GO
– SQL first day of year 
– SQL first day of the year  -  2012-01-01 00:00:00.000
SELECT DATEADD(yy, DATEDIFF(yy,0,CURRENT_TIMESTAMP), 0)
– SQL last day of year  
– SQL last day of the year   – 2012-12-31 00:00:00.000
SELECT DATEADD(yy,1, DATEADD(dd, -1, DATEADD(yy,
                     DATEDIFF(yy,0,CURRENT_TIMESTAMP), 0)))
– SQL last day of last year
– SQL last day of previous year   – 2011-12-31 00:00:00.000
SELECT DATEADD(dd,-1,DATEADD(yy,DATEDIFF(yy,0,CURRENT_TIMESTAMP), 0))
GO
– SQL calculate age in years, months, days
– SQL table-valued function
– SQL user-defined function – UDF
– SQL Server age calculation – date difference
– Format dates SQL Server 2008
USE AdventureWorks2008;
GO
CREATE FUNCTION fnAge  (@BirthDate DATETIME)
RETURNS @Age TABLE(Years  INT,
                   Months INT,
                   Days   INT)
AS
  BEGIN
    DECLARE  @EndDate     DATETIME, @Anniversary DATETIME
    SET @EndDate = Getdate()
    SET @Anniversary = Dateadd(yy,Datediff(yy,@BirthDate,@EndDate),@BirthDate)
    
    INSERT @Age
    SELECT Datediff(yy,@BirthDate,@EndDate) - (CASE
                                                 WHEN @Anniversary > @EndDate THEN 1
                                                 ELSE 0
                                               END), 0, 0
     UPDATE @Age     SET    Months = Month(@EndDate - @Anniversary) - 1
    UPDATE @Age     SET    Days = Day(@EndDate - @Anniversary) - 1
    RETURN
  END
GO

– Test table-valued UDF
SELECT * FROM   fnAge(’1956-10-23′)
SELECT * FROM   dbo.fnAge(’1956-10-23′)
/* Results
Years       Months      Days
52          4           1
*/

———-
– SQL date range between
———-
– SQL between dates
USE AdventureWorks;
– SQL between
SELECT POs=COUNT(*) FROM Purchasing.PurchaseOrderHeader
WHERE OrderDate BETWEEN ’20040301′ AND ’20040315′
– Result: 108

– BETWEEN operator is equivalent to >=…AND….<=
SELECT POs=COUNT(*) FROM Purchasing.PurchaseOrderHeader
WHERE OrderDate
BETWEEN ’2004-03-01 00:00:00.000′ AND ’2004-03-15  00:00:00.000′
/*
Orders with OrderDates
’2004-03-15  00:00:01.000′  – 1 second after midnight (12:00AM)
’2004-03-15  00:01:00.000′  – 1 minute after midnight
’2004-03-15  01:00:00.000′  – 1 hour after midnight

are not included in the two queries above.
*/
– To include the entire day of 2004-03-15 use the following two solutions
SELECT POs=COUNT(*) FROM Purchasing.PurchaseOrderHeader
WHERE OrderDate >= ’20040301′ AND OrderDate < ’20040316′

– SQL between with DATE type (SQL Server 2008)
SELECT POs=COUNT(*) FROM Purchasing.PurchaseOrderHeader
WHERE CONVERT(DATE, OrderDate) BETWEEN ’20040301′ AND ’20040315′
———-
– Non-standard format conversion: 2011 December 14
– SQL datetime to string
SELECT [YYYY Month DD] =
CAST(YEAR(GETDATE()) AS VARCHAR(4))+ ‘ ‘+
DATENAME(MM, GETDATE()) + ‘ ‘ +
CAST(DAY(GETDATE()) AS VARCHAR(2))

– Converting datetime to YYYYMMDDHHMMSS format: 20121214172638
SELECT replace(convert(varchar, getdate(),111),‘/’,) +
replace(convert(varchar, getdate(),108),‘:’,)

– Datetime custom format conversion to YYYY_MM_DD
select CurrentDate=rtrim(year(getdate())) + ‘_’ +
right(’0′ + rtrim(month(getdate())),2) + ‘_’ +
right(’0′ + rtrim(day(getdate())),2)

– Converting seconds to HH:MM:SS format
declare @Seconds int
set @Seconds = 10000
select TimeSpan=right(’0′ +rtrim(@Seconds / 3600),2) + ‘:’ +
right(’0′ + rtrim((@Seconds % 3600) / 60),2) + ‘:’ +
right(’0′ + rtrim(@Seconds % 60),2)
– Result: 02:46:40

– Test result
select 2*3600 + 46*60 + 40
– Result: 10000
– Set the time portion of a datetime value to 00:00:00.000
– SQL strip time from date
– SQL strip time from datetime
SELECT CURRENT_TIMESTAMP ,DATEADD(dd, DATEDIFF(dd, 0, CURRENT_TIMESTAMP), 0)
– Results: 2014-01-23 05:35:52.793 2014-01-23 00:00:00.000
/*******

VALID DATE RANGES FOR DATE/DATETIME DATA TYPES

SMALLDATETIME date range:
January 1, 1900 through June 6, 2079

DATETIME date range:
January 1, 1753 through December 31, 9999

DATETIME2 date range (SQL Server 2008):
January 1,1 AD through December 31, 9999 AD

DATE date range (SQL Server 2008):
January 1, 1 AD through December 31, 9999 AD

*******/
– Selecting with CONVERT into different styles
– Note: Only Japan & ISO styles can be used in ORDER BY
SELECT TOP(1)
     Italy  = CONVERT(varchar, OrderDate, 105)
   , USA    = CONVERT(varchar, OrderDate, 110)
   , Japan  = CONVERT(varchar, OrderDate, 111)
   , ISO    = CONVERT(varchar, OrderDate, 112)
FROM AdventureWorks.Purchasing.PurchaseOrderHeader
ORDER BY PurchaseOrderID DESC
/* Results
Italy       USA         Japan       ISO
25-07-2004  07-25-2004  2004/07/25  20040725
*/
– SQL Server convert date to integer
DECLARE @Datetime datetime
SET @Datetime = ’2012-10-23 10:21:05.345′
SELECT DateAsInteger = CAST (CONVERT(varchar,@Datetime,112) as INT)
– Result: 20121023

– SQL Server convert integer to datetime
DECLARE @intDate int
SET @intDate = 20120315
SELECT IntegerToDatetime = CAST(CAST(@intDate as varchar) as datetime)
– Result: 2012-03-15 00:00:00.000
————
– SQL Server CONVERT script applying table INSERT/UPDATE
————
– SQL Server convert date
– Datetime column is converted into date only string column
USE tempdb;
GO
CREATE TABLE sqlConvertDateTime   (
            DatetimeCol datetime,
            DateCol char(8));
INSERT sqlConvertDateTime (DatetimeCol) SELECT GETDATE()

UPDATE sqlConvertDateTime
SET DateCol = CONVERT(char(10), DatetimeCol, 112)
SELECT * FROM sqlConvertDateTime

– SQL Server convert datetime
– The string date column is converted into datetime column
UPDATE sqlConvertDateTime
SET DatetimeCol = CONVERT(Datetime, DateCol, 112)
SELECT * FROM sqlConvertDateTime

– Adding a day to the converted datetime column with DATEADD
UPDATE sqlConvertDateTime
SET DatetimeCol = DATEADD(day, 1, CONVERT(Datetime, DateCol, 112))
SELECT * FROM sqlConvertDateTime

– Equivalent formulation
– SQL Server cast datetime
UPDATE sqlConvertDateTime
SET DatetimeCol = DATEADD(dd, 1, CAST(DateCol AS datetime))
SELECT * FROM sqlConvertDateTime
GO
DROP TABLE sqlConvertDateTime
GO
/* First results
DatetimeCol                   DateCol
2014-12-25 16:04:15.373       20141225 */

/* Second results:
DatetimeCol                   DateCol
2014-12-25 00:00:00.000       20141225  */

/* Third results:
DatetimeCol                   DateCol
2014-12-26 00:00:00.000       20141225  */
————
– SQL month sequence – SQL date sequence generation with table variable
– SQL Server cast string to datetime – SQL Server cast datetime to string
– SQL Server insert default values method
DECLARE @Sequence table (Sequence int identity(1,1))
DECLARE @i int; SET @i = 0
DECLARE @StartDate datetime;
SET @StartDate = CAST(CONVERT(varchar, year(getdate()))+
                 RIGHT(’0′+convert(varchar,month(getdate())),2) + ’01′ AS DATETIME)
WHILE ( @i < 120)
BEGIN
      INSERT @Sequence DEFAULT VALUES
      SET @i = @i + 1
END
SELECT MonthSequence = CAST(DATEADD(month, Sequence,@StartDate) AS varchar)
FROM @Sequence
GO
/* Partial results:
MonthSequence
Jan  1 2012 12:00AM
Feb  1 2012 12:00AM
Mar  1 2012 12:00AM
Apr  1 2012 12:00AM
*/
————

————
– SQL Server Server datetime internal storage
– SQL Server datetime formats
————
– SQL Server datetime to hex
SELECT Now=CURRENT_TIMESTAMP, HexNow=CAST(CURRENT_TIMESTAMP AS BINARY(8))
/* Results

Now                     HexNow
2009-01-02 17:35:59.297 0x00009B850122092D
*/
– SQL Server date part – left 4 bytes – Days since 1900-01-01
SELECT Now=DATEADD(DAY, CONVERT(INT, 0x00009B85), ’19000101′)
GO
– Result: 2009-01-02 00:00:00.000

– SQL time part – right 4 bytes – milliseconds since midnight
– 1000/300 is an adjustment factor
– SQL dateadd to Midnight
SELECT Now=DATEADD(MS, (1000.0/300)* CONVERT(BIGINT, 0x0122092D), ’2009-01-02′)
GO
– Result: 2009-01-02 17:35:59.290
————
————
– String date and datetime date&time columns usage
– SQL Server datetime formats in tables
————
USE tempdb;
SET NOCOUNT ON;
– SQL Server select into table create
SELECT TOP (5)
      FullName=convert(nvarchar(50),FirstName+‘ ‘+LastName),
      BirthDate = CONVERT(char(8), BirthDate,112),
      ModifiedDate = getdate()
INTO Employee
FROM AdventureWorks.HumanResources.Employee e
INNER JOIN AdventureWorks.Person.Contact c
ON c.ContactID = e.ContactID
ORDER BY EmployeeID
GO
– SQL Server alter table
ALTER TABLE Employee ALTER COLUMN FullName nvarchar(50) NOT NULL
GO
ALTER TABLE Employee
ADD CONSTRAINT [PK_Employee] PRIMARY KEY (FullName )
GO
/* Results

Table definition for the Employee table
Note: BirthDate is string date (only)

CREATE TABLE dbo.Employee(
      FullName nvarchar(50) NOT NULL PRIMARY KEY,
      BirthDate char(8) NULL,
      ModifiedDate datetime NOT NULL
      )
*/
SELECT * FROM Employee ORDER BY FullName
GO
/* Results
FullName                BirthDate   ModifiedDate
Guy Gilbert             19720515    2009-01-03 10:10:19.217
Kevin Brown             19770603    2009-01-03 10:10:19.217
Rob Walters             19650123    2009-01-03 10:10:19.217
Roberto Tamburello      19641213    2009-01-03 10:10:19.217
Thierry D’Hers          19490829    2009-01-03 10:10:19.217
*/

– SQL Server age
SELECT FullName, Age = DATEDIFF(YEAR, BirthDate, GETDATE()),
       RowMaintenanceDate = CAST (ModifiedDate AS varchar)
FROM Employee ORDER BY FullName
GO
/* Results
FullName                Age   RowMaintenanceDate
Guy Gilbert             37    Jan  3 2009 10:10AM
Kevin Brown             32    Jan  3 2009 10:10AM
Rob Walters             44    Jan  3 2009 10:10AM
Roberto Tamburello      45    Jan  3 2009 10:10AM
Thierry D’Hers          60    Jan  3 2009 10:10AM
*/

– SQL Server age of Rob Walters on specific dates
– SQL Server string to datetime implicit conversion with DATEADD
SELECT AGE50DATE = DATEADD(YY, 50, ’19650123′)
GO
– Result: 2015-01-23 00:00:00.000

– SQL Server datetime to string, Italian format for ModifiedDate
– SQL Server string to datetime implicit conversion with DATEDIFF
SELECT FullName,
         AgeDEC31 = DATEDIFF(YEAR, BirthDate, ’20141231′),
         AgeJAN01 = DATEDIFF(YEAR, BirthDate, ’20150101′),
         AgeJAN23 = DATEDIFF(YEAR, BirthDate, ’20150123′),
         AgeJAN24 = DATEDIFF(YEAR, BirthDate, ’20150124′),
       ModDate = CONVERT(varchar, ModifiedDate, 105)
FROM Employee
WHERE FullName = ‘Rob Walters’
ORDER BY FullName
GO
/* Results
Important Note: age increments on Jan 1 (not as commonly calculated)

FullName    AgeDEC31    AgeJAN01    AgeJAN23    AgeJAN24    ModDate
Rob Walters 49          50          50          50          03-01-2009
*/

————
– SQL combine integer date & time into datetime
————
– Datetime format sql
– SQL stuff
DECLARE @DateTimeAsINT TABLE ( ID int identity(1,1) primary key, 
   DateAsINT int, 
   TimeAsINT int 
) 
– NOTE: leading zeroes in time is for readability only!  
INSERT @DateTimeAsINT (DateAsINT, TimeAsINT) VALUES (20121023, 235959)  
INSERT @DateTimeAsINT (DateAsINT, TimeAsINT) VALUES (20121023, 010204)  
INSERT @DateTimeAsINT (DateAsINT, TimeAsINT) VALUES (20121023, 002350)
INSERT @DateTimeAsINT (DateAsINT, TimeAsINT) VALUES (20121023, 000244)  
INSERT @DateTimeAsINT (DateAsINT, TimeAsINT) VALUES (20121023, 000050)  
INSERT @DateTimeAsINT (DateAsINT, TimeAsINT) VALUES (20121023, 000006)  

SELECT DateAsINT, TimeAsINT,
  CONVERT(datetime, CONVERT(varchar(8), DateAsINT) + ‘ ‘+
  STUFF(STUFF ( RIGHT(REPLICATE(’0′, 6) + CONVERT(varchar(6), TimeAsINT), 6),
                  3, 0, ‘:’), 6, 0, ‘:’))  AS DateTimeValue
FROM   @DateTimeAsINT 
ORDER BY ID
GO
/* Results
DateAsINT   TimeAsINT   DateTimeValue
20121023    235959      2012-10-23 23:59:59.000
20121023    10204       2012-10-23 01:02:04.000
20121023    2350        2012-10-23 00:23:50.000
20121023    244         2012-10-23 00:02:44.000
20121023    50          2012-10-23 00:00:50.000
20121023    6           2012-10-23 00:00:06.000
*/
————

– SQL Server string to datetime, implicit conversion with assignment
UPDATE Employee SET ModifiedDate = ’20150123′
WHERE FullName = ‘Rob Walters’
GO
SELECT ModifiedDate FROM Employee WHERE FullName = ‘Rob Walters’
GO
– Result: 2015-01-23 00:00:00.000

/* SQL string date, assemble string date from datetime parts  */
– SQL Server cast string to datetime – sql convert string date
– SQL Server number to varchar conversion
– SQL Server leading zeroes for month and day
– SQL Server right string function
UPDATE Employee SET BirthDate =
      CONVERT(char(4),YEAR(CAST(’1965-01-23′ as DATETIME)))+
      RIGHT(’0′+CONVERT(varchar,MONTH(CAST(’1965-01-23′ as DATETIME))),2)+
      RIGHT(’0′+CONVERT(varchar,DAY(CAST(’1965-01-23′ as DATETIME))),2)
      WHERE FullName = ‘Rob Walters’
GO
SELECT BirthDate FROM Employee WHERE FullName = ‘Rob Walters’
GO
– Result: 19650123

– Perform cleanup action
DROP TABLE Employee
– SQL nocount
SET NOCOUNT OFF;
GO
————
————
– sql isdate function
————
USE tempdb;
– sql newid – random sort
SELECT top(3) SalesOrderID,
stringOrderDate = CAST (OrderDate AS varchar)
INTO DateValidation
FROM AdventureWorks.Sales.SalesOrderHeader
ORDER BY NEWID()
GO
SELECT * FROM DateValidation
/* Results
SalesOrderID      stringOrderDate
56720             Oct 26 2003 12:00AM
73737             Jun 25 2004 12:00AM
70573             May 14 2004 12:00AM
*/
– SQL update with top
UPDATE TOP(1) DateValidation
SET stringOrderDate = ‘Apb 29 2004 12:00AM’
GO
– SQL string to datetime fails without validation
SELECT SalesOrderID, OrderDate = CAST (stringOrderDate as datetime)
FROM DateValidation
GO
/* Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an
out-of-range value.
*/
– sql isdate – filter for valid dates
SELECT SalesOrderID, OrderDate = CAST (stringOrderDate as datetime)
FROM DateValidation
WHERE ISDATE(stringOrderDate) = 1
GO
/* Results
SalesOrderID      OrderDate
73737             2004-06-25 00:00:00.000
70573             2004-05-14 00:00:00.000
*/
– SQL drop table
DROP TABLE DateValidation
Go

————
– SELECT between two specified dates – assumption TIME part is 00:00:00.000
————
– SQL datetime between
– SQL select between two dates
SELECT EmployeeID, RateChangeDate
FROM AdventureWorks.HumanResources.EmployeePayHistory
WHERE RateChangeDate >= ’1997-11-01′ AND 
      RateChangeDate < DATEADD(dd,1,’1998-01-05′)
GO
/* Results
EmployeeID  RateChangeDate
3           1997-12-12 00:00:00.000
4           1998-01-05 00:00:00.000
*/

/* Equivalent to

– SQL datetime range
SELECT EmployeeID, RateChangeDate
FROM AdventureWorks.HumanResources.EmployeePayHistory
WHERE RateChangeDate >= ’1997-11-01 00:00:00′ AND 
      RateChangeDate <  ’1998-01-06 00:00:00′
GO
*/
————
– SQL datetime language setting
– SQL Nondeterministic function usage – result varies with language settings
SET LANGUAGE  ‘us_english’;  –– Jan 12 2015 12:00AM 
SELECT US = convert(VARCHAR,convert(DATETIME,’01/12/2015′));
SET LANGUAGE  ‘British’;     –– Dec  1 2015 12:00AM 
SELECT UK = convert(VARCHAR,convert(DATETIME,’01/12/2015′));
SET LANGUAGE  ‘German’;      –– Dez  1 2015 12:00AM 
SET LANGUAGE  ‘Deutsch’;     –– Dez  1 2015 12:00AM 
SELECT Germany = convert(VARCHAR,convert(DATETIME,’01/12/2015′));
SET LANGUAGE  ‘French’;      –– déc  1 2015 12:00AM 
SELECT France = convert(VARCHAR,convert(DATETIME,’01/12/2015′));
SET LANGUAGE  ‘Spanish’;     –– Dic  1 2015 12:00AM 
SELECT Spain = convert(VARCHAR,convert(DATETIME,’01/12/2015′));
SET LANGUAGE  ‘Hungarian’;   –– jan 12 2015 12:00AM 
SELECT Hungary = convert(VARCHAR,convert(DATETIME,’01/12/2015′));
SET LANGUAGE  ‘us_english’;
GO
————
————
– Function for Monday dates calculation
————
USE AdventureWorks2008;
GO
– SQL user-defined function
– SQL scalar function – UDF
CREATE FUNCTION fnMondayDate
               (@Year          INT,
                @Month         INT,
                @MondayOrdinal INT)
RETURNS DATETIME
AS
  BEGIN
    DECLARE  @FirstDayOfMonth CHAR(10),
             @SeedDate        CHAR(10)
    
    SET @FirstDayOfMonth = convert(VARCHAR,@Year) + ‘-’ + convert(VARCHAR,@Month) + ‘-01′
    SET @SeedDate = ’1900-01-01′
    
    RETURN DATEADD(DD,DATEDIFF(DD,@SeedDate,DATEADD(DD,(@MondayOrdinal * 7) - 1,
                  @FirstDayOfMonth)) / 7 * 7,  @SeedDate)
  END
GO

– Test Datetime UDF
– Third Monday in Feb, 2015
SELECT dbo.fnMondayDate(2016,2,3)
– 2015-02-16 00:00:00.000

– First Monday of current month
SELECT dbo.fnMondayDate(Year(getdate()),Month(getdate()),1)
– 2009-02-02 00:00:00.000  
————

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...