Posts

Showing posts from 2020

SQL Tricks That You Didn’t Think Were Possible

SQL is one of the only ever Successful, mainstream, and general-purpose 4GL(Fourth-Generation programming Language)   From early days onwards, programming language designers had this desire to design languages in which you tell the machine WHAT you want as a result, not HOW to obtain it. For instance, in SQL, you tell the machine that you want to “connect” (JOIN) the user table and the address table and find the users that live in Ghana. You don’t care    HOW the database will retrieve this information (e.g. should the users table be loaded first, or the address table? Should the two tables be joined in a nested loop or any other method? Should all data be loaded in memory first and then filtered for Ghanaian users, or should we only load Ghanaian addresses in the first place? EVERYTHING IS A TABLE •         This is the most trivial of tricks, and not even really a trick, but it is fundamental to a thorough understanding of SQL...

Convert Number to Word in Sql Server

Prerequisite  – SQL introduction In SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a number and the task is to convert each digit of the number into words. Approach  is to select the corresponding word of a particular number using FnNumtoWords function. Below is the required implementation: Function 1 IF OBJECT_ID('FnConvertDigit') IS NOT NULL DROP FUNCTION FnConvertDigit GO CREATE Function [dbo].[FnConvertDigit](@decNumber decimal) RETURNS VARCHAR(6) AS BEGIN DECLARE @strWords VARCHAR(6) SELECT @strWords = CASE @decNumber WHEN '1' THEN 'One' WHEN '2' THEN 'Two' WHEN '3' THEN 'Three' WHEN '4' THEN 'Four' WHEN '5' THEN 'Five' WHEN '6' THEN 'Six' WHEN '7' THEN 'Seven' WHEN '8...