Skip to content

Instantly share code, notes, and snippets.

@RickyLin
RickyLin / CreatePrimaryKeyOnTableNameIdColumns.sql
Last active September 22, 2020 03:06
Create Primary Key on TableNameId Columns
SELECT T.TABLE_NAME, C.COLUMN_NAME, C.DATA_TYPE, C.IS_NULLABLE
INTO #Tables
FROM INFORMATION_SCHEMA.TABLES T
INNER JOIN (
SELECT TABLE_NAME, COLUMN_NAME, IS_NULLABLE, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = TABLE_NAME + 'Id'
) C ON C.TABLE_NAME = T.TABLE_NAME
WHERE T.TABLE_TYPE = 'BASE TABLE'
AND T.TABLE_NAME NOT IN (
@RickyLin
RickyLin / IQueryableExtension.cs
Last active June 28, 2022 07:25
Extension methods on Queryable/IQueryable
using System;
using System.Linq.Expressions;
using System.Linq;
using System.Collections.Generic;
namespace Rvc.Utilities
{
// refer to System.Web.Query.Dynamic namespace
public static class IQueryableExtensions
{
@RickyLin
RickyLin / AspNetWebFormValidatorEnable.js
Created February 7, 2021 05:35
Enable/Disable asp.net web form validator control on client side.
var rfv = document.getElementById('validator_control_id');
ValidatorEnable(rfv, true);
ValidatorEnable(rfv, false);
@RickyLin
RickyLin / ExtractValuesFromXML.sql
Created February 24, 2021 02:04
Extract values from XML type in SQL Server
DECLARE @Data XML
SET @Data = '<Ids>
    <Id>64122</Id>
    <Id>12345</Id>
    <Id>67890</Id>
</Ids>'
SELECT Ids.Id.value('text()[1]', 'varchar(100)')
FROM @Data.nodes('Ids/Id') AS Ids(Id)
@RickyLin
RickyLin / Startup.cs
Created March 23, 2021 03:01
The token validation in project using Identity Server 3 is not compatible with token created in Identity Server 4. So I have to tweak the token process in Project using Identity Server 4 for client and scopes that will access the project using Identity Server 3.
/*
add those 2 services in Startup class of Identity Server 4 project.
services.AddTransient<ITokenService, TokenService>()
.AddTransient<ITokenCreationService, TokenCreationService>();
*/
@RickyLin
RickyLin / Startup.cs
Created April 17, 2021 08:40
Access token management for HttpClient
// add IdentityModel.AspNetCore package
using Microsoft.Extensions.DependencyInjection;
using IdentityModel.Client;
public class Startup
{
/* removed all other code for brevity */
public void ConfigureServices(IServiceCollection services)
{
@RickyLin
RickyLin / fnGetDateRange.sql
Last active April 17, 2021 08:46
Get date range of a week, a month or a year that contains a date.
CREATE FUNCTION [dbo].[fnGetDateRange]
(
@Now DATETIME,
@FrequencyId INT
)
RETURNS @Result TABLE
(
StartDate DATE,
EndDate DATE
)
@RickyLin
RickyLin / spDuplicateEntryWithIdentityValue.sql
Created April 17, 2021 08:52
Generate and run "insert into ... select ... from" statement for a table.
CREATE PROCEDURE [dbo].[spDuplicateEntryWithIdentityValue]
@TableName NVARCHAR(128),
@IdentityColumnName NVARCHAR(128), -- the name of auto-increasing column
@CurrentIdentityValue INT,
@NewIdentityValue INT OUTPUT
AS
DECLARE @ColumnNames NVARCHAR(MAX)
SELECT @ColumnNames = STRING_AGG(N'[' + COLUMN_NAME + N']', N', ')
FROM INFORMATION_SCHEMA.COLUMNS
@RickyLin
RickyLin / GetDefaultConstraintName.sql
Created August 30, 2021 08:19
Get default constraint name of a column
SELECT T.name AS TableName, A.name AS ColumnName, d.name AS ConstraintName
FROM sys.default_constraints D
INNER JOIN sys.all_columns A ON A.default_object_id = D.object_id
INNER JOIN sys.tables T ON T.object_id = A.object_id
WHERE T.name = @TableName AND A.name = @ColumnName
@RickyLin
RickyLin / HtmlEncodeDecode.js
Created November 22, 2021 12:27
HtmlEncode and HtmlDecode in Javascript
/* source: https://stackoverflow.com/a/7124052/2753545 */
function htmlEscape(str) {
return str
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/\//g, '&#x2F;');