Joseph Michael Pesch
VP Programming

Get System Font List in C# Using Shell32.dll

by 31. December 2008 16:06

Creates a key/value dictionary of FontName/FontFile from the system fonts.  Also, contains a method to that accepts an output path which will be filled with image files (.png format) of sample text of each of the fonts. 

Original source of API code: http://blog.debreuil.com/archive/2007/05/31/5108.aspx

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
 
// Call the FontUtil.FontTool.BuildSampleFontImages() to generate sample image files of the fonts.
 
/*
 // Sample usage:
   FontUtil.FontList fnt = FontUtil.FontList.GetInstance();
   IEnumerator en = fnt.FontMap.GetEnumerator();
   while (en.MoveNext())
   {
     string FontName = ((System.Collections.Generic.KeyValuePair<string, string>)en.Current).Key;
     string FontFile = ((System.Collections.Generic.KeyValuePair<string, string>)en.Current).Value;
   }
 */
 
namespace FontUtil
{
   public static class FontTool
   {
     public static void BuildSampleFontImages(string OutputPath)
     {
      
       // The output path will be filled with image files (.png format) of sample text
       // of each of the fonts in the system that don't contain the words " bold" or " italic"
       // Also, checks after setting the since some of the fonts don't seem to register (see "Font Error" section).
       // NOTE: It will delete any existing image file with the same name before creating the new image.
 
       Font font = new Font("Times", 20);
       string FilePath = OutputPath;
 
       FontUtil.FontList fnt = FontUtil.FontList.GetInstance();
       IEnumerator en = fnt.FontMap.GetEnumerator();
       while (en.MoveNext())
       {
         string text = ((System.Collections.Generic.KeyValuePair<string, string>)en.Current).Key;
         try
         {
           // Skip bold and/or italic versions of the fonts.
           if (text.ToLower().Contains(" bold") || text.ToLower().Contains(" italic"))
             continue;
           font = new Font(text, 12);
           if (font.FontFamily.Name.ToLower() != text.ToLower())
           {
             System.Diagnostics.Debug.WriteLine("Font Error: " + text);
             continue;
           }
           FilePath = System.Web.HttpContext.Current.Server.MapPath("") + @"\images\fonts\" + text.Replace(" ", "_") + ".png";
           text += (char)10 + "1234567890";
           text += (char)10 + "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.";
           text += (char)10 + "The quick brown fox jumps over the lazy dog.";
           CreateTextImage(text, font, FilePath);
         }
         catch (Exception ex)
         {
           System.Diagnostics.Debug.WriteLine(ex.Message);
         }
       }
 
     }
     private static void CreateTextImage(string text, Font font, string FilePath)
     {
       Graphics g = Graphics.FromImage(new Bitmap(10, 10));
       SizeF sf = g.MeasureString(text, font);
       Bitmap bmp = new Bitmap(Convert.ToInt16(sf.Width), Convert.ToInt16(sf.Height));
 
       g = Graphics.FromImage(bmp);
       StringFormat strFormat = new StringFormat();
       strFormat.Alignment = StringAlignment.Center;
       strFormat.LineAlignment = StringAlignment.Center;
       // Color bgColor = Color.FromArgb(50, Color.Black); // 50% transparent black
       Color bgColor = Color.Transparent;
       g.FillRectangle(new SolidBrush(bgColor), 0, 0, Convert.ToInt16(sf.Width), Convert.ToInt16(sf.Height));
       g.DrawString(text, font, Brushes.Black,
        new RectangleF(0, 0, sf.Width, sf.Height), strFormat);
       g.Save();
 
       File.Delete(FilePath);
       bmp.Save(FilePath, ImageFormat.Png);
 
     }
 
   }
 
   public class FontList
   {
     public Dictionary<string, string> FontMap = new Dictionary<string, string>();
 
     private static FontList inst;
     private FontList()
     {
     }
 
     public static FontList GetInstance()
     {
       if (inst == null)
       {
         inst = new FontList();
         inst.GetAllFonts();
       }
       return inst;
     }
 
     private void GetAllFonts()
     {
       //string fp = Environment.GetFolderPath((Environment.SpecialFolder) 0x14);//CSIDL_FONTS = 0x0014
 
       StringBuilder sb = new StringBuilder();
       SHGetFolderPath(IntPtr.Zero, 0x0014, IntPtr.Zero, 0x0000, sb);//CSIDL_FONTS = 0x0014
       string fontDir = sb.ToString();
 
       if (!Directory.Exists(fontDir))
       {
         return;
       }
       string[] fonts = Directory.GetFiles(fontDir);
       for (int i = 0; i < fonts.Length; i++)
       {
         string name = GetFontName(fonts[i]);
         if (name != "")
         {
           FontMap.Add(name, fonts[i]);
         }
       }
     }
 
 
     private TT_OFFSET_TABLE ttResult;
     private TT_TABLE_DIRECTORY tbName;
     private TT_NAME_TABLE_HEADER ttNTResult;
     private TT_NAME_RECORD ttNMResult;
 
     private string GetFontName(string fontPath)
     {
       string result = "";
 
       FileStream fs = new FileStream(fontPath, FileMode.Open, FileAccess.Read);
       BinaryReader r = new BinaryReader(fs, Encoding.UTF8);
       byte[] buff = r.ReadBytes(Marshal.SizeOf(ttResult));
       buff = BigEndian(buff);
       IntPtr ptr = Marshal.AllocHGlobal(buff.Length);
       Marshal.Copy(buff, 0x0, ptr, buff.Length);
       ttResult = (TT_OFFSET_TABLE)Marshal.PtrToStructure(ptr, typeof(TT_OFFSET_TABLE));
       Marshal.FreeHGlobal(ptr);
 
       //Must be maj =1 minor = 0
       if (ttResult.uMajorVersion != 1 || ttResult.uMinorVersion != 0)
         return "";
 
       bool bFound = false;
       tbName = new TT_TABLE_DIRECTORY();
       for (int i = 0; i < ttResult.uNumOfTables; i++)
       {
         byte[] bNameTable = r.ReadBytes(Marshal.SizeOf(tbName));
         IntPtr ptrName = Marshal.AllocHGlobal(bNameTable.Length);
         Marshal.Copy(bNameTable, 0x0, ptrName, bNameTable.Length);
         tbName = (TT_TABLE_DIRECTORY)Marshal.PtrToStructure(ptrName, typeof(TT_TABLE_DIRECTORY));
         Marshal.FreeHGlobal(ptrName);
         string szName =
           tbName.szTag1.ToString() +
           tbName.szTag2.ToString() +
           tbName.szTag3.ToString() +
           tbName.szTag4.ToString();
         if (szName != null)
         {
           if (szName.ToString() == "name")
           {
             bFound = true;
             byte[] btLength = BitConverter.GetBytes(tbName.uLength);
             byte[] btOffset = BitConverter.GetBytes(tbName.uOffset);
             Array.Reverse(btLength);
             Array.Reverse(btOffset);
             tbName.uLength = BitConverter.ToUInt32(btLength, 0);
             tbName.uOffset = BitConverter.ToUInt32(btOffset, 0);
             break;
           }
         }
       }
       if (bFound)
       {
         fs.Position = tbName.uOffset;
         byte[] btNTHeader = r.ReadBytes(Marshal.SizeOf(ttNTResult));
         btNTHeader = BigEndian(btNTHeader);
         IntPtr ptrNTHeader = Marshal.AllocHGlobal(btNTHeader.Length);
         Marshal.Copy(btNTHeader, 0x0, ptrNTHeader, btNTHeader.Length);
         ttNTResult = (TT_NAME_TABLE_HEADER)Marshal.PtrToStructure(ptrNTHeader, typeof(TT_NAME_TABLE_HEADER));
         Marshal.FreeHGlobal(ptrNTHeader);
         bFound = false;
         for (int i = 0; i < ttNTResult.uNRCount; i++)
         {
           byte[] btNMRecord = r.ReadBytes(Marshal.SizeOf(ttNMResult));
           btNMRecord = BigEndian(btNMRecord);
           IntPtr ptrNMRecord = Marshal.AllocHGlobal(btNMRecord.Length);
           Marshal.Copy(btNMRecord, 0x0, ptrNMRecord, btNMRecord.Length);
           ttNMResult = (TT_NAME_RECORD)Marshal.PtrToStructure(ptrNMRecord, typeof(TT_NAME_RECORD));
           Marshal.FreeHGlobal(ptrNMRecord);
 
           // this is where the font name is recovered
           // to get the font family name (not incl 'bold' etc) use ttNMResult.uNameID == 1
           // see http://www.microsoft.com/OpenType/OTSpec/name.htm
           if (ttNMResult.uNameID == 4)
           {
             long fPos = fs.Position;
             fs.Position = tbName.uOffset + ttNMResult.uStringOffset + ttNTResult.uStorageOffset;
             char[] szResult = r.ReadChars(ttNMResult.uStringLength);
             if (szResult.Length != 0)
             {
               // some fonts are \0 A \0 r \0 i \0 a \0 l.... UTf8 encoding doesn't help
               if (szResult[0] == '\0')
               {
                 int count = 0;
                 char[] temp = new char[szResult.Length / 2];
                 for (int j = 1; j < szResult.Length; j += 2)
                 {
                   temp[count++] = szResult[j];
                 }
                 szResult = temp;
               }
               result = new String(szResult);
               break;
             }
           }
         }
       }
       return result;
     }
     private byte[] BigEndian(byte[] bLittle)
     {
       byte[] bBig = new byte[bLittle.Length];
       for (int y = 0; y < (bLittle.Length - 1); y += 2)
       {
         byte b1, b2;
         b1 = bLittle[y];
         b2 = bLittle[y + 1];
         bBig[y] = b2;
         bBig[y + 1] = b1;
       }
       return bBig;
     }
 
     [DllImport("shell32.dll")]
     static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken,
      uint dwFlags, [Out] StringBuilder pszPath);
 
     [StructLayout(LayoutKind.Sequential, Pack = 0x1)]
     struct TT_OFFSET_TABLE
     {
       public ushort uMajorVersion;
       public ushort uMinorVersion;
       public ushort uNumOfTables;
       public ushort uSearchRange;
       public ushort uEntrySelector;
       public ushort uRangeShift;
 
     }
     [StructLayout(LayoutKind.Sequential, Pack = 0x1)]
     struct TT_TABLE_DIRECTORY
     {
       public char szTag1;
       public char szTag2;
       public char szTag3;
       public char szTag4;
       public uint uCheckSum; //Check sum
       public uint uOffset; //Offset from beginning of file
       public uint uLength; //length of the table in bytes
     }
     [StructLayout(LayoutKind.Sequential, Pack = 0x1)]
     struct TT_NAME_TABLE_HEADER
     {
       public ushort uFSelector;
       public ushort uNRCount;
       public ushort uStorageOffset;
     }
     [StructLayout(LayoutKind.Sequential, Pack = 0x1)]
     struct TT_NAME_RECORD
     {
       public ushort uPlatformID;
       public ushort uEncodingID;
       public ushort uLanguageID;
       public ushort uNameID;
       public ushort uStringLength;
       public ushort uStringOffset;
     }
 
   }
 

}

Tags: ,

C# | Windows

HTML Div Win (divwin) Floating Windows

by 22. December 2008 15:59

HTML Div Win (divwin) Floating Windows with Auto Alignment and Expand/Collapse feature.  This is an HTML layout with javascript that allows auto alignment, collapsable, draggable/movable, inline virtual windows (divs with content and/or iFrames).  Here is the sample html code: divwin.zipx (258.48 kb)

See sample screen shot below.

Tags:

HTML

Microsoft Virtual PC (VPC) General Information

by 20. December 2008 14:31
Good link for "Differencing Disk" (i.e. creating additional machine from base image): http://arcanecode.wordpress.com/2008/03/04/differencing-disks-in-virtual-pc-2007/

Tags:

VPC

C# Gridview History Highlight Example

by 16. December 2008 14:52

This sample loads a GridView and performs the two functions shown below.  The first function is simply to make the column labels more user friendly by adding spaces.  The second function is relevant to the historical aspect of the data being viewed.  It was intended as a method to load a grid of audit trail type changes to a single record.  The comparison will then highlight changed values so they stand out visually against the unchanged values.

  1. Performs a string replacement on the column names, putting spaces at every captial letter. For example: "ThisColumnName" would be converted to "This Column Name".
  2. Performs a column-by-column record comparison of the current row in the GridView (using a RegEx function) against the next row in the DataTable, and if the values don't match the GridView column is highlighted yellow. Note: It is best presented with newest records on top.

GridViewHistoryHighlight.zip (5.57 kb)

Tags:

ASP.Net | C#

Workflow Foundation Testing

by 12. December 2008 20:31

This version was having troubles: WorkflowTesting.zip (144.31 kb)

This version works for both WFWeb and WFPersistenceSample, also added WFMonitor (from SDK WCF_WF_CardSpace_Samples): WorkflowTesting.zip (345.36 kb)

Tags:

Workflow Foundation

C# Regular Expression (RegEx) Samples

by 11. December 2008 18:57
using System;
using System.Text.RegularExpressions;

namespace RegExSamples
{
  class Program
  {
    static void Main(string[] args)
    {
      string result;
      // Replace each new word with uppercase.
      result = Regex.Replace("four score and seven years ago", @"\w+", new MatchEvaluator(CapText));
      Console.WriteLine(result);
      // Add a space before every capital letter.
      result = Regex.Replace("ThisIsAnotherTest", @"[A-Z]", new MatchEvaluator(PrefixSpace));
      Console.WriteLine(result);
      Console.Read();
    }
    static string CapText(Match m)
    {
      // Get the matched string.
      string x = m.ToString();
      // If the first char is lower case...
      if (char.IsLower(x[0]))
      {
        // Capitalize it.
        return char.ToUpper(x[0]) + x.Substring(1, x.Length - 1);
      }
      return x;
    }
    static string PrefixSpace(Match m)
    {
      string x = m.ToString();
      return " " + x;
    }
  }
}

Tags:

C#

SQL Server Raise Error (raiserror)

by 10. December 2008 14:23

raiserror
(
  N'There is already an active Modification record for Servicer Loan Number: %s, you must close that modification before starting a new one.',
  16,-- Severity.
  1,-- State.
  @ServicerLoanNumber -- substitution argument.
)

NOTE: Use %s for string substitution, %n for number substitution, etc.

Tags:

SQL Server

C# Create a String of Repeating Charcters

by 9. December 2008 17:56

newstring('x', 50);

 

Tags:

C#

C# Get Virtual Root Path of File System Web Site

by 8. December 2008 20:41

RootPath = newString('/', Request.Path.Replace(Request.ApplicationPath, "").Split('/').Length - 2).Replace("/", "../");

 

Tags:

ASP.Net | C#

Unread Mail Folder in Outlook

by 4. December 2008 13:46

To add the 'Unread Mail' item to the 'Favorite Folders' in Outlook:
1. Click on the 'Folder List' icon at the bottom on the left-hand side of Outlook.


2. Expand the 'Search Folders' menu.
3. Right-click the 'Unread Mail' item.


Tags:

Outlook

SQL Server Date Format via Convert Function

by 3. December 2008 16:47

To get month abbreviation + year (e.g. 'Jan 2009'): selectreplace(convert(varchar(11),getdate()+5, 106),right('0'+convert(varchar(2),datepart(day,getdate()+5)), 2)+' ','')

To get date range of today:

select * from sometable where somedate between convert(varchar(10), getdate(), 101) and convert(datetime, (convert(varchar(10), getdate(), 101) + ' 23:59:59.998'))

 

Standard Date Formats
Date Format Standard SQL Statement Sample Output
Mon DD YYYY 1
HH:MIAM (or PM)
Default SELECT CONVERT(VARCHAR(20), GETDATE(), 100) Jan 1 2005 1:29PM 1
MM/DD/YY USA SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] 11/23/98
MM/DD/YYYY USA SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY] 11/23/1998
YY.MM.DD ANSI SELECT CONVERT(VARCHAR(8), GETDATE(), 2) AS [YY.MM.DD] 72.01.01
YYYY.MM.DD ANSI SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD] 1972.01.01
DD/MM/YY British/French SELECT CONVERT(VARCHAR(8), GETDATE(), 3) AS [DD/MM/YY] 19/02/72
DD/MM/YYYY British/French SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY] 19/02/1972
DD.MM.YY German SELECT CONVERT(VARCHAR(8), GETDATE(), 4) AS [DD.MM.YY] 25.12.05
DD.MM.YYYY German SELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS [DD.MM.YYYY] 25.12.2005
DD-MM-YY Italian SELECT CONVERT(VARCHAR(8), GETDATE(), 5) AS [DD-MM-YY] 24-01-98
DD-MM-YYYY Italian SELECT CONVERT(VARCHAR(10), GETDATE(), 105) AS [DD-MM-YYYY] 24-01-1998
DD Mon YY 1 - SELECT CONVERT(VARCHAR(9), GETDATE(), 6) AS [DD MON YY] 04 Jul 06 1
DD Mon YYYY 1 - SELECT CONVERT(VARCHAR(11), GETDATE(), 106) AS [DD MON YYYY] 04 Jul 2006 1
Mon DD, YY 1 - SELECT CONVERT(VARCHAR(10), GETDATE(), 7) AS [Mon DD, YY] Jan 24, 98 1
Mon DD, YYYY 1 - SELECT CONVERT(VARCHAR(12), GETDATE(), 107) AS [Mon DD, YYYY] Jan 24, 1998 1
HH:MM:SS - SELECT CONVERT(VARCHAR(8), GETDATE(), 108) 03:24:53
Mon DD YYYY HH:MI:SS:MMMAM (or PM) 1 Default +
milliseconds
SELECT CONVERT(VARCHAR(26), GETDATE(), 109) Apr 28 2006 12:32:29:253PM 1
MM-DD-YY USA SELECT CONVERT(VARCHAR(8), GETDATE(), 10) AS [MM-DD-YY] 01-01-06
MM-DD-YYYY USA SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS [MM-DD-YYYY] 01-01-2006
YY/MM/DD - SELECT CONVERT(VARCHAR(8), GETDATE(), 11) AS [YY/MM/DD] 98/11/23
YYYY/MM/DD - SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD] 1998/11/23
YYMMDD ISO SELECT CONVERT(VARCHAR(6), GETDATE(), 12) AS [YYMMDD] 980124
YYYYMMDD ISO SELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS [YYYYMMDD] 19980124
DD Mon YYYY HH:MM:SS:MMM(24h) 1 Europe default + milliseconds SELECT CONVERT(VARCHAR(24), GETDATE(), 113) 28 Apr 2006 00:34:55:190 1
HH:MI:SS:MMM(24H) - SELECT CONVERT(VARCHAR(12), GETDATE(), 114) AS [HH:MI:SS:MMM(24H)] 11:34:23:013
YYYY-MM-DD HH:MI:SS(24h) ODBC Canonical SELECT CONVERT(VARCHAR(19), GETDATE(), 120) 1972-01-01 13:42:24
YYYY-MM-DD HH:MI:SS.MMM(24h) ODBC Canonical
(with milliseconds)
SELECT CONVERT(VARCHAR(23), GETDATE(), 121) 1972-02-19 06:35:24.489
YYYY-MM-DDTHH:MM:SS:MMM ISO8601 SELECT CONVERT(VARCHAR(23), GETDATE(), 126) 1998-11-23T11:25:43:250
DD Mon YYYY HH:MI:SS:MMMAM 1 Kuwaiti SELECT CONVERT(VARCHAR(26), GETDATE(), 130) 28 Apr 2006 12:39:32:429AM 1
DD/MM/YYYY HH:MI:SS:MMMAM Kuwaiti SELECT CONVERT(VARCHAR(25), GETDATE(), 131) 28/04/2006 12:39:32:429AM

 

Extended Date Formats
Date Format SQL Statement Sample Output
YY-MM-DD
SELECT SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 120), 3, 8) AS [YY-MM-DD]
SELECT REPLACE(CONVERT(VARCHAR(8), GETDATE(), 11), '/', '-') AS [YY-MM-DD]
99-01-24
YYYY-MM-DD
SELECT CONVERT(VARCHAR(10), GETDATE(), 120) AS [YYYY-MM-DD]
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 111), '/', '-') AS [YYYY-MM-DD]
1999-01-24
MM/YY SELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 3), 5) AS [MM/YY]
SELECT SUBSTRING(CONVERT(VARCHAR(8), GETDATE(), 3), 4, 5) AS [MM/YY]
08/99
MM/YYYY SELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 103), 7) AS [MM/YYYY] 12/2005
YY/MM SELECT CONVERT(VARCHAR(5), GETDATE(), 11) AS [YY/MM] 99/08
YYYY/MM SELECT CONVERT(VARCHAR(7), GETDATE(), 111) AS [YYYY/MM] 2005/12
Month DD, YYYY 1 SELECT DATENAME(MM, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY] July 04, 2006 1
Mon YYYY 1 SELECT SUBSTRING(CONVERT(VARCHAR(11), GETDATE(), 113), 4, 8) AS [Mon YYYY] Apr 2006 1
Month YYYY 1 SELECT DATENAME(MM, GETDATE()) + ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month YYYY] February 2006 1
DD Month 1 SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) AS [DD Month] 11 September 1
Month DD 1 SELECT DATENAME(MM, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2)) AS [Month DD] September 11 1
DD Month YY 1 SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) + ' ' + RIGHT(CAST(YEAR(GETDATE()) AS VARCHAR(4)), 2) AS [DD Month YY] 19 February 72 1
DD Month YYYY 1 SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) + ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [DD Month YYYY] 11 September 2002 1
MM-YY SELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 5), 5) AS [MM-YY]
SELECT SUBSTRING(CONVERT(VARCHAR(8), GETDATE(), 5), 4, 5) AS [MM-YY]
12/92
MM-YYYY SELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 105), 7) AS [MM-YYYY] 05-2006
YY-MM SELECT RIGHT(CONVERT(VARCHAR(7), GETDATE(), 120), 5) AS [YY-MM]
SELECT SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 120), 3, 5) AS [YY-MM]
92/12
YYYY-MM SELECT CONVERT(VARCHAR(7), GETDATE(), 120) AS [YYYY-MM] 2006-05
MMDDYY SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 1), '/', '') AS [MMDDYY] 122506
MMDDYYYY SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') AS [MMDDYYYY] 12252006
DDMMYY SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 3), '/', '') AS [DDMMYY] 240702
DDMMYYYY SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 103), '/', '') AS [DDMMYYYY] 24072002
Mon-YY 1 SELECT REPLACE(RIGHT(CONVERT(VARCHAR(9), GETDATE(), 6), 6), ' ', '-') AS [Mon-YY] Sep-02 1
Mon-YYYY 1 SELECT REPLACE(RIGHT(CONVERT(VARCHAR(11), GETDATE(), 106), 8), ' ', '-') AS [Mon-YYYY] Sep-2002 1
DD-Mon-YY 1 SELECT REPLACE(CONVERT(VARCHAR(9), GETDATE(), 6), ' ', '-') AS [DD-Mon-YY] 25-Dec-05 1
DD-Mon-YYYY 1 SELECT REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '-') AS [DD-Mon-YYYY] 25-Dec-2005 1

1 To make the month name in upper case, simply use the UPPER string function.

Tags:

SQL Server