Posts

Showing posts from 2014

To get a specific string from a string column

First you need to get the starting position of your string by CharIndex like: CHARINDEX('https:',FeedBody,1) Then you need to get the ending position of your string like: CHARINDEX('?AWS',FeedBody,1) Now you  have to Use SUBSTRING function to find your desired string by giving the starting and ending point in it which you get from CHARINDEX. like: select SUBSTRING(FeedBody,CHARINDEX('https:',FeedBody,1),CHARINDEX('?AWS',FeedBody,1)-CHARINDEX('https:',FeedBody,1))  from  feeds

To join a table with function split to get values of a column from that table

SQL: SELECT DISTINCT item AS Alltags FROM    dbo.Feeds CROSS APPLY dbo.fn_split(Tags, ',')

In 1:M relationship get single (record) instead of many records from MANY table

If you have two table caring 1 to many relationship and you want to get records with joins MANY table with only single/top most records then you have to get MANY tables records along with ROW NUMBER. then you can join this with main table with AND condition ROW NUMBER =1 for example: we have two table Inspection and Inspection_Details. One inspection can have many details but one detail is only of one inspection. So if we want to get only one inspection with only top/one inspection detail then the query will be, WITH    INS           AS ( SELECT   ROW_NUMBER() OVER ( PARTITION BY ID.InspectionId ORDER BY ID.InspectionDetailId DESC ) AS RN ,                         ID.*                FROM     dbo.Inspection_Details ID              )     SELECT  i.InspectionId ,     ...

Search for All the Stored Procedures Name that have a specific column

-- For All Objects SELECT  OBJECT_NAME(object_id) ,         definition FROM    sys.sql_modules WHERE   definition LIKE '%' + 'PhaseID' + '%' -- For Stored procedures only SELECT DISTINCT         OBJECT_NAME(object_id) ,         OBJECT_DEFINITION(object_id) FROM    sys.procedures WHERE   OBJECT_DEFINITION(object_id) LIKE '%' + 'PhaseID' + '%'

Enable CLR on a specific database

To enable clr on a specific database run the blow script on that database. sp_configure 'clr enabled',1 GO RECONFIGURE GO sp_configure 'clr enabled'  -- make sure it took GO

Reset Password of 'sa' account

Use command prompt is also an easy way to reset lost sa password. See the commands below: Method 3: Use Command Prompt to reset your lost SQL sa password Step 1. Open a command prompt (Start -> Run -> cmd) Step 2. Type the follow commands, and press Enter after each line:       Osql -S yourservername -E       1> EXEC sp_password NULL, 'yourpassword', 'sa'       2> GO Of course, you can reset your other sql account password, just by changing the 'sa' to your another sql account name.

SP_Who3

Copied Source:  http://sqlserverplanet.com/dba/a-better-sp_who2-using-dmvs-sp_who3 The following code generates the same information found in sp_who2, along with some additional troubleshooting information. It also contains the SQL Statement being run, so instead of having to execute a separate DBCC INPUTBUFFER, the statement being executed is shown in the results. Unlike sp_who2, this custom sp_who3 only shows sessions that have a current executing request. What is also shown is the reads and writes for the current command, along with the number of reads and writes for the entire SPID. It also shows the protocol being used (TCP, NamedPipes, or Shared Memory). The lead blocker below will show in the BlkBy column as -1. CREATE   PROCEDURE   [ dbo ] . [ sp_who3 ]   AS BEGIN SET   TRANSACTION  ISOLATION LEVEL  READ  UNCOMMITTED SELECT     SPID                 =  er . session_id ...