Wednesday, January 26, 2011

SQL "How To's"

1)How to display Year as YYYY instead of YY

Enterprise Manager adopts the User's regional settings for the date format. By default, the regional setting for the short date format is m/d/yy. To display it in m/d/yyyy format, follow the following steps:

On the taskbar, click the Start button, point to Settings and then select Control Panel. In the Control Panel dialog box double-click Regional Settings. In the Regional Settings Properties dialog box, select the Date tab. Next, change the value for the Short Date Style from M/d/yy to M/d/yyyy.


2) How to Build a Comma Delimited String from Table values

Sometimes you might want to return your records in a single comma delimited string. For example, if you have records like :-

Mango
Banana
Peach
Grapes
Strawberry

Instead of returning it as a recordset containing these 5 records, you might want to return them as 'Mango, Banana, Peach, Grapes, Strawberry'. Here's how you can accomplish this.

-- Create a temporary table & insert dummy records

Create Table #Fruits (Fruit Varchar(25)) Insert #Fruits (Fruit) values('Mango') Insert #Fruits (Fruit) values('Banana') Insert #Fruits (Fruit) values('Peach') Insert #Fruits (Fruit) values('Grapes') Insert #Fruits (Fruit) values('Strawberry') 
-- Build comma delimited string
Declare @Fruits Varchar(200) Set @Fruits = '' Update #Fruits Set @Fruits = @Fruits + Fruit + ',' Set @Fruits = Substring(@Fruits,1,len(@Fruits)-1)               -- to remove extra comma at the end print @Fruits

3) How to add a User Defined Error Message

Use Master Go  EXEC sp_addmessage @msgnum = 50001, @severity = 16,     @msgtext = N'Failed to insert Customer Transaction into %s table',     @lang = 'us_english'  Go 

You can raise this message from your code like this :-

INSERT Customers (CustomerName) VALUES ('Rick Nelson')  IF @@Error <> 0             RAISERROR (50001,16,1, 'Customers')

4) How to get the Rowcount as well as Error at the same time

Normally when we run a Query, we check if the query was executed successfully or not before commiting it.

DECLARE @CustomerId            Int SET @CustomerId = 1  Begin Transaction Update Orders Set OrderDate = GetDate() Where CustomerId = @CustomerId  IF @@Error <> 0 Begin             Raiserror('Failed to update Orders',16,1)                Rollback Transaction End  Commit Transaction 

But what if you also want to get the number of records which were effected by this query? If you do a @@Rowcount after your Error checkingstatement then you are going to get 0 as the value of @@Recordcount would have been reset. And if you place @@Recordcount before the error-checking statement then your @@Error would get reset. So what's the solution to get both of them at the same time ? Save both the values in a local variable and then check that local variable. Here's how it can be done.

DECLARE @Rcount                      Int DECLARE @ErrNum                    Int  DECLARE @CustomerId            Int  SET @Rcount = 0 SET @ErrNum = 0 SET @CustomerId = 1   Begin Transaction Update Orders Set OrderDate = GetDate() Where CustomerId = @CustomerId  SELECT @Rcount = @@RowCount, @ErrNum = @@Error  -- check if there was an error in Updating the records IF @ErrNum <> 0 Begin             Raiserror('Failed to update Orders',16,1)                Rollback Transaction End  -- check if some records were updated or not IF @Rcount = 0 Begin             Raiserror('No records for CustomerId %d',16,1, @CustomerId)                          Rollback Transaction End  Commit Transaction

Thursday, January 20, 2011

String Palindrome

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Palindrome
{
///
/// To check whether a word or a sentence is a palindrome or not.
/// A palindrome is a word or a sentence that reads the same way from left to right or from right to left.
///

class Program
{
static void Main ( string [ ] args )
{
Console.WriteLine ( "Enter the word or sentence to check Palindrome : " );
string strInput = Console.ReadLine ( );

//traverse through the string with a index at right and left, incrementing and
//decrementing respectively until both value is equal.

int iRightCounter = strInput.Length - 1;
bool bFlag = true;

for ( int iLeftCounter = 0; iLeftCounter < strInput.Length; iLeftCounter++, iRightCounter-- )
{
if ( strInput [ iLeftCounter ] != strInput [ iRightCounter ] )
{
bFlag = false;
break;
}
}

if ( bFlag )
Console.WriteLine ( "String is a Palindrome" );
else
Console.WriteLine ( "Not a Palindrome" );

Console.ReadLine ( );
}
}
}

Fibonacci sereis in C# [ Recursive and non Recursive]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FibonacciSereis
{
class Program
{
///
/// Get the number of sequequce required from the user
/// and prints the sereis.
///
///
static void Main ( string [ ] args )
{
//Fibonacci Sereis : 0, 1 , 1, 2 , 3, 5, 8, 13....
Console.WriteLine ( "Enter the number of Fibonacci sequence required : " );
int inum = Convert.ToInt32(Console.ReadLine ( ));

Console.WriteLine ( "****************" );

for ( int icount = 0; icount <>
{
Console.Write ( CalculateFibonacci ( icount ) +" " );
}

Console.WriteLine ( "\n***************************************************" );

CalFib ( inum );

//to hold the console to see the output.
Console.ReadLine ( );
}

///
/// Recursive function to calculate nth fibnacci number.
///
///
///
public static int CalculateFibonacci ( int n )
{
if ( n == 0 ) return 0;
if ( n == 1 ) return 1;
return CalculateFibonacci ( n - 1 ) + CalculateFibonacci ( n - 2 );
}

///
/// Non recursive function to find fibonacci sereis.
///
///
public static void CalFib ( int n )
{
//these are the seeds of fibonacci.
int ifirstNum = 0;
int iNextNum = 1;
int iResult = 0;

Console.Write ( ifirstNum + " " + iNextNum + " " );
for ( int icount = 2; icount <>
{
iResult = ifirstNum + iNextNum;

Console.Write ( iResult + " " );

ifirstNum = iNextNum;
iNextNum = iResult;
}

}

}
}

Tuesday, January 11, 2011

Stylizing the heading using font and border

Font property

font-style
font-variant
font-weight
font-size/line-height
font-family

The first three values can be placed in any order, while the others need to be in the order.
When you want to include the line-height value, put a forward slash between the font-size value and the line-height value.
p
{
font:1em/1.5em verdana, arial, sans-serif;
}

When setting style heading, remember that browsers have their own default values for padding and margins of paragraphs and heading tags.

-Use border-top and border-bottom properties when setting the style for the heading.

h2
{
font:bold italic 2em Gerorgia, Times, "Times New Roman", serif;
border-bottom:2px dashed black;
border-top:10px solid black;
margin:0;
padding:0.5em 0 0.5em 0;
font-size:1em;
}
p
{
margin:0;
padding :10px 0 0 0;
}

when setting borders, make sure to adjust the padding to put enough whitespace between the borders and the text of the heading. This aids to readability.

For styling Heading with text and image
p
{
font:bold italic 2em Gerorgia, Times, "Times New Roman", serif;
background-image:url(tall_grass.jpg);
background-repeat:repeat-x;
background-position:bottom;
border-bottom:10px solid black;
margin:0;
padding:0.5em 0 60px 0;
}

Setting Initial Cap

1. Begin the paragraph with a initial cap.

(traditional way)
1.Mark the paragraph of content with p element.

Online, activity of exchanging ideas is sped up.

2.Use the pseudo-element : first-letter
p:first-letter{
font-size:1.2em;
background-color:black;
color:white;
}
(result:increased font size with black background)

OR

1.wrap a span element with a class attribute around the first element.

Online, activity of exchanging ideas is sped up.

2.Set the style
p.initcap
{
font-size:1.2em;
background-color:black;
color:white;
}

2.Setting a Larger, Centered Initial Cap

To place a large initial cap at the center of the paragraph.

1.wrap a span element with a class attribute around the first element.

Online, activity of exchanging ideas is sped up.

2.set style

p{
text-indent:37%; //moves fist line toward the middle of the paragraph. 37% is little more than 1/3
line-height:1em;
}
p.initcap
{
font-size:6em; //makes font 6 times(600%) larger than default size.
line-height:0.6em;/*since font -size is 6 times larger as the rest of the type, the leading on the first line is now deeper than it is on the remaining lines. So adjust it*/
font-weight:bold;
}

Adjust values based on letter of initcap and width of paragraph.

3.Setting Initial cap with decoration( imagery)

1.wrap a span element with a class attribute around the first element.

Online, activity of exchanging ideas is sped up.

2.set style
p.initcap
{
display:none;//hide first letter of html
}
create image and make a note of width n height say 35px by 45px
set the image
p
{
line-height:1em;
background-image:url(initcap-o.gif);//put image
background-repeat:no-repeat;//no repeatition.
text-indent:45px; //width of image
padding-top:35px; //height of image.
}

Adjust value of text-indent and padding -top values so that the initial cap appears to rest on the baseline.


Setting Initial Cap

1. Begin the paragraph with a initial cap.

(traditional way)
1.Mark the paragraph of content with p element.

Online, activity of exchanging ideas is sped up.

2.Use the pseudo-element : first-letter
p:first-letter{
font-size:1.2em;
background-color:black;
color:white;
}
(result:increased font size with black background)

OR

1.wrap a span element with a class attribute around the first element.

Online, activity of exchanging ideas is sped up.

2.Set the style
p.initcap
{
font-size:1.2em;
background-color:black;
color:white;
}

2.Setting a Larger, Centered Initial Cap

To place a large initial cap at the center of the paragraph.

1.wrap a span element with a class attribute around the first element.

Online, activity of exchanging ideas is sped up.

2.set style
p{
text-indent:37%; //moves fist line toward the middle of the paragraph. 37% is little more than 1/3
line-height:1em;
}
p.initcap
{
font-size:6em; //makes font 6 times(600%) larger than default size.
line-height:0.6em;/*since font -size is 6 times larger as the rest of the type, the leading on the first line is now deeper than it is on the remaining lines. So adjust it*/
font-weight:bold;
}

Adjust values based on letter of initcap and width of paragraph.

Enforcing FOnt sizes

To override control over font sizes

p
{
font-size: 12px !important;
}

In some browsers, a user can have a style sheet set up for browsing the web that enables him to set font sizes to his liking. But as a designer, you might want to make sure your designs render in the manner you planned. The "!important" rule gives you a little insurance that our designs remain intact.

CSS Font-Size

Setting the size of the font with percentage causes the browser to calculate the size of the font based on the size of the parent element. For eg., if is set too the font size for the body 12 pixels and the font size for p element is set to 125%, the font size for the text in paragraphs is 15 pixels.

You can use following to set type size
* percentage
*length units
*font size

Length units falls into two categories
*Absolute
*Relative.


Absolute Length Units
  • Inches(in)
  • Centimeters(cm)
  • Millimeters(mm)
  • Points(pt) : is equal to 1/72nd of an inch
  • Picas(pc) : is equal to 12 points.
Note:
  • Use Absolute(fixed) measurement for web documents of fixed output.
  • If you want to make the text invisible, use the CSS properties visibility or display instead of setting the size of fonts to zero.
  • Negative length value, such as -12cm, for font-size is not allowed.
  • Mac and Windows render point sizes differently and pixel same.

Relative Length Units
  • Em :refer to the default font size set in the preference of the user's browser
  • X-height(ex) : refers to the height of the lowercase letter x in the font.
  • Pixels(px) : consistently control the size of typography in web documenlt. ( Not good for Netscape Navigator 4.7x, which doesn't display pixel size values correctly and Opera 5 for Mac, which displays pixel lengths smaller than the size set in the style sheet. Also, in IE we can't resize if type used is pixel.)
Font size Keyword
Absolute sizes - xx-small, x-small, small, medium, large, x-large,xx-large.
Relative sizes - larger, smaller.

Advantage of using font size keywords is that it make it easy to enlarge or reduce the size of the text in most browsers, and the font size in browsers never go smaller than nine pixels.




CSS Font Family

1.serif - Georgia, Times, Times New roman, Garamond, and Century Schoolbook
2.san-serif - Verdana, Arial, Helvetica, Trebuchet, and Tahoma
3.monospace - Courier, MS Courier New, and Prestige
4.cursive - Lucida Handwriting and Zapf-Chancery
5.fantasy - Comic Sans, Whimsey, Critter, and Cottonwood.

Monday, January 10, 2011

To Clear the Form Controls in Single Click

public static void ClearForm(System.Windows.Forms.Control parent)
{
foreach (System.Windows.Forms.Control ctrControl in parent.Controls)
{
//Loop through all controls
if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.TextBox)))
{
//Check to see if it's a textbox
((System.Windows.Forms.TextBox)ctrControl).Text = string.Empty;
//If it is then set the text to String.Empty (empty textbox)
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RichTextBox)))
{
//If its a RichTextBox clear the text
((System.Windows.Forms.RichTextBox)ctrControl).Text = string.Empty;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.ComboBox)))
{
//Next check if it's a dropdown list
((System.Windows.Forms.ComboBox)ctrControl).SelectedIndex = -1;
//If it is then set its SelectedIndex to 0
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.CheckBox)))
{
//Next uncheck all checkboxes
((System.Windows.Forms.CheckBox)ctrControl).Checked = false;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RadioButton)))
{
//Unselect all RadioButtons
((System.Windows.Forms.RadioButton)ctrControl).Checked = false;
}
if (ctrControl.Controls.Count > 0)
{
//Call itself to get all other controls in other containers
ClearForm(ctrControl);
}
}
}

//Example call
ClearForm(this);

How to change a NOT NULL column to allow NULLs?

ALTER TABLE Keyword does not seem to have this ability. A column that allows
NULL is one byte wider than a NOT NULL column, therefore you will need a
new page layout. So even if there was a command like ALTER TABLE...ALLOW
NULL, DB2 would have to do all the steps you described.
Need Import the data and export it and make the required changes.