Categories
SQL

Replacing first occurrence of certain characters in MSSQL database

This post is regarding how to replace first occurrence of one or more characters from database table Column.

Suppose we have table named User with columns Id, Name, Phone Number as shown in the figure below.

User database table
User database table

And contains records as shown in figure below:

User records before script
User records before script

Now what I required to accomplish is replace the first ‘-‘ character with space ‘ ‘ so that for example phone number 214-654-1800 becomes 214 654-1800. The script for this as below and next to it is the resulting data.

MS Sql Script:

DECLARE @find varchar(8000)
SELECT @find='-' -- << Replace character
UPDATE [User]
SET [PhoneNumber]=Stuff([PhoneNumber], CharIndex(@find, [PhoneNumber]), Len(@find), ' ') -- << Replacement character

After applying the script the changes happen as in figure below:

User records after applying script
User records after applying script

 

Reference link: Is it possible to replace the first occurrence of a string in a column ?

 

Categories
Interview Questions SQL Utility

MSSQL – Change default database by sql query

Microsoft SQL Server Management Studio showing...
Microsoft SQL Server Management Studio showing a query, the results of the query, and the Object Explorer pane while connected to a SQL Server database engine instance. (Photo credit: Wikipedia)

Dear reader,

Recently I noticed in my daily work that every time I logged in MS SQL I was required to select the database which I had to work on and this was regularly in use by me and this lead me to find the solution to change the default database selection after I log on to MS SQL using SSMS to reduce the hassle and also so that I don’t execute queries on different databases by mistake.

There is an inbuilt stored procedure name sp_defaultdb which can be used as below

Exec sp_defaultdb @loginame='Domain/UserName', @defdb='MyDefaultDatabaseName'

Here value of parameter @loginname is User Name such as ‘sa’ and it can change depending on the MSSQL login configurations such as Sql Authentication or Windows Authentication. Example shown here is for Windows Authentication. Then value of parameter @defdb is the database name such as ‘master’ or ‘AdventureWorks’ and it will change as per the need to set the default database for the particular login specified.

But this stored procedure seems soon to be obsolete as mentioned in MSDN link here. There is newer option available for this purpose – ALTER LOGIN which has many other usability apart from setting the default database but I would stick to the topic and show its usage.

ALTER LOGIN [Domain\UserName] WITH DEFAULT_DATABASE = [MyDefaultDatabaseName]

I hope you found this helpful and learnt something new from this.

Reference links: StackOverflow – How can I change my default database in SQL Server without using MS SQL Server Management Studio?

Keep your comments flowing for what you prefer. 🙂