SQL Server 2005 Express Import/Export Functionality

SQL 2005 Express is a great little server from Microsoft that allows you to run a slimmed down version of SQL Server 2005 for free. You can even use it to host websites, and as long as your individual databases don't run over 4GB of storage, and you don't care about using more than 2GB of RAM, it might be the perfect candidate for your environment.

The reason Microsoft did this is to fight back against the free products from other vendors. Other alternatives include PostgreSQL, MySQL, Oracle Lite among others. With the release of this free Express edition of SQL Server, Microsoft aims to get people to use SQL Server, and keep customers from flocking to open source alternatives. Once they have you locked it, they are hoping that at some point you will either need the tools or features that are offered by the SQL Server 2005 Standard or that you will hit the 4GB database limit.

Since SQL Server Express 2005 was first released, people complained that it didn't provide full text search capabilities, among other things. With Service Pack 1, Microsoft has addressed this concern and others.

What many people don't know, however, is that part of the SP1 you can also download the Microsoft SQL Server 2005 Express Edition Toolkit SP1, which includes an Import/Export wizard, among other things. Just download it here and you will get the SQL Server Import and Export Wizard installed by default to "C:\Program Files\Microsoft SQL Server\90\DTS\Binn\DTSWizard.exe". You can even create a link to it straight from the Microsoft SQL Server Management Studio Express by going to Tools->External Tools and defining and Import/Export Wizard shortcut, by pointing the command parameter to "C:\Program Files\Microsoft SQL Server\90\DTS\Binn\DTSWizard.exe"

Download Link

SQL Server - "Arithmetic overflow error converting expression to data type int."

Today, when I tried to do a

select count(*) from sometable cross join someothertable cross join athirdtable
I got the following error:

Arithmetic overflow error converting expression to data type int.

Turns out you need to use the COUNT_BIG(*) function instead.

select count_big(*) from sometable cross join someothertable cross join athirdtable

ProperCase Function for SQL Server

Have you ever wanted to convert some text that you have in a database to proper case? Maybe you have data that's all in uppercase or all in lowercase, and you want it displayed properly? Here is a handy function that will allow you to do this using just SQL queries, instead of resorting to pulling the data into another programming language like ColdFusion, as that would be a lot slower.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[ProperCase](@Text as varchar(8000))
returns varchar(8000)
as
begin
declare @Reset bit;
declare @Ret varchar(8000);
declare @i int;
declare @c char(1);
declare @lookahead char(1);
declare @lookahead2 char(1);

select @Reset = 1, @i=1, @Ret = '';

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

So lets say you have data like this:

select top 10 firstName from names

firstname
-----------
AARON
ABBEY
ABBIE
ABBY
ABDUL
ABE
ABEL
ABIGAIL
ABRAHAM
ABRAM

(10 row(s) affected)

Lets see what it would look like proper cased:

select top 10 dbo.properCase(firstName) as firstName from names

firstName
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aaron
Abbey
Abbie
Abby
Abdul
Abe
Abel
Abigail
Abraham
Abram

(10 row(s) affected)

Now lets update the table and see the results:

update names set firstName=dbo.properCase(firstname)
select top 10 firstName from names
firstName
-----------
Aaron
Abbey
Abbie
Abby
Abdul
Abe
Abel
Abigail
Abraham
Abram

(10 row(s) affected)

We've successfully updated all our names to propercase. Note that this function should work well with longer text and full sentences. Here is an example:

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

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This,My Friends, Is A Test.What Do You Think?I Like Shaquile O'Neal

(1 row(s) affected)

SQL Server Update with a join

Have you ever needed to update one table based on the contents of another table? The way to do it is with aliases.

update t1 set t1.field=replace(t1.field, t2.field1, t2.field2) from table1 t1 inner join table2 t2 on t1.primaryKey=t2.foreignkey

SQL Server loops and prefilling table data

Let's say you need a table that has numbers from 1 to 10.

declare @i integer
set @i=0
while (@i<10)
begin
   insert into digits (digit) values (@i)
   set @i=@i+1
end

select * from digits

digit
------
0
1
2
3
4
5
6
7
8
9

(10 row(s) affected)

Lets say you wanted all the letters in a table. Checking www.asciitable.com, we see that A has the ASCII value of 65, and Z has the ASCII value of 90.

declare @i integer
set @i=65
while (@i<=90)
begin
   insert into letters (letter) values (CHaR(@i))
   set @i=@i+1
end

select * from letters

letter
------
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

(26 row(s) affected)

Deleting duplicate rows from SQL Server

Lets say you have a table with duplicate data in it, and you'd like to get rid of it easily. For simplicity's sake, lets say you have a table named names and it contains two columns, nameid and firstname.

select top 10 * from names order by firstName
nameId firstName
----------- -----------
77 AARON
3920 AARON
2802 ABBEY
2260 ABBIE
1913 ABBY
831 ABDUL
854 ABE
485 ABEL
1739 ABIGAIL
347 ABRAHAM

You can see right off the bat, that you have 2 Aaron's. Lets find out how many duplicate names you have total.

select firstName from names group by firstName having count(firstName)>1 order by firstName
firstName
-----------
AARON
ADAM
ADRIAN
ALBERT
ALEX
ALEXANDER
ALEXIS
ALI
ALLEN
ALVA
ANDRE
ANDREA
ANDREW
ANGEL
ANGELO
ANTHONY
ANTONIA
ANTONIO
ARIEL
ARTHUR
ASHLEY
AUBREY
AUGUSTINE
AUSTIN
AVERY
BENNIE
BERNIE
BERRY
BILLIE
BILLY
BLAIR
BLAKE
BOBBIE
BOBBY
BRANDON
BRETT
BRIAN
BRITT
CAMERON
CAREY
CARL
CARLOS
CARMEN
CAROL
CARROL
CARROLL
CARY
CASEY
CECIL
CHANG
CHARLES
CHARLIE
CHI
CHONG
CHRIS
CHRISTIAN
CHRISTOPHER
CHUNG
CLAIR
CLARENCE
CLAUDE
CLEO
CLYDE
CODY
COLBY
CONNIE
COREY
CORY
COURTNEY
CRUZ
CURTIS
DALE
DALLAS
DAN
DANA
DANIEL
DANNIE
DARNELL
DARYL
DAVID
DEAN
DEE
DEMETRIUS
DENNIS
DENNY
DEON
DEVIN
DEVON
DION
DOMINIQUE
DONALD
DONG
DONNIE
DORIAN
DREW
DUSTY
EDDIE
EDWARD
ELISHA
ELLIS
ELMER
ERIC
ERIN
EUGENE
EVAN
FRANCES
FRANCIS
FRANCISCO
FRANK
FRANKIE
FRED
FREDDIE
GABRIEL
GAIL
GALE
GARY
GAYLE
GENE
GEORGE
GERALD
GERRY
GLENN
GREGORY
GUADALUPE
HAROLD
HENRY
HOLLIS
HONG
IRA
IVORY
JACK
JACKIE
JAE
JAIME
JAME
JAMES
JAMEY
JAMIE
JAN
JASON
JAY
JEAN
JEFFREY
JEREMY
JERRY
JESSE
JESSIE
JESUS
JEWEL
JEWELL
JIMMIE
JIMMY
JOAN
JODY
JOE
JOEL
JOEY
JOHN
JOHNIE
JOHNNIE
JOHNNY
JON
JORDAN
JOSE
JOSEPH
JOSHUA
JUAN
JUDE
JULIAN
JULIO
JUSTIN
KARL
KASEY
KEITH
KELLEY
KELLY
KENDALL
KENNETH
KERRY
KEVIN
KIM
KIRBY
KRIS
KYLE
LACY
LANE
LARRY
LAUREN
LAURENCE
LAVERN
LAVERNE
LAWRENCE
LEE
LEIGH
LEO
LEON
LESLEY
LESLIE
LESTER
LEWIS
LINDSAY
LINDSEY
LOGAN
LONNIE
LOREN
LOU
LOUIE
LOUIS
LUIS
LUPE
LYNN
MAN
MARIA
MARIO
MARION
MARK
MARLIN
MARSHALL
MARTIN
MARTY
MARY
MATTHEW
MAURICE
MELVIN
MERLE
MERRILL
MICAH
MICHAEL
MICHAL
MICHEAL
MICHEL
MICKEY
MIKE
MINH
MITCHELL
MORGAN
NICKY
NOEL
NORMAN
NUMBERS
ODELL
OLLIE
OSCAR
OTHA
PARIS
PAT
PATRICIA
PATRICK
PAUL
PERRY
PETER
QUINN
RANDY
RAY
RAYMOND
REFUGIO
RENE
RICHARD
RICKIE
ROBBIE
ROBERT
ROBERTO
ROBIN
RONALD
RONNIE
RORY
ROSARIO
ROY
ROYCE
RUDY
RUSSELL
RYAN
SAM
SAMMIE
SAMMY
SAMUEL
SANDY
SANG
SANTOS
SCOTT
SCOTTIE
SEAN
SHANE
SHANNON
SHAUN
SHAWN
SHAYNE
SHELBY
SHIRLEY
SIDNEY
SOL
SON
STACEY
STACY
STEPHEN
STEVEN
STEVIE
SUNG
SYDNEY
TAYLOR
TERRELL
TERRY
THANH
THEO
THOMAS
TIMOTHY
TOBY
TOMMIE
TOMMY
TONY
TORY
TRACEY
TRACY
TRAVIS
TRINIDAD
TRISTAN
TROY
TYLER
VAL
VALENTINE
VAN
VERNON
VICTOR
VIRGIL
WALTER
WESLEY
WHITNEY
WILLIAM
WILLIE
YONG
YOUNG

(331 row(s) affected)

Turns out we have 331 duplicate names. Now, how do we get the Id of only 1 of the names?

select max(nameId) as nameId, firstName from names group by firstName having count(firstName)>1 order by firstName
nameId firstName
----------- -----------
3920 AARON
5053 ADAM
2178 ADRIAN
4673 ALBERT
3879 ALEX
3389 ALEXANDER
1676 ALEXIS
3653 ALI
4267 ALLEN
2391 ALVA
3744 ANDRE
1300 ANDREA
3422 ANDREW
1643 ANGEL
4265 ANGELO
3349 ANTHONY
1645 ANTONIA
3368 ANTONIO
2332 ARIEL
3916 ARTHUR
1282 ASHLEY
2284 AUBREY
3165 AUGUSTINE
5400 AUSTIN
4446 AVERY
2282 BENNIE
4107 BERNIE
4557 BERRY
1497 BILLIE
2700 BILLY
2575 BLAIR
5397 BLAKE
1507 BOBBIE
2238 BOBBY
3983 BRANDON
4361 BRETT
4801 BRIAN
3328 BRITT
3081 CAMERON
2134 CAREY
3366 CARL
4614 CARLOS
1330 CARMEN
1237 CAROL
2652 CARROL
2491 CARROLL
2713 CARY
1680 CASEY
2557 CECIL
4555 CHANG
2473 CHARLES
2412 CHARLIE
3387 CHI
2291 CHONG
1747 CHRIS
2122 CHRISTIAN
3187 CHRISTOPHER
3417 CHUNG
3161 CLAIR
5120 CLARENCE
4953 CLAUDE
1815 CLEO
3279 CLYDE
3592 CODY
5300 COLBY
1325 CONNIE
2598 COREY
2748 CORY
1464 COURTNEY
2405 CRUZ
5297 CURTIS
1915 DALE
3386 DALLAS
5036 DAN
1398 DANA
2540 DANIEL
4551 DANNIE
3562 DARNELL
3589 DARYL
2488 DAVID
2920 DEAN
1917 DEE
4354 DEMETRIUS
3620 DENNIS
5386 DENNY
4548 DEON
2825 DEVIN
2373 DEVON
5290 DION
1891 DOMINIQUE
3873 DONALD
5112 DONG
2614 DONNIE
3384 DORIAN
4492 DREW
3301 DUSTY
2076 EDDIE
4053 EDWARD
2242 ELISHA
5379 ELLIS
5032 ELMER
3644 ERIC
1379 ERIN
4212 EUGENE
4211 EVAN
1266 FRANCES
1612 FRANCIS
3943 FRANCISCO
3910 FRANK
1789 FRANKIE
5031 FRED
2323 FREDDIE
3254 GABRIEL
1374 GAIL
1909 GALE
3760 GARY
1594 GAYLE
2518 GENE
2535 GEORGE
4784 GERALD
2562 GERRY
4942 GLENN
4489 GREGORY
1523 GUADALUPE
4727 HAROLD
3558 HENRY
3537 HOLLIS
2846 HONG
2754 IRA
2604 IVORY
4436 JACK
1454 JACKIE
4168 JAE
1717 JAIME
5185 JAME
2094 JAMES
4126 JAMEY
1365 JAMIE
1559 JAN
3731 JASON
3345 JAY
1277 JEAN
4862 JEFFREY
5276 JEREMY
2057 JERRY
2263 JESSE
1434 JESSIE
3500 JESUS
1741 JEWEL
1803 JEWELL
1886 JIMMIE
4012 JIMMY
1281 JOAN
1621 JODY
2517 JOE
3867 JOEL
3114 JOEY
2038 JOHN
3158 JOHNIE
1610 JOHNNIE
3344 JOHNNY
4480 JON
2011 JORDAN
2927 JOSE
2581 JOSEPH
4860 JOSHUA
3077 JUAN
4541 JUDE
4011 JULIAN
5367 JULIO
4010 JUSTIN
4089 KARL
2066 KASEY
5018 KEITH
1669 KELLEY
1286 KELLY
2733 KENDALL
3803 KENNETH
1671 KERRY
3381 KEVIN
1337 KIM
3938 KIRBY
2052 KRIS
2807 KYLE
2151 LACY
3864 LANE
4381 LARRY
1381 LAUREN
4848 LAURENCE
2529 LAVERN
1674 LAVERNE
4711 LAWRENCE
1560 LEE
1658 LEIGH
4845 LEO
4589 LEON
1831 LESLEY
1362 LESLIE
5447 LESTER
5355 LEWIS
1521 LINDSAY
1520 LINDSEY
4772 LOGAN
2555 LONNIE
2504 LOREN
1823 LOU
4041 LOUIE
2550 LOUIS
4000 LUIS
1821 LUPE
1385 LYNN
4526 MAN
1226 MARIA
3376 MARIO
1397 MARION
2767 MARK
5007 MARLIN
5438 MARSHALL
4240 MARTIN
2716 MARTY
1220 MARY
3935 MATTHEW
3965 MAURICE
4287 MELVIN
2058 MERLE
4523 MERRILL
3632 MICAH
2018 MICHAEL
3964 MICHAL
3994 MICHEAL
3441 MICHEL
2689 MICKEY
5160 MIKE
3932 MINH
4329 MITCHELL
1795 MORGAN
3664 NICKY
2452 NOEL
3601 NORMAN
3335 NUMBERS
3129 ODELL
1716 OLLIE
5339 OSCAR
4283 OTHA
3225 PARIS
1606 PAT
1221 PATRICIA
3266 PATRICK
3013 PAUL
3928 PERRY
5250 PETER
4910 QUINN
2914 RANDY
2567 RAY
4282 RAYMOND
3750 REFUGIO
1893 RENE
2792 RICHARD
4906 RICKIE
1872 ROBBIE
2215 ROBERT
4687 ROBERTO
1319 ROBIN
3961 RONALD
2730 RONNIE
5245 RORY
1817 ROSARIO
4989 ROY
4419 ROYCE
4231 RUDY
5423 RUSSELL
2448 RYAN
3600 SAM
2502 SAMMIE
4571 SAMMY
5333 SAMUEL
1541 SANDY
3370 SANG
2831 SANTOS
3960 SCOTT
4683 SCOTTIE
3775 SEAN
3491 SHANE
1342 SHANNON
3889 SHAUN
1751 SHAWN
4984 SHAYNE
1738 SHELBY
1246 SHIRLEY
2702 SIDNEY
5236 SOL
3851 SON
1430 STACEY
1399 STACY
3819 STEPHEN
3886 STEVEN
3627 STEVIE
3488 SUNG
2230 SYDNEY
2024 TAYLOR
4818 TERRELL
1472 TERRY
3423 THANH
3816 THEO
3015 THOMAS
4561 TIMOTHY
2764 TOBY
1997 TOMMIE
2923 TOMMY
2763 TONY
4456 TORY
1499 TRACEY
1327 TRACY
5226 TRAVIS
2487 TRINIDAD
4031 TRISTAN
4367 TROY
3882 TYLER
3881 VAL
4454 VALENTINE
2935 VAN
5408 VERNON
4816 VICTOR
5405 VIRGIL
4143 WALTER
4674 WESLEY
1587 WHITNEY
2337 WILLIAM
1438 WILLIE
2307 YONG
2121 YOUNG

(331 row(s) affected)
Now we can delete the record with the highest id.
delete from names where nameID IN (select max(nameId) as nameId from names group by firstName having count(firstName)>1)
(331 row(s) affected)

If you wanted to delete the record with the lowest Id, you would run this code:

delete from names where nameID IN (select min(nameId) as nameId from names group by firstName having count(firstName)>1)
(331 row(s) affected)

If you have more then 1 set of duplicates, you can just run the last piece of code a bunch of times until you have no more rows affected, or you can put it in a loop.

BlogCFC was created by Raymond Camden.