Jengz
Jengz

Reputation: 15

How to combine 2 select statements and output of two columns from one table?

i'm newbie on Sqlserver Here is my table.

 req
 61-01-0001
 61-02-0002
 61-03-0004
 61-04-0005

And this is my sql query.

SELECT MAX(RIGHT(req,4))as rq FROM tbl_inv_req  
UNION  
SELECT Substring(req,4,2) as rs  FROM tbl_inv_req

here The output

 rq
 0001
 0002
 0004
 0005

I want the output to look like.

 rq       rs
----     ----
 0001     01
 0002     02
 0004     03
 0005     04

pls tell me what i miss in my sql query. Thanks in advance for the help as usual.

Upvotes: 0

Views: 356

Answers (2)

neer
neer

Reputation: 4082

There is no need to use a UNION. You can select as follows.

SELECT 
    RIGHT(req,4)as rq,
    Substring(req_id,4,2) as rs 
FROM 
    tbl_inv_req 

UNION combines the results of two or more queries into a single result.

For comment:

SELECT 
    RIGHT(req,4) as rq,
    Substring(req_id,4,2) as rs 
FROM 
    tbl_inv_req 
WHERE
    RIGHT(req,4) = '0005' AND
    Substring(req_id,4,2) = '04'

Upvotes: 1

WL.T
WL.T

Reputation: 248

SELECT
 MAX(RIGHT(req, 4)) AS rq,
 SUBSTRING(req_id, 4, 2) AS rs
FROM tbl_inv_req;

Upvotes: 3

Related Questions