Posts

Showing posts from December, 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 ,     ...