Top 10 reads of May 2014

these are the top 10 reads of may 2014.

  • How to get current URL in JavaScript
  • How to check if a file exists using Python?
  • How to add a new column to an existing table in MSSQL Server?
  • How to check if a string contains another string in PHP
  • How to use java.String.format?
  • How to rename a file in c# or vb.net
  • How to change the cursor when hovering a Listitem
  • How to convert a string to int in Java?
  • How to change the time in a DateTime in c# or VB.NET
  • How to replace multiple spaces with a single space in c# or vb.net
  • thannks to all visitors.

    Please rate this snippet

    How to add a new column to an existing table in MSSQL Server?

    To add a new column to an existing table in MSSQL Server you can use the following snippet.

    ALTER TABLE {TABLENAME} 
    ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} 
    CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
    

    if you need to check if the column already exists you can use the following snippet.

    IF NOT EXISTS(SELECT * FROM sys.columns WHERE [name] = N'columnName' AND [object_id] = OBJECT_ID(N'tableName'))
    BEGIN
        -- Add your column
    END
    

    Sample MSSQL

    IF NOT EXISTS(SELECT * FROM sys.columns WHERE [name] = N'CreationDate' AND [object_id] = OBJECT_ID(N'Testtable'))
    BEGIN
        ALTER TABLE dbo.Testtable
    	ADD CreationDate DATETIME NOT NULL
    	CONSTRAINT DF__Testtable__CreationDate DEFAULT GETDATE()
    END
    

    see also ALTER TABLE (Transact-SQL), Check If Column Exists in SQL Server Table, Working with Default Constraints

    Please rate this snippet

    How to convert a string to int in Java?

    To convert a string to int in Java you can use the Integer.parseInt function.

    Sample Java

    int myInt = Integer.parseInt("1337");
    

    tip

    you can also use the parseInt method when using a StringBuffer, but you need to use it like

    int myInt = Integer.parseInt(myStringBuffer.toString());
    

    for more informations see the Javadoc parseInt

    Please rate this snippet

    How to change the time in a DateTime in c# or VB.NET

    To change the time in a DateTime you can either use Timespan or the add methods of the DateTime class. I will show you both ways and give you also a extension method.

     

    using the Timespan class

    Sample C#

    DateTime today = DateTime.Now;
    TimeSpan newTime = new TimeSpan(13, 37, 0);
    today = today.Date + newTime;
    

    Sample VB.NET

    Dim today As DateTime = DateTime.Now
    Dim newTime As New TimeSpan(13, 37, 0)
    today = today.Date + newTime
    

    using the Add methods of the DateTime class

    Sample C#

    DateTime today = DateTime.Now;
    today = today.Date.AddHours(13).AddMinutes(37).AddSeconds(0);
    

    Sample VB.NET

    Dim today As DateTime = DateTime.Now
    today = today.Date.AddHours(13).AddMinutes(37).AddSeconds(0)
    

    The Extension Method

    Sample C#

    public static DateTime ChangeTime(this DateTime dateTime, int hours, int minutes, int seconds, int milliseconds)
    {
          return new DateTime(dateTime.Year,dateTime.Month,dateTime.Day,hours,minutes,seconds,milliseconds,dateTime.Kind);
    }
    

    Sample VB.NET

    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function ChangeTime(dateTime As DateTime, hours As Integer, minutes As Integer, seconds As Integer, milliseconds As Integer) As DateTime
        Return New DateTime(dateTime.Year, dateTime.Month, dateTime.Day, hours, minutes, seconds, milliseconds, dateTime.Kind)
    End Function
    

    for more informations take a look at the MSDN: DateTime Structure, TimeSpan Structure, Extension Methods (C# Programming Guide)

    Please rate this snippet