numbervar RmVal:=0; numbervar Amt:=0; numbervar pAmt:=0; stringvar InWords :="Rupees "; Amt := ({ORCT.DocTotal}); Amt := round(Amt,0); if Amt > 10000000 then RmVal := truncate(Amt/10000000); if Amt = 10000000 then RmVal := 1; if RmVal = 1 then InWords := InWords + " " + towords(RmVal,0) + " crore" else if RmVal > 1 then InWords := InWords + " " + towords(RmVal,0) + " crores"; Amt := Amt - Rmval * 10000000; if Amt > 100000 then RmVal := truncate(Amt/100000); if Amt = 100000 then RmVal := 1; if RmVal >=1 then InWords := InWords + " " + towords(RmVal,0) + " lakhs"; Amt := Amt - Rmval * 100000; if Amt > 0 then InWords := InWords + " " + towords(truncate(Amt),0); pAmt := (Amt - truncate(Amt)) * 100; if pAmt > 0 then InWords := InWords + " and " + towords(pAmt,0) + " paisa only" else InWords := InWords + " only"; UPPERCASE(InWords)
Friday, 27 February 2015
Crystal Report Formula(Convert Number to word)
Monday, 23 February 2015
Difference Between Char, Nchar, Varchar and Nvarchar Data Types in SQL Server
To store data as characters, numeric values and special characters in a database, there are 4 data types that can be used. So what is the difference among all 4 of these data types?
- CHAR vs VARCHAR
- NCHAR vs NVARCHAR
Considering an example, we will look into each one of them.
- DECLARE @string CHAR(20)
- SET @string = 'Robin'
- SELECT @string AS 'String', DATALENGTH(@string) AS 'Datalength' , LEN(@string) AS 'Len'
Note: The LEN() method provides the length of a character excluding trailing blanks stored in the string expression whereas the DATALENGTH() method provides the number of byte spaces occupied by the characters in a string expression.
As you know we represent the character values within single quotes, for example 'Robin'. But do you know we can represent these same characters within double quotes similar to programming languages representing a string, for example “Robin”? This can be done by setting the value:
As you know we represent the character values within single quotes, for example 'Robin'. But do you know we can represent these same characters within double quotes similar to programming languages representing a string, for example “Robin”? This can be done by setting the value:
- SET QUOTED_IDENTIFIER OFF
By default, it is set to ON
CHAR vs VARCHAR
CHAR vs VARCHAR
Talking about the CHAR data type:
- It is a fixed length data type
- Used to store non-Unicode characters
- Occupiers 1 byte of space for each character
If the value provided to a variable of CHAR data type is shorter than the length of a column of declared the size of the variable, then the value would be right-padded with blanks to match the size of column length.
- DECLARE @string CHAR(20)
- SET @string = 'Robin'
- SELECT @string AS 'String', DATALENGTH(@string) AS 'Datalength' , LEN(@string) AS 'Len'

As you can see above, the bytes occupied by the variable are 20 even though the length of the characters is 5. That means that irrespective of the character stored in the column, it will occupy all bytes to store the value.
About the VARCHAR data type:
- It is a variable length data type
- Used to store non-Unicode characters
- Occupies 1 byte of space for each character
- DECLARE @string VARCHAR(20)
- SET @string = 'Robin'
- SELECT @string AS 'String', DATALENGTH(@string) AS 'Datalength' , LEN(@string) AS 'Len'

As you can see above, it is showing DATALENGTH as 5 which means it will use only the number of bytes equal to the number of characters. This will allow me to avoid wasting database space.
Note: If SET ANSI_PADDING is OFF when CREATE TABLE or ALTER TABLE is executed, a CHAR column defined as NULL is considered as VARCHAR.
When to use what?
If you are sure about the fixed length of the data that would be captured for any specific column then go for CHAR data type and if the data may vary then go for VARCHAR.
NCHAR vs NVARCHAR
Similar to CHAR data type, the NCHAR data type:
Note: If SET ANSI_PADDING is OFF when CREATE TABLE or ALTER TABLE is executed, a CHAR column defined as NULL is considered as VARCHAR.
When to use what?
If you are sure about the fixed length of the data that would be captured for any specific column then go for CHAR data type and if the data may vary then go for VARCHAR.
NCHAR vs NVARCHAR
Similar to CHAR data type, the NCHAR data type:
- Is a fixed length data type
- Used to store Unicode characters (for example the languages Arabic, German and so on)
- Occupies 2 bytes of space for each character
- DECLARE @string NCHAR(20)
- SET @string = 'Robin'
- SELECT @string AS 'String', DATALENGTH(@string) AS 'Datalength' , LEN(@string) AS 'Len'

As you can see above, the data length column shows 40 bytes even though the size declared is 20. It's because NCHAR holds 2 bytes of space for each character.
About the NVARCHAR data type:
- It is a variable-length data type
- Used to store Unicode characters
- Occupies 2 bytes of space for each character
- DECLARE @string NVARCHAR(20)
- SET @string = 'Robin'
- SELECT @string AS 'String', DATALENGTH(@string) AS 'Datalength' , LEN(@string) AS 'Len'

As in the output above, you will observe DATALENGTH column is showing only 10 as a value. That is because it occupies 2 bytes of space for each character and the data length is only 5 characters, therefore it will occupy 10 bytes of space in the database.
Thursday, 19 February 2015
Difference Between Const, ReadOnly and Static ReadOnly in C#
These
are very common keywords and are quite confusing. So today we will discuss
these keywords and try to understand them.
- Const: Const is nothing but "constant", a variable
of which the value is constant but at compile time. And it's mandatory to
assign a value to it. By default a const is static and we cannot change
the value of a const variable throughout the entire program.
Here I have created a class named Variables and defined all three variables, so first let's play with const.
Here I tried to de-initialize the const variable, it gaves me an error like "A const field requires a value to be provided". Ok now I initialize a value for this variable and try to change it further in the class.
Here I have created a static constructor, default constructor, parameterized constructor and a Simple Method. I tried to change the value of the const variable everywhere but once I assign the value, I am unable to change it again since when I do it gives me a compile time error as you can see in the snapshot above.
Now let's on to the readonly keyword.
- Readonly: Readonly is the keyword whose value we can
change during runtime or we
can assign it at run time but only
through the non-static constructor. Not even a method. Let's see:
Here first I try to initialize the value in the static constructor. It gives me an error. Which you can see above.
Now I try to change the value in a method, see what happened:
Here, it is also giving an error that you can only assign a value either through a variable or a constructor.
Now try to change the value in the default constructor.
Now in the snapshot above you can see it's built successfully without an error, warning or messages. Let's check if there is a runtime error. OK.
Now here we can see that there is not a runtime error and the value was assigned successfully to the Readonly variable.
Now one gotcha is, now that you have assigned the value, can you change this value again ???
Let's try to change the value again.
Here I created a parameterized constructor and created a new object, and passing a value as "Hello Frend'z" and as I built it, it gave me the result "Build Succeeded". Now let's move ahead and check for a runtime error:
See guys. There is no runtime error !! And the value can be changed again and again through a constructor.
Now move ahead to Static Readonly variables.
- Static ReadOnly: A Static Readonly type variable's value can be
assigned at runtime or assigned at compile time and changed at runtime.
But this variable's value can only be changed in the static constructor.
And cannot be changed further. It can change only once at runtime. Let's
understand it practically.
Now in the preceding you can see that I used two variables, one is not assigned and another is assigned, and the static constructor. Now in the static constructor you can see that the unassigned variable is being assigned and the assigned value is being changed. And there is no compile time error. Further I try to again change this variable's value. See what happened:
As you can see in the above, I created Default, Parameterized Constructor and Method and tried to change the value again here. But I am getting a compile time error for all.
Read only
and const variable in C#
in this article we will learn about
the read only and constant variables in C#
Background
Whenever i took the interview of any candidate then i always asked one question to them what is read only and constant variable then believe me even four plus experienced candidate get confused ,so whatever i learned from my tech gurus about this concept according to that i will try to explain it in details so beginners and experienced candidate can understand it.
so let us start with definition
Whenever i took the interview of any candidate then i always asked one question to them what is read only and constant variable then believe me even four plus experienced candidate get confused ,so whatever i learned from my tech gurus about this concept according to that i will try to explain it in details so beginners and experienced candidate can understand it.
so let us start with definition
What is Constant variable ?
The variable whose value can not be changed during the execution of the program is called as constant variable.
The variable whose value can not be changed during the execution of the program is called as constant variable.
in the above definition
value can not be changed during execution of the program means we can not be
assign the values to constant variable at run time it must be assign at compile
time at the declaration of constant variable.
constants are of
following types

In the above diagram
,we can see the Constants are of two types
- Compile
time constants (const)
- Runtime
constants (Readonly)
- Compile time constants
The
compile time constants are declared by using the const keyword which value can not be changed during the execution of
the program.
Syntax
int const a=10;
Some key points about const variable
- Its must to assign value
at the time of declaration.
eg.
int const a=10;
- const only allow
constant variables into the expression.
eg.
int const a=10;
int const b=20; // correct
int const c=a+b;
int d=1;
int const c=d+b; //compile time error because d variable is the non constant into
expression.
- const can be declared at
class level as wel as inside the method.
- const can not be declared
using static keyword because they are by default static.
- constants are
absolute constant which value can no be changed or assigned at the run
time.
- constant variable are compile
time constant variable.
When to use const
The const is used when
their is value is absolute constant such PI values which can not be changed,but
according to your requirement you can use it as wish as rather than PI values
declaration.
Example of const variable.
1.
using System;
2.
3.
namespace UsingConst
4.
{
5.
class Program
6.
{
7.
const int a = 10;
8.
9.
static void Main(string[] args)
10.
{
11.
const int b = 20;
12.
const int c = b + a;
13.
Console.WriteLine(c);
14.
Console.ReadLine();
15.
}
16.
}
17.
}
the output of
the above program is 30,from the above example we clearly see that the const
must be assign the value at declaration time and in expression both the
variable must be const.
Runtime constants (Readonly)
The
Run time constants are declared by using the Readonly keyword which value
can not be changed during the execution of the program.
Syntax
int Readonly a; or
int Readonly a=0;
Some key points about const variable
- Its not must to
assign value at the time of declaration,we can also assign the value for
readonly through constructor.
eg.
int readonly a;
a=0;
- Readonly allows,
readonly constant as wel as non readonly constant
variables into the expression.
eg.
int readonly a=10;
int b=1;
int readonly c=a+b;
- Readonly can be declared only
at class level not inside the method.
- Readonly can not be declared
using static keyword because they are by default static.
- Readonly constants
value can be set through reference variable.
- Readonly constant
variable are runtime time constant variable.
When to use Readonly
we can use Readonly, when value is not absolute
constant means which can be changed frequently,like dollar vs INR ,in
this requirement we can set the value through configuration file or another
variable expression so we can avoid to change class file frequently.
Example of Readonly
variable.
let us define the
value from config file for readonly constant variable ,which will be set
through constructor .
1.
<configuration>
2.
<appSettings>
3.
<add key="DollarPrice" value="61.23"/>
4.
</appSettings>
5.
6.
</configuration>
now,let us
explain it through sample program
1.
using System;
2.
using System.Configuration;
3.
4.
namespace Usingreadonly
5.
{
6.
class Program
7.
{
8.
readonly int a = 10;
9.
int b = 30;
10.
int c;
11.
readonly string r;
12.
public Program()
13.
{
14.
r = ConfigurationManager.AppSettings["DollarPrice"];
15.
Console.WriteLine("The dollar value is:"+" "+r);
16.
Console.WriteLine();
17.
c = a + b;
18.
Console.WriteLine("The addition of readonly constant and non Readonly constant is :"+Environment.NewLine+ c);
19.
Console.ReadLine();
20.
}
21.
22.
static void Main(string[] args)
23.
{
24.
Program p = new Program();
25.
26.
Console.ReadLine();
27.
}
28.
}
29.
}
in the above
program ,we have assigned the value for readonly constant through constructor
which is defined in the config file,because readonly constant not must to
assign the value at the time of declaration.
now run the program
the output will be as.

let us outline
the differences between const and readonly variables.
- const fields has to be
initialized while declaration only, while readonly fields can be
initialized at declaration or in the constructor.
- const variables can declared in
methods ,while readonly fields cannot be declared in methods.
- const fields cannot be used
with static modifier, while readonly fields can be used with static
modifier.
- const field is a
compile-time constant, the readonly field can be used for run time
constants.
Subscribe to:
Posts (Atom)