Posts

Showing posts from December, 2013

To get All dates, Months, quarters and year with in two dates

Use the query below to get all the dates, months, quarters and years with in two dates. if object_id('tempdb..#Calendar_Test')is not null drop table #Calendar_Test declare @Date_Start datetime, @Date_End datetime select @Date_Start = '2000-01-01 00:00:00.000' select @Date_End = '2020-12-31 00:00:00.000'; with CTE_Dates as (     select @Date_Start as [Date]     union all     select dateadd(dd, 1, [Date])     from CTE_Dates     where [Date] < @Date_End ), CTE_Calendar as (     select         [Date], datename(dd, [Date]) as [Day_ID],         datename(dw, [Date]) as [Day],         datepart(ww, [Date]) as [Week],         datepart(mm, [Date]) as [MonthID],         datepart(yy, [Date]) as [YearID],         datepart(qq, [Date]) as [QuarterID]      from CTE_Dates ) se...

To add hyphen sign or any other sign between a varchar value

If you want to add a  hyphen or any other sign in between a varchar column then you can you Stuff built in function of sql server. syntax is declare @test varchar(max) = '1234567890' SELECT STUFF(STUFF(@test,4,0,'-'),8,0,'-') the output of the following sql statement is "123-456-7890" 4,0 and 8,0 in stuff function means, 4 means insert hyphen at 4th position 0 means replace characters with  hyphen .In this case its 0 so it will not replace any character just insert  hyphen sign. Same as the case with 8,0