?
Solved

Cursor loop for all values inside case

Posted on 2007-08-09
3
Medium Priority
?
255 Views
Last Modified: 2013-11-05
Is there a way I can loop through when district count > 1, and get all District Values
Something of the kind DSDC.DistrictValue(0) + DSDC.DistrictValue(1) + DSDC.DistrictValue(2) ...

                    CASE
            WHEN DSDC.DistrictCount > 1 THEN
                  DSDC.DistrictValue + '*'  -- I need to loop here for all values
            ELSE
                  DSDC.DistrictValue
      END As DistrictCourt
                     ............................................

Appreciate the Help
VB
0
Comment
Question by:vbothello
3 Comments
 
LVL 2

Expert Comment

by:vissol
ID: 19666368
Is this a SQL Server question ?
0
 
LVL 8

Accepted Solution

by:
srafi78 earned 1000 total points
ID: 19666486
Do you want to list all the Different District values together?
Create function fnDistrict(@Distrinct as int)
Returns Varchar(1000)
AS
Begin
Declare @DistrictValue Varchar(1000)
SET @DistrictValue = ''
SELECT @DistrictValue = @DistrictValue + ', ' + DistrictValue From DSDC where District = @District
Return Left(@DistrictValue,  Len(@DistrictValue) -2)
End

Use this function in the select and pass each district as

Select Col1, Col2, dbo.fnDistrict(District) from DSDC
0
 

Author Comment

by:vbothello
ID: 19669525
This is a SQL Server question, The Case statement is just a part of the query (DSDC, DSPS is the table name already defined )
========================================================================
      ,CASE
            WHEN DSDC.DistrictCount > 1 THEN
                  DSDC.DistrictValue + '*'
            ELSE
                  DSDC.DistrictValue
      END As DistrictCourt
      ,CASE
            WHEN DSPS.DistrictCount > 1 THEN
                  DSPS.DistrictValue + '*'
            ELSE
                  DSPS.DistrictValue
      END As PSC
=====================================================================
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question