Posts

Find 3rd Highest Salary

---------Using Sub Query 3rd Highest Salary All Employees--------- SELECT * FROM Emp WHERE Salary IN( SELECT TOP 1   salary FROM ( SELECT TOP 3 salary FROM Emp e ORDER BY Salary DESC ) a ORDER BY a.Salary ) ------------------- 3rd highest salary----------- -----3rd Hightest salary---------- DECLARE @n INT=3 SELECT DISTINCT(Salary) from emp e1 where @n=( SELECT COUNT(DISTINCT(salary))  from emp e2 where  e2.salary>=e1.salary ) --------------3rd Highest Salary-----------  ;WITH cte AS  (      SELECT Id,Name,e.Salary, DENSE_RANK() OVER (PARTITION BY e.Salary ORDER BY e.Salary) AS sal      FROM Emp e  )  SELECT *  FROM cte WHERE sal=2 ----------------------- DECLARE @N int SET @N = 3  -- Change the value here to pick a different salary rank SELECT Salary FROM (     SELECT row_number() OVER (ORDER BY Salary DESC) as SalaryRank, Salary     FROM Emp e ) as SalaryCTE WHERE...

Convert Linq to DataTable

         var query = (from employees in _dsSecurity.Employees.AsEnumerable()                                  join empSites in _dtEmployeesSite                                      on employees.EmployeeId equals empSites.EmployeeId                                  join empFilter in _dtScheduleProviders.AsEnumerable()                       ...
ALTER FUNCTION [dbo].[ConvertToClientLocalTime] (     -- Add the parameters for the function here      @Time DATETIME     ,@TimeZone NVARCHAR(100) ) RETURNS DATETIME AS BEGIN     -- Declare the return variable here     DECLARE @LocalDate DATETIME     SELECT         @LocalDate =             CASE WHEN @Time IS NULL OR @Time = '' THEN NULL                  ELSE dbo.ConvertToClientTime(@Time, @TimeZone)             END     -- Return the result of the function     RETURN @LocalDate; END ------------------------------Below function called inside above----- ALTER FUNCTION [dbo].[ConvertToClientTime](@time [datetime], @timeZone [nvarchar](100)) RETURNS [datetime] --WITH EXECUTE AS CALLER ...

StringToNumeric

ALTER FUNCTION [dbo].[ConvertStringToNumeric] (@Value text) RETURNS NUMERIC(12,2) AS     BEGIN                 declare @string varchar(max)             SET @string = CONVERT(VARCHAR(MAX), @Value)             --set @string='abc93.3ad4'             DECLARE @IncorrectCharLoc SMALLINT             SET @IncorrectCharLoc = PATINDEX('%[^0-9.-]%', @string)             WHILE @IncorrectCharLoc > 0             BEGIN             SET @string = STUFF(@string, @IncorrectCharLoc, 1, '')             SET @IncorrectCharLoc = PATINDEX('%[^0-9.-]%', @string)     ...

ConvertLocalToUTC

ALTER FUNCTION [dbo].[ConvertLocalToUTC](@time [datetime],@timeZoneAdj  Int, @IsDayLightSavings Bit) RETURNS [datetime] --WITH EXECUTE AS CALLER AS BEGIN --EXTERNAL NAME [Mmt.Cyramed.Cryptography].[Mmt.Cyramed.Cryptography.Cryptography].[ConvertLocalToUTC] RETURN dbo.[GetPracticeUTCDateTime](@time,@timeZoneAdj,@IsDayLightSavings) END --------------Below function is called inside above----------- ALTER  FUNCTION  [dbo].[GetPracticeUTCDateTime] (     @InLocaleDateTime DATETIME,     @TimeZoneAdj INT,     @IsDayLightSaving BIT ) RETURNS DateTime AS BEGIN     DECLARE @OutLocalDateTime DATETIME            set @outLocalDateTime = dateadd(MINUTE , -1 * @TimeZoneAdj, @inLocaleDateTime)         if (@IsDayLightSaving = 1) -- figure in daylight savings time        BEGIN        declare @yearT...

Split

ALTER FUNCTION [dbo].[Split](@String nvarchar(max), @Delimiter char(1))            returns @temptable TABLE (items nvarchar(max))   as        begin                declare @index int                declare @slice varchar(8000)                           select @index = 1                    if len(@String)                    while @index!= 0                begin                ...

SplitWordList

ALTER function [dbo].[SplitWordList] (  @list varchar(MAX) ) returns @t table  (  Word varchar(50) not null,  Position int identity(1,1) not null ) as begin   declare      @pos int,     @lpos int,     @item varchar(100),     @ignore varchar(100),     @dl int,     @a1 int,     @a2 int,     @z1 int,     @z2 int,     @n1 int,     @n2 int, @n3 int,   @c varchar(1),     @a smallint   select      @a1 = ascii('a'),     @a2 = ascii('A'),     @z1 = ascii('z'),     @z2 = ascii('Z'),     @n1 = ascii('0'),     @n2 = ascii('9'), @n3 = ascii('-') set @ignore = '''"'   set @pos ...