Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Wednesday, May 9, 2007

Selecting concatanation fo two columns in SQL

Okie...if you want to select firstname + lastname as fullName its eazy ...and anyone can do it...

But, what when you want to select three numbers concatanated...namely, month, date and year...

try using "+" and the SQL server gives you the sum of the three numbers which is no good...

Here is a tiny trick i played on SQL server...

SELECT (Upper(MONTH)) + '/'+Upper(Day)+'/' +Upper(Year) as Date FROM DateTable...

when the SQL server does toUpper function, it assumes, the argument is a string and volah..the query runs seemlessly

Have fun coding...

Thursday, April 26, 2007

Identity reset for SQL Server 2000

Have you ever wanted to delete all the data from the table just for testing purposes...

It usually takes me around 2 mins to delete the data and reset the identity..

Here is a handy script which would actually reset the seed back to 0

Delete FROM tableName;
DBCC CHECKIDENT('tabelName', RESEED, 0)

Be careful though when working with foreignkeys...

I usually drop all relationships while am testing a perticular table and once the data insertion part makes me happy, I put the relations back and test again...

makes deletions and resets eazier...