Posts

Flatten in SQL Server

CREATE TYPE [ dbo ] . [ SingleColumnText ] AS TABLE ( [ TextValue ] NVARCHAR ( MAX ) NULL ) ; create function dbo . Flatten ( @input dbo . SingleColumnText readonly , @delimiter nvarchar ( max ) ) returns nvarchar ( max ) as begin return ( select stuff ( ( select @delimiter + TextValue from @input for xml path ( '' ) , type ) . value ( '(./text())[1]' , 'nvarchar(max)' ) , 1 , 1 , '' ) ) ; end ;

SQL Server Numbers table

if not exists ( select 1 from information_schema . schemata where schema_name = 'util' ) begin exec sp_executesql N 'create schema [util];' ; end ; go create table util . Numbers ( Num int not null , constraint PK_util_Numbers primary key ( Num ) ) ; go declare @MaxValue int = 1000000 ; while ( select count ( 1 ) from util . Numbers ) < @MaxValue begin declare @n int = @MaxValue - ( select count ( 1 ) from util . Numbers ) ; declare @offset int = isnull ( ( select max ( Num ) from util . Numbers ) , 0 ) ; insert util . Numbers select top ( @n ) row_number ( ) over ( order by o1 . [ object_id ] ) + @offset from sys . objects o1 cross join sys . objects o2 end ; select * from util . Numbers order by Num ; go

Median in .NET

dotnet add package MathNet.Numerics https://numerics.mathdotnet.com/ MathNet . Numerics . Statistics . Statistics . Median ( nums )

HTML to text

Install-Package NUglify public static IHtmlContent HtmlToText < T > ( this IHtmlHelper < T > html , string htmlText ) = > new StringHtmlContent ( Uglify . HtmlToText ( $ " <div>{htmlText}</div> " ) . ToString ( ) ) ;

Add view location

 In ASP.NET Core MVC, here’s how to add a new default location to find views: services . AddControllersWithViews ( ) . AddRazorOptions ( options = > options . ViewLocationFormats . Add ( " /Views/SpecialPlace/{0}.cshtml " ) )

Convert UTC to local time in SQL Server

 Thanks to this StackOverflow answer : convert ( datetime , switchoffset ( convert ( datetimeoffset , MyTable . UtcColumn ) , datename ( TzOffset , sysdatetimeoffset ( ) ) ) ) as ColumnInLocalTime