Monday, 26 November 2018

MSSQL - Max function in SQL server that takes two values like Math.Max in .NET


Watch this example on YouTube




1. Table

CREATE TABLE [dbo].[NumberTest](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Num1] [int] NULL,
    [Num2] [nchar](10) NULL,
 CONSTRAINT [PK_NumberTest] PRIMARY KEY CLUSTERED
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

2. Query 1

(Select Max(val) From (Values (1002), (20)) as res(val))

3. Query 2

Select m.ID,
(Select Max(val) From (Values (m.Num1),(m.Num2)) as res(val)) From NumberTest m

No comments:

Post a Comment