Thursday, May 3, 2012
Exporting to Word, Excel ,pdf and CSV
Monday, April 9, 2012
Currenct Convertor( Number to Word) Asp.Net (C#)
public string NumberToWord(Int32 number)
{
if (number == 0) return "Zero";
//if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight";
int[] num = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (number < 0)
{
sb.Append("Minus ");
number = -number;
}
string[] words0 = { "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine " };
string[] words = { "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " };
string[] words2 = { "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " };
string[] words3 = { "Thousand ", "Lakh ", "Crore " };
// { "", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion" };
num[0] = number % 1000; // units
num[1] = number / 1000;
num[2] = number / 100000;
num[1] = num[1] - 100 * num[2];// thousands
num[3] = number / 10000000; // crores
num[2] = num[2] - 100 * num[3]; // lakhs
for (int i = 3; i > 0; i--)
{
if (num[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (num[i] == 0) continue;
u = num[i] % 10; // ones
t = num[i] / 10;
h = num[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sb.Append(words0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (h > 0 || i == 0) sb.Append("and ");
if (t == 0)
sb.Append(words0[u]);
else if (t == 1)
sb.Append(words[u]);
else
sb.Append(words2[t - 2] + words0[u]);
}
if (i != 0) sb.Append(words3[i - 1]);
}
return sb.ToString().TrimEnd();
}
2) Dollar and Euro
public string NumberToWords(string rawnumber)
{
int inputNum = 0;
int dig1, dig2, dig3, level = 0, lasttwo, threeDigits;
string dollars, cents;
try
{
string[] Splits = new string[2];
Splits = rawnumber.Split('.'); //notice that it is ' and not "
inputNum = Convert.ToInt32(Splits[0]);
dollars = "";
cents = Splits[1];
if (cents.Length == 1)
{
cents += "0"; // 12.5 is twelve and 50/100, not twelve and 5/100
}
}
catch
{
cents = "00";
inputNum = Convert.ToInt32(rawnumber);
dollars = "";
}
string x = "";
//they had zero for ones and tens but that gave ninety zero for 90
string[] ones = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
string[] tens = { "", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
string[] thou = { "", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion" };
bool isNegative = false;
if (inputNum < 0)
{
isNegative = true;
inputNum *= -1;
}
if (inputNum == 0)
{
//return "zero and " + cents + "/100";
return "zero";
}
string s = inputNum.ToString();
while (s.Length > 0)
{
//Get the three rightmost characters
x = (s.Length < 3) ? s : s.Substring(s.Length - 3, 3);
// Separate the three digits
threeDigits = int.Parse(x);
lasttwo = threeDigits % 100;
dig1 = threeDigits / 100;
dig2 = lasttwo / 10;
dig3 = (threeDigits % 10);
// append a "thousand" where appropriate
if (level > 0 && dig1 + dig2 + dig3 > 0)
{
dollars = thou[level] + " " + dollars;
dollars = dollars.Trim();
}
// check that the last two digits is not a zero
if (lasttwo > 0)
{
if (lasttwo < 20)
{
// if less than 20, use "ones" only
dollars = ones[lasttwo] + " " + dollars;
}
else
{
// otherwise, use both "tens" and "ones" array
dollars = tens[dig2] + " " + ones[dig3] + " " + dollars;
}
if (s.Length < 3)
{
if (isNegative) { dollars = "negative " + dollars; }
//return dollars + " and " + cents + "/100";
return dollars ;
}
}
// if a hundreds part is there, translate it
if (dig1 > 0)
{
dollars = ones[dig1] + " hundred " + dollars;
s = (s.Length - 3) > 0 ? s.Substring(0, s.Length - 3) : "";
level++;
}
else
{
if (s.Length > 3)
{
s = s.Substring(0, s.Length - 3);
level++;
}
}
}
if (isNegative) { dollars = "negative " + dollars; }
//return dollars + " and " + cents + "/100";
return dollars ;
}
Using Stored Procedure(Sql Server)
Create Procedure NumberToWord
@Number AS BIGINT
AS
BEGIN
DECLARE @Digits INT
SET @Digits = @Number
IF (LEN(@Digits) > 4)
BEGIN
RAISERROR ('PLEASE ENTER WITH IN 4 DIGIT NUMBERS',12,-1)
END
ELSE IF (LEN(@Digits) <= 4)
BEGIN
DECLARE @NumWords TABLE(Number INT, Ones VARCHAR(15), Tens VARCHAR(15), Hundreds VARCHAR(15), Thousands VARCHAR(15))
INSERT INTO @NumWords
SELECT 0,'','','','' UNION ALL
SELECT 1,'ONE','TEN','ONE HUNDRED','ONE THOUSAND' UNION ALL
SELECT 2,'TWO','TWENTY','TWO HUNDRED','TWO THOUSAND' UNION ALL
SELECT 3,'THREE','THIRTY','THREE HUNDRED','THREE THOUSAND' UNION ALL
SELECT 4,'FOUR','FORTY','FOUR HUNDRED','FOUR THOUSAND' UNION ALL
SELECT 5,'FIVE','FIFTY','FIVE HUNDRED','FIVE THOUSAND' UNION ALL
SELECT 6,'SIX','SIXTY','SIX HUNDRED','SIX THOUSAND' UNION ALL
SELECT 7,'SEVEN','SEVENTY','SEVEN HUNDRED','SEVEN THOUSAND' UNION ALL
SELECT 8,'EIGHT','EIGHTY','EIGHT HUNDRED','EIGHT THOUSAND' UNION ALL
SELECT 9,'NINE','NINETY','NINE HUNDRED','NINETHOUSAND' UNION ALL
SELECT 10,'NINETEEN','','','' UNION ALL
SELECT 11,'ELEVEN','','','' UNION ALL
SELECT 12,'TWELVE','','','' UNION ALL
SELECT 13,'THIRTEEN','','','' UNION ALL
SELECT 14,'FOURTEEN','','','' UNION ALL
SELECT 15,'FIFTEEN','','','' UNION ALL
SELECT 16,'SIXTEEN','','','' UNION ALL
SELECT 17,'SEVENTEEN','','','' UNION ALL
SELECT 18,'EIGHTEEN','','','' UNION ALL
SELECT 19,'NINETEEN','','',''
SELECT RTRIM(MAX(CASE WHEN NUMBER = SUBSTRING(REVERSE(@Digits),4,1) THEN THOUSANDS ELSE '' END)) --FROM @NumWords
+SPACE(2)+ LTRIM(MAX(CASE WHEN NUMBER = SUBSTRING(REVERSE(@Digits),3,1) THEN HUNDREDS ELSE '' END) )
+SPACE(2)+ MAX(CASE WHEN RIGHT(@Digits,2) NOT BETWEEN 11 AND 19 AND NUMBER = SUBSTRING(REVERSE(@Digits),2,1) THEN TENS ELSE '' END)
+SPACE(2)+ MAX(CASE WHEN RIGHT(@Digits,2) NOT BETWEEN 11 AND 19 AND NUMBER = RIGHT(@Digits,1) THEN ONES
WHEN RIGHT(@Digits,2) BETWEEN 11 AND 19 AND NUMBER = RIGHT(@Digits,2) THEN ONES ELSE '' END)
As "Number to Words"
FROM @NUMWORDS
END
END
--EXEC NumberToWord 2010
Friday, November 25, 2011
Displaying multiple records in one row
I have a table with values as follows:
SQL> SELECT deptno, ename FROM emp ORDER BY deptno, ename;
DEPTNO ENAME
------ ----------
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
14 rows selected.
but I need them in the following less convenient format:
DEPTNO ENAME
------ -----------------------------------------
10 CLARK, KING, MILLER
20 ADAMS, FORD, JONES, SCOTT, SMITH
30 ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD
The following example illustrates the technique using the SCOTT demo table "emp":2
SELECT deptno
, LTRIM(SYS_CONNECT_BY_PATH(ename,','))
FROM ( SELECT deptno, ename
, ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) -1 AS seq
FROM emp )
WHERE connect_by_isleaf = 1
CONNECT BY seq = PRIOR seq +1 AND deptno = PRIOR deptno
START WITH seq = 1;
DEPTNO CONCATENATED
---------- --------------------------------------------------
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
3 rows selected.
Another approach involves harnessing the dark power of XML:3
SELECT deptno
, RTRIM
( xmlagg (xmlelement (c, ename || ',') order by ename).extract ('//text()')
, ',' ) AS concatenated
FROM emp
GROUP BY deptno;
DEPTNO CONCATENATED
---------- ---------------------------------------------------------------
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
3 rows selected.