Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 convert nvarchar to table field name

Author  Topic 

nmarks
Yak Posting Veteran

53 Posts

Posted - 2007-10-02 : 06:18:23
Hi,

Is it possible to convert an nvarchar type to a field name type?

For example if I have a nvarchar called 'Sex' set to 'Male' and want a SELECT statement get data from the [Male] column in a table how can I convert the 'Sex' nvarchar to a field name type?

Is that possible?

I know CONVERT and CAST exist but I'm not sure how to use them.

Help very much appreciated.








SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-02 : 06:24:56
SELECT SUM(CASE WHEN Sex = 'Male' THEN 1 ELSE 0 END) AS Male,
SUM(CASE WHEN Sex = 'Female' THEN 1 ELSE 0 END) AS Female
FROM Table1



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-02 : 07:49:32
"SELECT statement get data from the [Male] column in a table how can I convert the 'Sex' nvarchar to a field name type?"

You have a column called [Sex]

Do you also have a column called [Male]? In a different table?

Or is "Male" just a value in the [Sex] column?

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-03 : 03:13:03
quote:
Originally posted by nmarks

Hi,

Is it possible to convert an nvarchar type to a field name type?

For example if I have a nvarchar called 'Sex' set to 'Male' and want a SELECT statement get data from the [Male] column in a table how can I convert the 'Sex' nvarchar to a field name type?

Is that possible?

I know CONVERT and CAST exist but I'm not sure how to use them.

Help very much appreciated.











Question seems unclear
Post table structure, sample data and expected result

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -