all groups > sql server programming > september 2006 >
You're in the

sql server programming

group:

PICKLIST IN SQL


PICKLIST IN SQL Ridhima Sood
9/14/2006 9:13:02 PM
sql server programming:
hi

I am keen to know the sql command to display picklist values rather than
unique numbers.

for example, title is 1=mr and 2 = mrs
if i say select title from contact sql returns 1 and 2 but i want it to
Re: PICKLIST IN SQL Warren Brunk
9/14/2006 9:25:28 PM
You could you a case statement...


select CASE
When title = '1' then 'Mr.'
When title = '2' then 'Mrs.'
Else 'Error'
End
From Contact



--
/*
Warren Brunk - MCITP - SQL 2005, MCDBA
www.techintsolutions.com
*/


[quoted text, click to view]

RE: PICKLIST IN SQL Ridhima Sood
9/14/2006 9:30:03 PM
the title field is a picklist field in CRM 3.0's entity contact that i want
to get the values in for in sql
[quoted text, click to view]
Re: PICKLIST IN SQL Arnie Rowland
9/14/2006 9:30:13 PM
Typically, what you are referring to is done with the client =
application. It is so easy to do conversion and enum list using client =
code.

However, if you have to do something like this in SQL code, use CASE =
statements. However, it is NOT dynamic and does not lead itself to easy =
change.

DECLARE @Title int
SET @Title =3D 3

SELECT=20
CASE @Title
WHEN 1 THEN 'Mr.'
WHEN 2 THEN 'Mrs.'
ELSE 'Unknown'
END




--=20
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.=20
Most experience comes from bad judgment.=20
- Anonymous


[quoted text, click to view]
to=20
Re: PICKLIST IN SQL Ridhima Sood
9/14/2006 9:38:01 PM
hi Aaron

This is not a table. It is a field in CRM of type picklist.. i am not sure
if i could clarify this. please let me know.

[quoted text, click to view]
RE: PICKLIST IN SQL Ridhima Sood
9/14/2006 9:39:01 PM
Waren i am kinda lost
below is the sql statement
SELECT ContactExtensionBase.New_ReferralPoints1, ContactBase.FirstName,
ContactBase.LastName, ContactExtensionBase.New_ReferralMethod1,
ContactExtensionBase.New_ReferralMethod2.,
ContactExtensionBase.New_ReferralPoints2
FROM ContactBase INNER JOIN
ContactExtensionBase ON ContactBase.ContactId =
ContactExtensionBase.ContactId

the result is for example
method first name last name points
2 Barbara Johnston 1
However i know that 1= web , 2=phone etc and for points
1=10,2=20 etc...but i cant get SQl to display the field value...

i hope u get what i mean now..

[quoted text, click to view]
Re: PICKLIST IN SQL Ridhima Sood
9/14/2006 9:49:02 PM
Aaron we are using an SBS CRM the database of which ties to SQL2005 and i am
using SQL Management studio 2005. Does this help please?

[quoted text, click to view]
Re: PICKLIST IN SQL Warren Brunk
9/14/2006 9:54:20 PM
You can use a case statement to replace values on any cloumn in your select
statement. You can also use a case on more than one column. Lets Pretend
that ContactExtensionBase.New_ReferralPoints1is the column points in your
example.

SELECT
Case ContactExtensionBase.New_ReferralPoints1 When 1 then 'Web' When 2 then
'Phone' Else 'Error' End as Points,
ContactBase.FirstName,
ContactBase.LastName,
ContactExtensionBase.New_ReferralMethod1,
ContactExtensionBase.New_ReferralMethod2.,
ContactExtensionBase.New_ReferralPoints2
FROM
ContactBase
INNER JOIN
ContactExtensionBase
ON ContactBase.ContactId = ContactExtensionBase.ContactId

--
/*
Warren Brunk - MCITP - SQL 2005, MCDBA
www.techintsolutions.com
*/


[quoted text, click to view]

Re: PICKLIST IN SQL Aaron Bertrand [SQL Server MVP]
9/15/2006 12:23:51 AM
[quoted text, click to view]

I don't understand, does your table have 1 column, 2 columns, 3 columns, 4?
If you only have 1 and 2 in your table, is SQL Server supposed to know that
1 = mr and 2 = mrs???

[quoted text, click to view]

You're going to have to provide more information. DDL, sample data, desired
results? (See http://www.aspfaq.com/5006

Re: PICKLIST IN SQL Aaron Bertrand [SQL Server MVP]
9/15/2006 12:41:27 AM
[quoted text, click to view]

And how are you getting to a picklist field in CRM from T-SQL?

Re: PICKLIST IN SQL Ridhima Sood
9/17/2006 1:40:01 PM
hi Warren

thanks heaps for the reply. Just a bit confused on the syntax "Else 'Error'
End as Points" what does this mean please....

really appreciate your reply.

regards
ridhima

[quoted text, click to view]
Re: PICKLIST IN SQL Ridhima Sood
9/17/2006 8:29:01 PM
thanks heaps Hugo. That was soooooooooooo helpful and clear :)

Have a great day. :)

[quoted text, click to view]
Re: PICKLIST IN SQL Hugo Kornelis
9/18/2006 12:01:30 AM
[quoted text, click to view]

Hi Ridhima,

That is a combination of the last part of the CASE expression, followed
by a column alias.

The CASE expression has two forms. Warren used the "simple CASE" syntax:

CASE input_expression
WHEN when_expression THEN result_expression
WHEN when_expression THEN result_expression
....
ELSE else_expression
END

SQL Server will evaluate the input_expression. It will the evaluate each
when_expression, in order, and as soon os it finds one that matches the
input_expression, the matching result_expression is returned as the
result of the CASE expression. If no match is found, the result of
evaluating the else_expression is used as the result of the CASE.

In the code above, "ELSE 'Error' END" is the last part of Warren's
"simple CASE" expression.

The last part ("AS Points") is simply to give the column in the result
set the name "Points". You can use this on any column in the column list
of a SELECT statement to override the default name. (The default name is
equal to the column name if only that column is specified in the SELECT
list, or blank if an expression or a constant is used). Note that the
keyword AS is optional - though I recommend anyone to always use it,
since it can help to reduce confusion.

--
AddThis Social Bookmark Button