Archive for the ‘MS SQL Server’ Category

How to check if a varchar (string) column can be converted to numeric

Use the ISNUMERIC function of the sql server to this job.

To check all records of that particular column if it contains a string or can be converted to numeric use the script below.

select
case when sum(
case when COLUMN_NAME is null then 1
when ltrim(rtrim(COLUMN_NAME)) = ” then 1
else isnumeric(COLUMN_NAME)
end) = count(*) then 1 else 0 end x from TABLENAME

You can find more info here

, , ,

No Comments


How to email result query from SQL Server

To send the result via email in sql server use the sp_send_dbmail procedures but before you execute that sp you need to enable the Database Mail using the Configuration Wizard or use the sp_configure

Example:

EXEC msdb.dbo.sp_send_dbmail
@profile_name = ‘MyProfilename’,
@recipients = ‘myemail@gmail.com’,
@query =’SELECT * FROM tblname,
@body = ‘Description.’,
@subject = ‘Subject’ ;

Read More

, ,

No Comments


How to check the SQL Server Version

You can check the sql server version by using the SERVERPROPERTY function.

Exampe:

SELECT
SERVERPROPERTY(‘ProductVersion’) AS ProductVersion,
SERVERPROPERTY(‘ProductLevel’) AS ProductLevel,
SERVERPROPERTY(‘Edition’) AS Edition,
SERVERPROPERTY(‘EngineEdition’) AS EngineEdition;
GO

Read More…

, ,

No Comments


Difference between Clustered and Non-Clustered Index
  • Clustered Index
    1. A clustered index is a special type of index that reorders the way records in the table are physically stored.
    2. Therefore table can have only one clustered index. The leaf nodes of a clustered index contain the data pages.
  • Non-Clustered Index
    1. A nonclustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk.
    2. The leaf node of a nonclustered index does not consist of the data pages. Instead, the leaf nodes contain index rows.

source

, , , ,

No Comments


Rebuild Index Script for SQL Server

DECLARE @tblName VARCHAR(50)

DECLARE rs CURSOR FOR
SELECT TABLE_NAME FROM information_schema.tables WHERE [TABLE_TYPE] =N’BASE TABLE’

OPEN rs
FETCH NEXT FROM rs INTO @tblName

WHILE @@FETCH_STATUS = 0
BEGIN
DBCC DBREINDEX( @tblName,”,90)
END

CLOSE rs
DEALLOCATE rs

, ,

No Comments


http://www.banmanpro.com/support/filegroupfull.asp

Try to shrink / back up the database.

Solution:
1. Check if the autogrowth is enable
2. Check if the autoshrink is checked

or
Goto forums
or
Article on how to setup database

No Comments


How to change the table owner in Sql Server

Use the sp_changeobjectowner stored procedure.

Synstax:

sp_changeobjectowner ‘OldUser.tableName’, ‘NewUser’

Example

In Domain User

  1. sp_changeobjectowner '[Domain\Username].[tblTableProperty]', 'dbo'

Local User

  1. sp_changeobjectowner '[Username].[tblTableProperty]', 'dbo'

, ,

No Comments


How to add a Line Break in T-SQL Query

char(13) + char(10)

example:

<pre lang=”vb” >

PRINT ‘LINE ONE ‘ + char(13) + char(10) + ‘LINE TWO’

</pre>

Read Forum

,

No Comments


Sql Server 2008 Management Studio Express Installation

You have to download and install the following before installing SQL SERVER 2008 Management Studio Express

  1. 1. <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=AB99342F-5D1A-413D-8319-81DA479AB0D7&displaylang=en">Microsoft .Net Framework 3.5 SP1</a>
  2. 2. <a href="http://support.microsoft.com/kb/942288">Windows Installer 4.5</a>
  3. 3. <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx">Windows PowerShell 1.0</a>

,

No Comments


How to Compute Distance Between Two Points in SQL Server
  1. CREATEfunction [dbo].[computedistance](@lat1 FLOAT, @lat2 FLOAT,@lng1 FLOAT,@lng2 FLOAT)
  2. RETURNS FLOAT
  3. AS
  4. BEGIN
  5.  DECLARE @theta FLOAT
  6.  DECLARE @dist FLOAT
  7.  DECLARE @miles FLOAT
  8.  DECLARE @unit char(1)
  9.  
  10.  SET @Unit = 'K'
  11.  set @theta = @lng1 - @lng2
  12.  set @dist = (180/PI())*(
  13.       ACOS(
  14.        ( SIN(( (PI()/180)*@lat1)) * SIN(( (PI()/180)*@lat2)) ) + degree to radiAn 'SIN(DEGREES(PI/@lat1))'
  15.        ( COS(( (PI()/180)*@lat1)) * COS(( (PI()/180)*@lat2)) * COS(( (PI()/180)*@theta)) )
  16.        )
  17.       )
  18.  
  19.  SET @miles = (@dist * 60 * 1.1515) convert to miles
  20.  
  21.  SET @miles = @miles * 1.609344 convert to k
  22.  
  23.  RETURN @miles
  24. END

, , ,

No Comments



SetPageWidth