Are you looking for customized advices? Click here to contact the expert!
cghersi
cghersi
Reputation Top 1%
Cristiano Ghersi
Great passion for Software Engineering, mostly coding in Java and C#, I enjoy creating modular and reusable code for everybody!
196 Snippets  (2nd place)
Published
13 Channels
Created
60 Channels
Following
10 hours ago
Last Visit
May 5, 2012
Registered
6955 points  (2nd place)
Reputation
Top 1% Publisher
Junior Code Generator
Serious Code Generator
Senior Code Generator
Master Code Generator
Guru Code Generator
Junior Publisher
Serious Publisher
Senior Publisher
Master Publisher
Guru Publisher
Master Topic Creator
Guru Topic Creator
Junior Topic Hub
Serious Topic Hub
Senior Topic Hub
Master Topic Hub
Junior Trend Maker
Serious Trend Maker
Senior Trend Maker
Master Trend Maker
Senior Team Manager
Junior Influencer
Serious Influencer
Senior Influencer
Junior Commenter
Serious Commenter
Senior Commenter
Master Commenter
Junior Judge
Serious Judge
Senior Judge
Master Judge
Junior Popular Coder
Serious Popular Coder
Senior Popular Coder
Master Popular Coder
Guru Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Master Autobiographer
Junior Snip2Coder
Junior Geek Aggregator
Serious Geek Aggregator
Senior Geek Aggregator
Junior Famous Coder
Serious Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by cghersi

public by cghersi modified Apr 16, 2015  1  3  0

How to create an instance of a generic class in Java

This is the simplest way to create an instance of an object, given its class
T d = null;
try {
	d = (T)clazz.newInstance();
} catch (InstantiationException e) {
	log.error("Cannot create instance of class " + clazz, e);
	return;
} catch (IllegalAccessException e) {
	log.error("Cannot create instance of class " + clazz, e);
	return;
}

public by cghersi modified Mar 12, 2015  65  4  0

Git update from Batch

How to perform a git update from Batch script
cd my/path
call git reset <branch_name> --hard
call git checkout <branch_name>
call git pull                      

public by cghersi modified Feb 14, 2015  207  6  0

Nice way to show the current time of day

Current Time printed in a nice way
public static string NiceCurrentTime()
{  
  int hour = DateTime.Now.Hour;

  if (hour <= 5)
    return "this night oh yeah";
  else if (hour > 5 && hour <= 12)
    return "this morning";
  else if (hour > 12 && hour < 18)
    return "this afternoon";
  else 
    return "this evening";
}                                                                                    

public by cghersi modified Dec 16, 2014  495  5  1

How to view active SQL sessions onto a MS SQL Server

This script lets the sysadmin check the active SQL session on a server. Thanks to David Kranes, original post can be found here: http://www.sqlservercentral.com/scripts/Administration/88041/
SQL
SET NOCOUNT ON
GO

IF OBJECT_ID('tempdb..#curSQL') IS NOT NULL
 DROP TABLE #curSQL
GO

CREATE TABLE #curSQL
(spID SMALLINT,
blocked VARCHAR(3),
hostName VARCHAR(2000),
dbName VARCHAR(100),
cmd VARCHAR(100),
sqlText NTEXT,
phyIO BIGINT,
Status VARCHAR(100),
programName VARCHAR(1000),
loginTime DATETIME,
lastBatch DATETIME
)

DECLARE @spID smallint,
        @Blocked VARCHAR(3),
        @sqltext VARBINARY(128),
        @physIO BIGINT,
        @hostName VARCHAR(2000),
        @desSPID SMALLINT,
        @dbName VARCHAR(100),
        @Cmd VARCHAR(100),
        @status VARCHAR(100),
        @programName VARCHAR(1000),
        @loginTime DATETIME,
        @lastBatch DATETIME
        

-- Set desired SPID. If NULL, it will return all non-system SPID's.
--SET @desSPID = <Put desired SPID her to filter>


BEGIN TRY
    IF @desSPID IS NOT NULL
        DECLARE spID_cursor CURSOR
        FORWARD_ONLY READ_ONLY
        FOR SELECT spid, CASE blocked WHEN 0 THEN 'NO' ELSE 'YES' END blocked, hostname, db_name(dbid), cmd, physical_io, status, program_name, login_time, last_batch
            FROM sys.sysprocesses 
            WHERE spid = @desSPID
            GROUP BY spid, blocked, physical_io, hostname, db_name(dbid), cmd, physical_io, status, program_name, login_time, last_batch
            ORDER BY spid

    ELSE
        DECLARE spID_cursor CURSOR
        FORWARD_ONLY READ_ONLY
        FOR SELECT spid, CASE blocked WHEN 0 THEN 'NO' ELSE 'YES' END blocked, hostname, db_name(dbid), cmd, physical_io, status, program_name, login_time, last_batch
            FROM sys.sysprocesses 
            WHERE spid > 50
            GROUP BY spid, blocked, physical_io, hostname, db_name(dbid), cmd, physical_io, status, program_name, login_time, last_batch
            ORDER BY spid
        OPEN spID_cursor

        FETCH NEXT
        FROM spID_cursor
        INTO @spID, @Blocked, @hostName, @dbName, @cmd, @physIO, @status, @programName, @loginTime, @lastBatch

        WHILE @@FETCH_STATUS = 0
        BEGIN

         SELECT @sqltext = sql_handle
         FROM sys.sysprocesses
         WHERE spid = @spID

         INSERT INTO #curSQL
         SELECT @spID, @Blocked, @hostName, @dbName, @cmd, TEXT, @physIO, @status, @programName, @loginTime, @lastBatch
         FROM ::fn_get_sql(@sqltext)
         
         FETCH NEXT
         FROM spID_cursor
         INTO @spID, @Blocked, @hostName, @dbName, @cmd, @physIO, @status, @programName, @loginTime, @lastBatch
         
         END
         
         CLOSE spID_cursor
         DEALLOCATE spID_cursor
     
    SELECT * FROM #curSQL ORDER BY blocked asc, spid
END TRY

BEGIN CATCH
    CLOSE spID_cursor
    DEALLOCATE spID_cursor
    
    SELECT ERROR_NUMBER() ErrorNBR, ERROR_SEVERITY() Severity, ERROR_LINE() ErrorLine, ERROR_MESSAGE() Msg
    
    PRINT 'There was an error in the script.'
    
END CATCH    

            

public by cghersi modified Dec 10, 2014  195  5  3

How to query some values inside XML in SQL Server

This query relies on 'value' operator applied on XML fields. You can directly transform the resulting value into a particular type with the second parameter of this function.
WITH XMLNAMESPACES ('http://www.snip2code.com/namespaces/s2c' AS s2c)
SELECT  field1, field2
FROM    MyTable
WHERE   MyXMLField.value('/s2c:root/fieldName != ""', 'bit') = 1      

WITH XMLNAMESPACES ('http://www.snip2code.com/namespaces/s2c' AS s2c)
SELECT  field1, field2
FROM    MyTable
WHERE   MyXMLField.value('(/s2c:root/fieldName/text())[1]', 'varchar(100)') = 'some text'