Tuesday, 1 July 2014

Generate Time sheet file

Declare @StartDate as Datetime SET @StartDate='2014-06-26' Declare @EndDate as Datetime SET @EndDate='2014-07-26' Declare @YearMonth as varchar(6) set @YearMonth='201407' Declare @CurrDate as Datetime SET @CurrDate=@StartDate Declare @ct as int set @ct=1; Declare @dcnt as int set @dcnt=0 while (@startDate<@EndDate) begin if (datepart(dw,@startdate)<> 1) and( datepart(dw,@startdate)<>7) begin if @dcnt=0 begin set @CurrDate=@startdate end set @dcnt=@dcnt+1 end if (@dcnt=5) or (@startDate=dateadd(dd,-1,@EndDate)) begin insert into Dest SELECT 'User Name','','AVMI0'+ CAST(@ct AS VARCHAR(2))+ @YearMonth,'task'+CAST(@ct AS VARCHAR(2)),@startdate,@CurrDate,@dcnt,@dcnt*8,'','' As [Resolution],DATEADD(s, 2, @CurrDate),@startdate,@dcnt,@dcnt*8,'','' As [Created By],@CurrDate,'Closed(16)',DATEADD(s, 1, @CurrDate),DATEADD(s, 2, @CurrDate),'','','','','','','','','','','','','',@startdate,'','','','','','','','','','','','','','' set @ct=@ct+1 set @dcnt=0 end SET @startdate=dateadd(dd,1,@startdate) end go select * from Dest

Friday, 27 June 2014

Sql query based on Substring,Charindex functions

Hi Guys I have one question on a sql query if you know any one, if they can help to write a query. My question is if there is a string example1 "450 lbs weight capacity" example 2 "10-40 weight capacity" the result should be displayed for the first example minimum value maximum value weight 0 450 lbs Second example minimum value maximum value weight 10 40 lbs The results should be displayed in this way if you know some one please help me . --------------------------------------------------------------------------------------------
Solution
create table #temp(test varchar(2000)) insert into #temp values('450 lbs weight capacity')
insert into #temp values('10-40 weight capacity')
insert into #temp values('560-600 lbs weight capacity')
insert into #temp values('')
insert into #temp values(null)
insert into #temp values('ab')
--select * from #temp
select *,replace(substring(test,0,charindex('weight',test)),'lbs','') as value ,CASE WHEN CHARINDEX('-',replace(substring(test,0,charindex('weight',test)),'lbs',''))=0 THEN 0 ELSE SUBSTRING(replace(substring(test,0,charindex('weight',test)),'lbs',''),0,CHARINDEX('-',replace(substring(test,0,charindex('weight',test)), 'lbs',''))) END as minimum ,CASE WHEN CHARINDEX('-',replace(substring(test,0,charindex('weight',test)),'lbs',''))=0 THEN replace(substring(test,0,charindex('weight',test)), 'lbs','') ELSE SUBSTRING(replace(substring(test,0,charindex('weight',test)),'lbs',''),CHARINDEX('-',replace(substring(test,0,charindex('weight',test)), 'lbs',''))+1,LEN(replace(substring(test,0,charindex('weight',test)),'lbs',''))) END as maximum, 'lbs' as weightq from #temp