Tuesday, 23 December 2014

How to split column value into 3 separate values.


How to split column value into 3 separate values.

    Using simple function you can achieve these

  Declare @column varchar(100) = 'AB12-23PP-tKK%IU'
  Select @column,
              PARSENAME(REPLACE(@column,'-','.'),3)'Second',
              PARSENAME(REPLACE(@column,'-','.'),2)'Third',
              PARSENAME(REPLACE(@column,'-','.'),1)'Fourth'

  If you want to create function

CREATE FUNCTION dbo.splitValue(@string VARCHAR(MAX), @delim CHAR(1))
RETURNS @result TABLE (id INT IDENTITY, value VARCHAR(MAX))
AS
BEGIN
 WHILE CHARINDEX(@delim,@string) > 0
  BEGIN
   INSERT INTO @result (value) VALUES (LEFT(@string,CHARINDEX(@delim,@string)-1))
   SET @string = RIGHT(@string,LEN(@string)-CHARINDEX(@delim,@string))
  END
   INSERT INTO @result (value) VALUES (@string)

RETURN
END
GO

DECLARE @table TABLE (value VARCHAR(20))
INSERT INTO @table VALUES('AB12-23PP-tKK%IU')
INSERT INTO @table VALUES('XYz-PrP-Hind')

SELECT *
  FROM (
SELECT t.*, x.id, x.value as stringValue
  FROM @table t
    CROSS APPLY dbo.splitValue(value,'-') x
       ) s
  PIVOT( max(stringValue) for id in ([1],[2],[3])) pv



Monday, 22 December 2014

How to search Special character in SQL SERVER


How can i search special character in column

  Create table test
  (
       ID int,
       ProductName varchar(100)
  )

  Insert into test values (1,'hey%boss')
  Insert into test values (1,'helloboss')
  Insert into test values (1,'hiboss')


  Select * From test
  Where ProductName like '%[%]%'


  Select * From test
  Where ProductName like '%\%%' ESCAPE '\'




How to find SQL Version



How to get SQL Server Version Information

SELECT @@SERVERNAME AS [Server Name],
       @@VERSION AS [SQL Server and OS Version Info];

How to check when sql server is installed:

SELECT  @@SERVERNAME AS [Server Name],
           create_date AS [SQL Server Install Date]
FROM    sys.server_principals WITH (NOLOCK)
WHERE   name = N'NT AUTHORITY\SYSTEM'
or      name = N'NT AUTHORITY\NETWORK SERVICE' OPTION (RECOMPILE);

How to get server properties

SELECT SERVERPROPERTY('MachineName') AS [MachineName],
              SERVERPROPERTY('ServerName') AS [ServerName], 
              SERVERPROPERTY('InstanceName') AS [Instance],
              SERVERPROPERTY('IsClustered') AS [IsClustered],
              SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS [ComputerNamePhysicalNetBIOS],
              SERVERPROPERTY('Edition') AS [Edition],
              SERVERPROPERTY('ProductLevel') AS [ProductLevel],
              SERVERPROPERTY('ProductVersion') AS [ProductVersion],
              SERVERPROPERTY('ProcessID') AS [ProcessID],
              SERVERPROPERTY('Collation') AS [Collation],
              SERVERPROPERTY('IsFullTextInstalled') AS [IsFullTextInstalled],
              SERVERPROPERTY('IsIntegratedSecurityOnly') AS [IsIntegratedSecurityOnly];


How to create proxy account

How to create SQL Server Agent proxy

A SQL Server Agent proxy account defines a security context in which a job step can run. Each proxy corresponds to a security credential. To set permissions for a particular job step, create a proxy that has the required permissions for a SQL Server Agent subsystem, and then assign that proxy to the job step

You must create a credential before you create a proxy if one is not already available.

To create credential, Right click on Credentials







Give Credential Name – New Credential
Identity – Domain Name\Username (Windows username)
Password – Windows password
Confirm password and click OK.
So now credentials has been created

Now let’s create proxy account,


Right Click on Proxies and select New proxy



Give Proxy Name(as your choice)
Credential Name: select the credentials you just created your (Domain Name\Username)
From Active to the following subsystems
Selection options (SQL Server Integration service package) if you want to run package
Or Choose appropriate one based on your requirement. Click ok

Now if you want to run Agent job using proxy account, choose RUN AS proxy you created

see below image.