Tag Archives: TSQL

Move Data Files – SQL Server

[adsense slot=”5402167102″ width=”468″]

One of those clean-up tasks we come across in development severs is to move database mdf and ldf files to a new location. SQL Server isn’t as GUI-friendly yet to allow one to just change the paths on database properties. I have tried this out in SQL Server 2008, but the method must essentially be the same in 2005 and 2000 versions also.

What we have to do is this.
– Detach the Database
– Move the files
– Attach the database with the new location


Instead of “use master”, you can just set the query window drop-down to any DB other than DB_To_Move

Now, you need to move the files from its current location to wherever you need it to be (say, d:\data\)


Again, ideally it is just the sp_attach_db line that you would need to execute. Make sure the file security permissions are set aptly at the target location.

However, things get a bit tricky if you want to move system db files to a new location (we don’t usually findourselves doing this). Please refer this for further reference on that.

SELECT column values into a concatenated string (SQL)

Often there is a requirement to get column values in a table to a concatenated string. Say, we have e-mail ID as one column on a person table and want a comma separated list of all (or a subset) of e-mail ID’s.

Here is one dirty little piece of code to do just that! :)

The above query returns a concatenated string of values in EmailID column, separated by semi-colon.