Intan Nur Shafienaz
Intan Nur Shafienaz

Reputation: 69

SQL Server : How to place dashes sign ( - ) between first and last record

Here I have query for select the first and last query.

SELECT MAX(invoiceNo) AS MaxInvoiceNo, MIN(invoiceNo) AS MinInvoiceNo FROM JobInvoice

My question is how I'm gonna fix this to put dash ( - ) between that MAX(invoiceNo) and MIN(invoiceNo). Just like MAX(invoiceNo)- MIN(invoiceNo)

Upvotes: 0

Views: 88

Answers (2)

Intan Nur Shafienaz
Intan Nur Shafienaz

Reputation: 69

It work from Gordon's help. Here I add a bit.

SELECT RTRIM(MAX(invoiceNo)) + ' - ' + MIN(invoiceNo) AS InvoiceRange FROM JobInvoice

Output like

enter image description here

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269643

You can just concatenate them. If the values are strings:

SELECT LTRIM(RTRIM(MAX(invoiceNo))) + '-' + LTRIM(RTRIM(MIN(invoiceNo))) AS InvoiceRange
FROM JobInvoice;

If not, you need to cast them:

SELECT CONVERT(VARCHAR(255), MAX(invoiceNo)) + '-' + CONVERT(VARCHAR(255), MIN(invoiceNo)) AS InvoiceRange
FROM JobInvoice;

Upvotes: 1

Related Questions