Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function in SQL Server 2008 similar to GREATEST in mysql?

I want to find the maximum value of multiple columns.

MySQL supports the GREATEST function but SQL Server doesn't.

Is there any function similar to this in SQL Server 2008?

like image 870
Hset Hset Aung Avatar asked Jan 18 '11 15:01

Hset Hset Aung


People also ask

Are MySQL and SQL Server similar?

No, MySQL is not the same as SQL server. Both of these are relational database management systems offered by different vendors. They differ in terms of use cases, licensing, pricing, features, pros, cons, etc. MySQL is offered through Oracle and SQL Server is offered through Microsoft corporation.

What is SQL greatest function?

The GREATEST() function returns the greatest value of the list of arguments.

Which is best MySQL or SQL Server?

In terms of data security, the SQL server is much more secure than the MySQL server. In SQL, external processes (like third-party apps) cannot access or manipulate the data directly. While in MySQL, one can easily manipulate or modify the database files during run time using binaries.

What is least and greatest in SQL?

The Oracle GREATEST function returns the “greatest” or largest value in a set of values that you provide to it. The Oracle LEAST function returns the “least” or smallest value in a set of values that you provide to it, and it's the opposite of the GREATEST function.


1 Answers

Not in SQL Server 2008 but these functions are/will be available finally in SQL Server (presumably SQL Server 2022)

the GREATEST and LEAST T-SQL functions are now generally available in Azure SQL Database, as well as in Azure Synapse Analytics (serverless SQL pools only) and Azure SQL Managed Instance.

The functions will also be available in upcoming releases of SQL Server.

For previous versions you can use the fact a sub query can access the columns from the outer query so you can add a sub query Selecting the max from a union of those.

SELECT *, 
      (SELECT MAX(c) FROM (VALUES(number),(status)) T (c)) AS Greatest
FROM master..spt_values

Or for SQL Server 2000/2005

SELECT *, 
      (SELECT MAX(c) FROM 
                    (SELECT number AS c 
                     UNION ALL 
                     SELECT status) T) AS GreatestNumberOrStatus
FROM master..spt_values
like image 171
Martin Smith Avatar answered Sep 21 '22 17:09

Martin Smith