Wednesday, September 19, 2012

C# DateTime Format

Many DateTime formats are available. C# programs use the ToString method on the DateTime type. ToString receives many useful formats. These formats have confusing syntax forms. Correct formatting of dates and times is essential to many programs.

Format string

To start, we see an example of how you can use a specific formatting string with DateTime and ToString to obtain a special DateTime string. This is useful when interacting with other systems, or when you require a precise format.

Program that uses DateTime format [C#]  using System;  class Program
{static void Main()
{ DateTime time = DateTime.Now;// Use current time
string format = "MMM ddd d HH:mm yyyy"; // Use this format
Console.WriteLine(time.ToString(format)); // Write to console } }
Output Feb Fri 27 11:41 2009 Format string pattern MMM
three-letter month ddd display three-letter day of the WEEK d
display day of the MONTH HH display two-digit hours on 24-hour scale mm
display two-digit minutes yyyy display four-digit year

Note: The letters in the format string above specify the output you want to display. The final comment shows what the MMM, ddd, d, HH, mm, and yyyy will do.

Modify format


Continuing on, we see how you can modify the DateTime format string in the above example to get different output with ToString. We change some of the fields so the resulting value is shorter.

Program that uses different format [C#]
using System;
class Program {static void Main()
{ DateTime time = DateTime.Now; // Use current time string format = "M d h:mm yy";
// Use this format Console.WriteLine(time.ToString(format)); // Write to console } }
Output 2 27 11:48 09 Format string pattern M display one-digit month number
[changed] d display one-digit day of the MONTH [changed] h
display one-digit hour on 12-hour scale [changed] mm
display two-digit minutes yy display two-digit year
[changed]

Format string usages. You will also need to specify a format string when using DateTime.ParseExact and DateTime.ParseExact. This is because those methods require a custom pattern to parse.



Next you can use a single character with ToString or DateTime.ParseExact to specify a preset format available in the framework. These are standard formats and useful in many programs. They can eliminate typos in the custom format strings.

Program that tests formats [C#]
using System;
class Program {
static void Main()
{ DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("d"));
Console.WriteLine(now.ToString("D"));
Console.WriteLine(now.ToString("f"));
Console.WriteLine(now.ToString("F"));
Console.WriteLine(now.ToString("g"));
Console.WriteLine(now.ToString("G"));
Console.WriteLine(now.ToString("m"));
Console.WriteLine(now.ToString("M"));
Console.WriteLine(now.ToString("o"));
Console.WriteLine(now.ToString("O"));
Console.WriteLine(now.ToString("s"));
Console.WriteLine(now.ToString("t"));
Console.WriteLine(now.ToString("T"));
Console.WriteLine(now.ToString("u"));
Console.WriteLine(now.ToString("U"));
Console.WriteLine(now.ToString("y"));
Console.WriteLine(now.ToString("Y"));} }
Output
d 2/27/2009 D
Friday, February 27, 2009 f
Friday, February 27, 2009 12:11 PM F
Friday, February 27, 2009 12:12:22 PM g
2/27/2009 12:12 PM G 2/27/2009 12:12:22 PM m
February 27 M February 27 o 2009-02-27T12:12:22.1020000-08:00 O
2009-02-27T12:12:22.1020000-08:00 s 2009-02-27T12:12:22 t
12:12 PM T 12:12:22 PM u 2009-02-27 12:12:22Z U
Friday, February 27, 2009 8:12:22 PM y February, 2009 Y February, 2009

Date strings

Here we see the ToLongDateString, ToLongTimeString, ToShortDateString, and ToShortTimeString methods on DateTime. These methods are equivalent to the lowercase and uppercase D and T methods shown in the example above.

Program that uses ToString methods [C#]
using System; class Progra{ static void Main()
{ DateTime now = DateTime.Now; Console.WriteLine(now.ToLongDateString());
// Equivalent to D Console.WriteLine(now.ToLongTimeString());
// Equivalent to T Console.WriteLine(now.ToShortDateString());
// Equivalent to d Console.WriteLine(now.ToShortTimeString());
// Equivalent to t
Console.WriteLine(now.ToString()); } }
Output ToLongDateString
Friday, February 27, 2009 ToLongTimeString
12:16:59 PM ToShortDateString 2/27/2009 ToShortTimeString
12:16 PM ToString 2/27/2009 12:16:59 PM

SOURCE-http://www.dotnetperls.com/datetime-format

Tuesday, September 11, 2012

Add fck editor in php

include "fckeditor/fckeditor.php"; //Top of the page


where you need the edior

$FCKeditor = new FCKeditor('FCKeditor1');
$FCKeditor->BasePath = 'fckeditor/';
$FCKeditor->Value = $content;
$FCKeditor->Height = '400px';
$FCKeditor->Create();
?>

Monday, September 10, 2012

To find the second highest salary from a table 

select top 1 name,id from tbltest where name not in (select top 1 name from tbltest order by name desc) order by name desc

Same if you want to select the nth highest salary from a table 

select top 1 name,id from tbltest where name not in (select top n name from tbltest order by name desc) order by name desc

Thursday, August 30, 2012

Convert number into words in SQL server

Create a function named InWords 


CREATE FUNCTION  [dbo].[InWords] (@intNumberValue bigint)
RETURNS VARCHAR(2000)
AS
BEGIN
  DECLARE @strNumberString VARCHAR(9)
  DECLARE @strReturn VARCHAR(2000)
  DECLARE @intUnits SMALLINT
  -- Create table of number groups
  DECLARE @tblNumberGroups TABLE (Units SMALLINT, Hundreds SMALLINT, Tens SMALLINT)
   -- Handle errors and 'quick wins'
  IF @intNumberValue IS NULL RETURN NULL
  IF ISNUMERIC(@intNumberValue)=0 RETURN NULL
  IF @intNumberValue = 0 RETURN 'ZERO'
  IF @intNumberValue < 0
  BEGIN
    SET @strReturn='MINUS '
    SET @intNumberValue=ABS(@intNumberValue)
  END
  SET @intUnits =0
  -- Populate table of number groups
  WHILE (@intNumberValue % 1000) > 0 OR  (@intNumberValue/1000) >0
  BEGIN
    INSERT INTO @tblNumberGroups (Units, Hundreds, Tens) VALUES (@intUnits, (@intNumberValue % 1000)/100, (@intNumberValue % 1000) % 100 )
    SELECT @intNumberValue = CAST (@intNumberValue / 1000 AS INTEGER)
    SET @intUnits = @intUnits + 1
  END
  -- Remove last unit added
  SET @intUnits = @intUnits-1
  -- Concatenate text number by reading number groups in reverse order
  SELECT @strReturn = ISNULL(@strReturn,' ') +
  ISNULL(
  ISNULL((CASE Hundreds
    WHEN 1 THEN 'ONE HUNDRED '
    WHEN 2 THEN 'TWO HUNDRED '
    WHEN 3 THEN 'THREE HUNDRED '
    WHEN 4 THEN 'FOUR HUNDRED '
    WHEN 5 THEN 'FIVE HUNDRED '
    WHEN 6 THEN 'SIX HUNDRED '
    WHEN 7 THEN 'SEVEN HUNDRED '
    WHEN 8 THEN 'EIGHT HUNDRED '
    WHEN 9 THEN 'NINE HUNDRED '
  END),' ') +
  CASE WHEN (Hundreds >0 OR Units<@intUnits) AND Tens > 0   THEN ' AND ' ELSE ' ' END +
  ISNULL((CASE Tens / 10
    WHEN 2 THEN 'TWENTY '
    WHEN 3 THEN 'THIRTY '
    WHEN 4 THEN 'FORTY '
    WHEN 5 THEN 'FIFTY '
    WHEN 6 THEN 'SIXTY '
    WHEN 7 THEN 'SEVENTY '
    WHEN 8 THEN 'EIGHTY '
    WHEN 9 THEN 'NINETY '
  END),' ') +
  ISNULL((CASE Tens
    WHEN 10 THEN 'TEN '
    WHEN 11 THEN 'ELEVEN '
    WHEN 12 THEN 'TWELVE '
    WHEN 13 THEN 'THIRTEEN '
    WHEN 14 THEN 'FOURTEEN '
    WHEN 15 THEN 'FIFTEEN '
    WHEN 16 THEN 'SIXTEEN '
    WHEN 17 THEN 'SEVENTEEN '
    WHEN 18 THEN 'EIGHTEEN '
    WHEN 19 THEN 'NINETEEN '
  END),' ') +
  COALESCE(
    CASE WHEN Tens %10 =1 AND Tens / 10  <> 1 THEN 'ONE ' END,
    CASE WHEN Tens %10 =2 AND Tens / 10  <> 1 THEN 'TWO ' END,
  CASE WHEN Tens %10 =3 AND Tens / 10  <> 1 THEN 'THREE ' END,
  CASE WHEN Tens %10 =4 AND Tens / 10  <> 1 THEN 'FOUR ' END,
    CASE WHEN Tens %10 =5 AND Tens / 10  <> 1 THEN 'FIVE ' END,
    CASE WHEN Tens %10 =6 AND Tens / 10  <> 1 THEN 'SIX ' END,
    CASE WHEN Tens %10 =7 AND Tens / 10  <> 1 THEN 'SEVEN ' END,
    CASE WHEN Tens %10 =8 AND Tens / 10  <> 1 THEN 'EIGHT ' END,
    CASE WHEN Tens %10 =9 AND Tens / 10  <> 1 THEN 'NINE ' END,
  ' ')+
  COALESCE(
   CASE WHEN Units=1 AND (Hundreds>0 OR Tens>0) THEN 'THOUSAND ' END,
    CASE WHEN Units=2 AND (Hundreds>0 OR Tens>0) THEN 'MILLION ' END,
   CASE WHEN Units=3 AND (Hundreds>0 OR Tens>0) THEN 'BILLION ' END,
   CASE WHEN Units=4 AND (Hundreds>0 OR Tens>0) THEN 'TRILLION ' END,
  ' ')
   ,' ')
  FROM @tblNumberGroups
  ORDER BY units DESC
 
  -- Get rid of all the spaces
  WHILE CHARINDEX('  ', @strReturn)>0
   BEGIN
      SET @strReturn = REPLACE(@strReturn,'  ',' ')
   END
  SET @strReturn = LTRIM(RTRIM(@strReturn))
  RETURN @strReturn
END
GO




Now execute the query to call the function with the given parameters and get the result
select  dbo.InWords(-234973555459)
---------------------------------------------------------------
MINUS TWO HUNDRED AND THIRTY FOUR BILLION NINE HUNDRED AND SEVENTY THREE MILLION FIVE HUNDRED AND FIFTY FIVE THOUSAND FOUR HUNDRED AND FIFTY NINE


For more you can visit
http://stackoverflow.com/questions/4245330/sql-query-for-retrieving-numeric-value-and-printing-as-words


Wednesday, August 29, 2012

Convert Numeric value in words in PHP

function convert_number($number)
{
if (($number < 0) || ($number > 999999999))
{
throw new Exception("Number is out of range");
}

$Gn = floor($number / 1000000); /* Millions (giga) */
$number -= $Gn * 1000000;
$kn = floor($number / 1000); /* Thousands (kilo) */
$number -= $kn * 1000;
$Hn = floor($number / 100); /* Hundreds (hecto) */
$number -= $Hn * 100;
$Dn = floor($number / 10); /* Tens (deca) */
$n = $number % 10; /* Ones */

$res = "";

if ($Gn)
{
$res .= convert_number($Gn) . " Million";
}

if ($kn)
{
$res .= (empty($res) ? "" : " ") .
convert_number($kn) . " Thousand";
}

if ($Hn)
{
$res .= (empty($res) ? "" : " ") .
convert_number($Hn) . " Hundred";
}

$ones = array("", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen",
"Nineteen");
$tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",
"Seventy", "Eigthy", "Ninety");

if ($Dn || $n)
{
if (!empty($res))
{
$res .= " and ";
}

if ($Dn < 2)
{
$res .= $ones[$Dn * 10 + $n];
}
else
{
$res .= $tens[$Dn];

if ($n)
{
$res .= "-" . $ones[$n];
}
}
}

if (empty($res))
{
$res = "zero";
}

return $res;
}

Get Query string values from Javascript

function QuseryStringValue(QID) {

// Fetch the query string.
var QStringOriginal = window.location.search.substring(1);

// Change the case as querystring id values normally case insensitive.
QString = QStringOriginal.toLowerCase();
var qsValue = '';

// QueryString ID.
QID = QID.toLowerCase();
// Start & end point of the QueryString Value.
var qsStartPoint = QString.indexOf(QID);
if (qsStartPoint != -1) {

qsValue = QStringOriginal.substring(qsStartPoint + QID.length + 1);
// Search for '&' in the query string;
var qsEndPoint = qsValue.indexOf('&');
if (qsEndPoint != -1) {

// retrive the QueryString value & Return it.
qsValue = qsValue.substring(0, qsEndPoint);

}
else if (qsValue.indexOf('#') != -1) {

// Search for '#' in the query string;
qsEndPoint = qsValue.indexOf('&');
// retrive the QueryString value & Return it.
qsValue = qsValue.substring(0, qsEndPoint);

}
else {

qsValue = qsValue.substring(0);

}

}

return qsValue;
}

Saturday, August 18, 2012

If your are having a problem with "DbContext" class in Entity coding.
Please download the dll from http://packages.nuget.org/v1/package/download/entityframework/4.3.0
and remane  "EntityFramework.4.3.0.nupkg" to "EntityFramework.4.3.0.nupkg.rar".
Add the dll reference into your project and go on .

thanks