Tuesday, 23 September 2014

sql-update-query-using-joins

You don't quite have SQL Server's proprietary UPDATE FROM syntax down. Also not sure why you needed to join on the CommonField and also filter on it afterward. Try this:
UPDATE t1
  SET t1.CalculatedColumn = t2.[Calculated Column]
  FROM dbo.Table1 AS t1
  INNER JOIN dbo.Table2 AS t2
  ON t1.CommonField = t2.[Common Field]
  WHERE t1.BatchNo = '110';

Wednesday, 3 September 2014

CREATE SplitString FUNCTION IN SQL

/*
use the user define spit function
select * from fnSplitString('a,b,c,d,e',',')
*/
CREATE FUNCTION [dbo].[fnSplitString]  
(  
    @string NVARCHAR(MAX),  
    @delimiter CHAR(1)  
)  
RETURNS @output TABLE(splitdata NVARCHAR(MAX)  
)  
BEGIN  
    DECLARE @start INT, @end INT  
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)  
    WHILE @start < LEN(@string) + 1 BEGIN  
        IF @end = 0  
            SET @end = LEN(@string) + 1
       
        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start))  
        SET @start = @end + 1  
        SET @end = CHARINDEX(@delimiter, @string, @start)
         
    END  
    RETURN
END