Category Archives: SQL Server

SQL Server Transact-SQL

get a substring in SQL SERVER using space as separator

get a substring in SQL SERVER using space as separator


SELECT CASE CHARINDEX(' ', news.description, 40)
  WHEN 0 THEN news.description
  ELSE SUBSTRING(news.description, 0, CHARINDEX(' ', news.description, 40) - 0)
  END
 AS FirstWord
FROM news

source:
http://www.dreamincode.net/forums/topic/239440-how-can-i-get-a-substring-in-sql-server-using-space-as-separator/
CHARINDEX (Transact-SQL) http://msdn.microsoft.com/pt-pt/library/ms186323.aspx

SUBSTRING (Transact-SQL)http://msdn.microsoft.com/en-us/library/ms187748.aspx

Microsoft SQL Server

How to enable remote Connection sql server 2008

Error message:

“A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)”

How to solve this issue?

There are a couple of things that might be going on here… (All of the following configurations are made on the computer running your SQL Server 2008 instance)

Allow remote connections to this server

The first thing you want to check is if Remote Connections are enabled on your SQL Server database. In SQL Server 2008 you do this by opening SQL Server 2008 Management Studio, connect to the server go to Properties -> Connections -> Check “Allow remote connections to this server”.

for further issues check  http://blogs.msdn.com/b/walzenbach

Microsoft SQL Server

Problem with Identity Specification in SQL…

Error message when changing the identity column to “Yes” and save the changes:

“Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can’t be re-created or enabled the option Prevent saving changes that require the table to be re-created.”

It’s not a bug. It’s a safety measure added to SQL 2008’s management studio to indicate that the change that you’ve requested to the table requires that the table be dropped and recreated.

It’s there so that people don’t ‘accidentally’ make changes in production that will take hours.

use “SET IDENTITY_INSERT” command, for more info, see SQL BOL.

Or turn off the warning under tools–>options–>designers–>table and database designers

uncheck prevent saving changes that require table re-creation

SQL Server Transact-SQL

SQL Server Dumper

SQL Server Dumper enables you to dump selected SQL Server database tables into SQL INSERT statements, that are saved as local .sql files and contain all the data required to create a duplicate table, or to be used for backup purposes. You can choose to create an individual .sql file for each table, or combine all selected tables into a single file.

This program implements the following special features:

  • Foreign Keys> order tables in text file in order to insert data without colisions/errors
  • Primary Key IDENTITY> guarantees the value of the IDENTITY field
  • Reference to itself> eliminates the constraint and at the end create it again

http://sqldumper.ruizata.com/

Microsoft SQL Server Transact-SQL

SQL SERVER – Insert Data From One Table to Another Table

INSERT INTO SELECT – SELECT INTO TABLE

Following three questions are many time asked on this blog.

How to insert data from one table to another table efficiently?
How to insert data from one table using where condition to anther table?
How can I stop using cursor to move data from one table to another table?

There are two different ways to implement inserting data from one table to another table. I strongly suggest to use either of the method over cursor. Performance of following two methods is far superior over cursor. I prefer to use Method 1 always as I works in all the case.

read more »

Microsoft SQL Server Tips & tricks Transact-SQL

SQL to Select a random row from a database table

Select a random row with MySQL:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

Select a random row with PostgreSQL:

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

Select a random row with Microsoft SQL Server:

SELECT TOP 1 column FROM table
ORDER BY NEWID()

Select a random row with IBM DB2

SELECT column, RAND() as IDX 
FROM table 
ORDER BY IDX FETCH FIRST 1 ROWS ONLY

Select a random record with Oracle:

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

source link: http://www.petefreitag.com/item/466.cfm
Microsoft SQL Server Transact-SQL

Insert Multiple Rows in SQL Server

In SQL Server 2005, in order to insert 3 rows to a table, you had to run 3 INSERT statements:

insert into Customers (Name, City, Phone) values (‘Customer #1’, ‘Jerusalem’, ‘2343245’)

insert into Customers (Name, City, Phone) values (‘Customer #2’, ‘Tel Aviv’, ‘0987345’)

insert into Customers (Name, City, Phone) values (‘Customer #3’, ‘Haifa’, ‘275466’)

In SQL Server 2008, you can insert multiple rows in a single insert statement that takes a number of value arrays:

insert into Customers (Name, City, Phone)

values

(‘Customer #1’, ‘Jerusalem’, ‘2343245’),

(‘Customer #2’, ‘Tel Aviv’, ‘0987345’),

(‘Customer #3’, ‘Haifa’, ‘275466’)

Source link

Microsoft SQL Server

A WMI error has occurred and no additional error information is available

This can happen when you have different versions installed. For example, if you install SQL Server 2005 SP3 and later, and only then install the Reporting Services.

In this situation it should be rerun after the SP3 installation of Reporting Services

SQL Server Transact-SQL

SQL collations

Query:

select * from fn_helpcollations()
where name like ‘%SQL_Latin1_General_CP1_CI_AS%’
or name = ‘Latin1_General_CI_AS’
or name like ‘%cp1%’

Query result:

read more »

Microsoft SQL SQL Server

Find the name of SQL Server

select serverproperty (‘MachineName’) as Servername

SELECT SERVERPROPERTY(‘ServerName’) AS ServerName

Select SERVERPROPERTY(‘ComputerNamePhysicalNetBIOS’) as PhysicalName

SELECT @@SERVERNAME AS ServerNameGlobalVar