0

Trying to figure out how to do a query to give me the results in a desired way:

Table Cars:

car_id  Make    Model   Color
1   Ford    Focus   Red
2   Ford    Fiesta  Silver
3   Honda   Accord  Silver

Select from cars where car_id = 1

Car_id  1
Make    Ford
Model   Focus
Color   Red

So pretty much taking a column and converting it into a row. Using SQL 2005 (which I am new tat using) as a database.

Thanks

1
  • Format text as code where you need line breaks preserved. Commented Sep 20, 2011 at 22:30

4 Answers 4

2

Here is an easy way:

;with a as
(
  select id, make, model, color
  from cars
  where id = 1
)
select 'Car_id' [Col1], cast(Car_id as varchar(10)) [Col2]
from a
union all
select 'make', make
from a
union all
select 'Model', Model
from a
union all
select 'Color', Color
from a
Sign up to request clarification or add additional context in comments.

1 Comment

I was getting this error message "Conversion failed when converting the varchar value 'Ford' to data type int.", and figured it was making the first column type int. Casting car_id as varchar got it to work for me. Thanks Now to see if I can get this to work while attmepting to join three tables together.
1

Its simplier than you think, you can concatenate those columns into one column, that one column's value becomes the resulting row:

SELECT CarModel + ' ' + CarMake + ' ' + CarColor AS MyColumn FROM MyTable

5 Comments

That works if I want a single row returned, but I want a row for each of those columns. So where I would had one row if I filtered by car_id = 1 I actually want to get 4 rows back. Hope that makes sense.
@Wayne In ML - This doesn't make sense, my solution would work on each current row. Assume you have 4 cars in the table, you would get 4 rows with the car model the car make and the color, for each car.
I tired your query using from table where car_id = 1. I got exactly one row returned. Create table #cars( car_id int, make varchar(10), model varchar(10), color varchar(10) ) insert into #cars values(1,'Ford','Focus','Red') insert into #cars values(2,'Ford','Fiesta','Silver') insert into #cars values(3,'Honda','Accord','Silver') SELECT Model + ' ' + Make + ' ' + Color AS MyColumn FROM #cars where car_id = 1 DROP table #cars
Well the formatting doesn't come across.. But if you take my comment start at Creat etable and copying to the end, pop it into a query window and run it, you'll get one row returned, with the three field combined into that one row.
Just to make it clear, I hope, when I run the query for car_id = 1, I would get one row with two columsn, Make and Ford, a second row with two columns, Model and Fiesta, and a last row with two columns, Color and Red.
1

we can write generic solution for any query.

--sample table
SELECT * INTO #cars
FROM (  SELECT 1 car_id, 'Ford' Make, 'Focus' Model,'Red' Color
    UNION all
    SELECT 2 car_id, 'Ford' Make, 'Fiesta' Model,'Silver' Color
    UNION ALL 
    SELECT 3 car_id, 'Honda' Make, 'Accord' Model,'Silver' Color
    ) x

--selected record to #tmp
SELECT * INTO #tmp FROM #cars WHERE car_id=1

--generic solution
DECLARE @sql VARCHAR(max)
SET @sql=''
SELECT @sql=@sql + 'SELECT '''+Name+''' as ColumnName,
    cast(['+Name+'] as varchar(500)) as Value FROM #tmp union all ' 
FROM tempdb.sys.columns 
where  object_id=object_id('tempdb..#tmp')

SET @sql = LEFT(@sql,LEN(@sql)-9)  -- except last 'union all'

EXEC( @sql)

DROP TABLE #tmp,#cars

1 Comment

Welcome to stack overflow. You can format code by putting four spaces before code lines and the engine will take care of the rest. I edited it for you but have to ask... did you really try to put your answer inside a BLINK tag??? That's funny right there.
0

how about

declare @mytable table(car_id int, Make varchar(10), model varchar(10), color varchar(10))

insert @mytable values(1,'Ford','Focus','Red')
insert @mytable values(2,'Ford','Fiesta','Silver')
insert @mytable values(3,'Honda','Accord','Silver')

select 'Car_id ' +convert(varchar, car_id)+char(10)+char(13)+ 
'Make ' + Make + char(10)+char(13)+ 
'Color '+color 
from @mytable
where car_id = 1

1 Comment

Thanks, but gives me the same results as the first suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.