Beyondrelational

Monday, February 13, 2012

SQL SERVER – Selecting Domain from Email Address

Following script which will extract the domain and will also count how many email addresses are there with the same domain address.



declare @t table(email varchar(100))
insert into @t
select 'test@yahoo.com' union all
select 'test@msn.com' union all
select 'test@gmail.com' union all
select 'test1@yahoo.com' union all
select 'test@sify.com' union all
select 'test2@yahoo.com' union all
select 'test3@msn.com' union all
select 'test4' union all
select '' union all
select null union all
select 'test1@gmail.com'


Method #1


select
stuff(email,1,charindex('@',email),'') as domain,
count(*) as emailcount
from
@t
where 
email > ''
group by
stuff(email,1,charindex('@',email),'')
order by
emailcount desc


Method #2


SELECT RIGHT(Email, LEN(Email) - CHARINDEX('@', email)) Domain ,
COUNT(Email) EmailCount
FROM   @t
WHERE  LEN(Email) > 0
GROUP BY RIGHT(Email, LEN(Email) - CHARINDEX('@', email))
ORDER BY EmailCount DESC


SELECT RIGHT(Email, LEN(Email) - CHARINDEX('@', email)) Domain ,
COUNT(Email) EmailCount
FROM @t
WHERE LEN(Email) > 0 and CHARINDEX('@', email) > 0
GROUP BY RIGHT(Email, LEN(Email) - CHARINDEX('@', email))
ORDER BY EmailCount DESC


Method #3


;WITH cte AS (
    SELECT CAST(
    '<i>' + REPLACE(email, '@', '</i><i>') + '</i>'
    AS XML).value('/i[2]', 'varchar(50)') AS Domain
    FROM @t 
)
SELECT
    Domain, 
    COUNT(*) AS Cnt
FROM cte
WHERE Domain IS NOT NULL
GROUP BY Domain 
ORDER BY COUNT(*) DESC


Method #4


select
substring(email,charindex('@',email)+1,len(email)) as domain,
count(*) as emailcount
from
@t
where
email>''
group by
substring(email,charindex('@',email)+1,len(email))
order by
emailcount desc



Above script will select the domain after @ character. Please note, if there is more than one @ character in the email, this script will not work as that email address is already invalid.


domain emailcount
yahoo.com 3
gmail.com 2
msn.com 2
sify.com 1

Different ways to know structure of a table


Generate SQL Script option from Enterprise Manager/Management Studio


select * from information_schema.columns where table_name='table_name'


EXEC sp_help 'table_name'


EXEC sp_columns 'table_name'


In Query Analyser type the name of the table, highlight it and press Alt+F1

Monday, August 1, 2011


Dynamically change the proper case


create function ProperCase(@Text as varchar(8000))
returns varchar(8000)
as
begin
   declare @Reset bit;
   declare @Ret varchar(8000);
   declare @i int;
   declare @c char(1);


   select @Reset = 1, @i=1, @Ret = '';
   
   while (@i <= len(@Text))
    select @c= substring(@Text,@i,1),
               @Ret = @Ret + case when @Reset=1 then UPPER(@c) else LOWER(@c) end,
               @Reset = case when @c like '[a-zA-Z]' then 0 else 1 end,
               @i = @i +1
   return @Ret
end






select dbo.ProperCase('this,my friends, is a test.wHat DO you think?i like shaquile o''neal')

Dynamic Change the text case in SQL SERVER


CREATE FUNCTION udf_TitleCase (@x varchar(7999))
RETURNS varchar(7999)
AS
  BEGIN


DECLARE @y int
SET @y = 1

SELECT @x = UPPER(SUBSTRING(@x,1,1))+LOWER(SUBSTRING(@x,2,LEN(@x)-1))+' '

WHILE @y < LEN(@x)
 BEGIN
SELECT @y=CHARINDEX(' ',@x,@y)
SELECT @x=SUBSTRING(@x,1,@y)+UPPER(SUBSTRING(@x,@y+1,1))+SUBSTRING(@x,@y+2,LEN(@x)-@y+1)
SELECT @y=@y+1
 END
RETURN @x
END


SELECT dbo.udf_TitleCase('THE QUICK FOX jUMPED OVER tHE LAZY DOG')

Friday, October 22, 2010

Useful Shortcut Keys




Other useful shortcut keys



Key                                                     »»» Description



ARROW KEYS  »»» Move one cell up, down, left, or right in a worksheet.


CTRL+ARROW KEY  »»» moves to the edge of the current data region in a worksheet.


SHIFT+ARROW KEY  »»» extends the selection of cells by one cell.


CTRL+SHIFT+ARROW KEY  »»» extends the selection of cells to the last nonblank cell in the same column or row as the active cell, or if the next cell is blank, extends the selection to the next nonblank cell.


LEFT ARROW or RIGHT ARROW  »»» selects the tab to the left or right when the Ribbon is selected. When a submenu is open or selected, these arrow keys switch between the main menu and the submenu. When a Ribbon tab is selected, these keys navigate the tab buttons.


DOWN ARROW or UP ARROW  »»» selects the next or previous command when a menu or submenu is open. When a Ribbon tab is selected, these keys navigate up or down the tab group.
In a dialog box, arrow keys move between options in an open drop-down list, or between options in a group of options.


DOWN ARROW or ALT+DOWN ARROW  »»» opens a selected drop-down list.


BACKSPACE  »»» Deletes one character to the left in the Formula Bar.
Also clears the content of the active cell.
In cell editing mode, it deletes the character to the left of the insertion point.


DELETE  »»» Removes the cell contents (data and formulas) from selected cells without affecting cell formats or comments.
In cell editing mode, it deletes the character to the right of the insertion point.


END  »»» Moves to the cell in the lower-right corner of the window when SCROLL LOCK is turned on.
Also selects the last command on the menu when a menu or submenu is visible.


CTRL+END  »»» moves to the last cell on a worksheet, in the lowest used row of the rightmost used column. If the cursor is in the formula bar, CTRL+END moves the cursor to the end of the text.


CTRL+SHIFT+END  »»» extends the selection of cells to the last used cell on the worksheet (lower-right corner). If the cursor is in the formula bar, CTRL+SHIFT+END selects all text in the formula bar from the cursor position to the end—this does not affect the height of the formula bar.


ENTER  »»» Completes a cell entry from the cell or the Formula Bar, and selects the cell below (by default).
In a data form, it moves to the first field in the next record.
Opens a selected menu (press F10 to activate the menu bar) or performs the action for a selected command.
In a dialog box, it performs the action for the default command button in the dialog box (the button with the bold outline, often theOK button).


ALT+ENTER  »»» starts a new line in the same cell.


CTRL+ENTER  »»» fills the selected cell range with the current entry.


SHIFT+ENTER  »»» completes a cell entry and selects the cell above.


ESC  »»» Cancels an entry in the cell or Formula Bar.
Closes an open menu or submenu, dialog box, or message window.
It also closes full screen mode when this mode has been applied, and returns to normal screen mode to display the Ribbon and status bar again.


HOME  »»» Moves to the beginning of a row in a worksheet.
Moves to the cell in the upper-left corner of the window when SCROLL LOCK is turned on.
Selects the first command on the menu when a menu or submenu is visible.


CTRL+HOME  »»» moves to the beginning of a worksheet.


CTRL+SHIFT+HOME  »»» extends the selection of cells to the beginning of the worksheet.


PAGE DOWN  »»» Moves one screen down in a worksheet.


ALT+PAGE DOWN  »»» moves one screen to the right in a worksheet.


CTRL+PAGE DOWN  »»» moves to the next sheet in a workbook.


CTRL+SHIFT+PAGE DOWN  »»» selects the current and next sheet in a workbook.


PAGE UP  »»» Moves one screen up in a worksheet.


ALT+PAGE UP  »»» moves one screen to the left in a worksheet.


CTRL+PAGE UP  »»» moves to the previous sheet in a workbook.


CTRL+SHIFT+PAGE UP  »»» selects the current and previous sheet in a workbook.


SPACEBAR  »»» In a dialog box, performs the action for the selected button, or selects or clears a check box.


CTRL+SPACEBAR  »»» selects an entire column in a worksheet.


SHIFT+SPACEBAR  »»» selects an entire row in a worksheet.


CTRL+SHIFT+SPACEBAR  »»» selects the entire worksheet.
* If the worksheet contains data, CTRL+SHIFT+SPACEBAR selects the current region. Pressing CTRL+SHIFT+SPACEBAR a second time selects the current region and its summary rows. Pressing CTRL+SHIFT+SPACEBAR a third time selects the entire worksheet.
* When an object is selected, CTRL+SHIFT+SPACEBAR selects all objects on a worksheet.


ALT+SPACEBAR  »»» displays the Control menu for the Microsoft Office Excel window.


TAB  »»» Moves one cell to the right in a worksheet.
Moves between unlocked cells in a protected worksheet.
Moves to the next option or option group in a dialog box.


SHIFT+TAB  »»» moves to the previous cell in a worksheet or the previous option in a dialog box.


CTRL+TAB  »»» switches to the next tab in dialog box.


CTRL+SHIFT+TAB  »»» switches to the previous tab in a dialog box.