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!
192 Snippets  (1st place)
Published
13 Channels
Created
54 Channels
Following
3 hours ago
Last Visit
May 5, 2012
Registered
6326 points  (1st 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
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
Serious Snip2Coder
Senior Snip2Coder
Junior Geek Aggregator
Serious Geek Aggregator
Senior Geek Aggregator
Junior Famous Coder

Recent Snippets See all snippets by cghersi

public by cghersi modified 5 hours ago  28  3  1

Retrieve the domain of an email in MS SQL Server

Suppose you have a table with an 'Email' field: here is the way to retrieve just only the domain of that email, and the name of the domain without the extension (.com, .net, etc.)
select 
  SUBSTRING(Email, 
    charindex('@', Email, 0) + 1, 
    (charindex('.', Email, charindex('@', Email, 0)) - charindex('@', Email, 0)) - 1
  ) as domainWithNoExt, 
  SUBSTRING(Email, charindex('@', Email, 0) + 1, 200) as domain
from MyTable

public by cghersi modified Nov 20, 2014  44  3  1

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

public by cghersi modified Nov 20, 2014  283  5  0

Query XML fields' content in MS SQL Server

This is the right syntax to perform XQuery queries for XML columns in a SQL Server table
SELECT MyField.query('
  declare namespace s2c="http://www.snip2code.com/namespaces/s2c";
	(/s2c:myroot/myelem/fieldIwant[1]/text())')
FROM  MyTable

public by cghersi modified Nov 20, 2014  65  4  0

How to create an asynchronous task in C#

This is a simple way to create a task that will run asynchronously with respect to the current thread
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
  log.Info("This will run async");
});

public by cghersi modified Nov 4, 2014  102  4  1

Calculate the occurrences of words in Rss feed

Creates a dictionary with keys=words and values=occurrences of that word in the given Rss (specified by the URL). The caller should specify the maximum number of items in the returning dictionary and, optionally, the minimum length of the words that will be added to the output.
public static Dictionary<string, int> ComputeWordsOccurrencesInRss(string url, int maxItems, int minLenOfAcceptedWords = 1)
{
  Dictionary<string, int> occurrences = new Dictionary<string, int>();

  IList<RssReader.RssItem> singleRssResult = RssReader.ReadRss(url, maxItems, true);
  string[] splitted = null;
  foreach (RssReader.RssItem rss in singleRssResult)
  {
    List<string> words = new List<string>();

    //split title in words:
    splitted = StringHelper.SplitIntoWords(rss.Title);
    if (splitted != null)
      words.AddRange(splitted);

    //split summary in words:
    splitted = StringHelper.SplitIntoWords(rss.Summary);
    if (splitted != null)
      words.AddRange(splitted);

    //compute occurrences of words:
    for (int w = 0; w < words.Count; w++)
    {
      string word = words[w].ToLower();
      if (word.Length < minLenOfAcceptedWords)
        continue;
      if (occurrences.ContainsKey(word))
        occurrences[word]++;
      else
        occurrences.Add(word, 1);
    }
  }

  return occurrences;
}