Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Query Select

SQL Query Select

Scheduled Pinned Locked Moved Database
helpdatabasetutoriallearning
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    reogeo2008
    wrote on last edited by
    #1

    Hi all, Can anybody help me in the following issue. I have a course table contains coursename and type I want to select course name and type and the display should like Coursename(type) th problem is my type values are integers.. For example 1 for Regular type ,2 for distance type so if type is 1 and coursename is MCA Output should be MCA(Regular) is it possiblle thru the sql statement Thanks in advance, Reena

    B M N 3 Replies Last reply
    0
    • R reogeo2008

      Hi all, Can anybody help me in the following issue. I have a course table contains coursename and type I want to select course name and type and the display should like Coursename(type) th problem is my type values are integers.. For example 1 for Regular type ,2 for distance type so if type is 1 and coursename is MCA Output should be MCA(Regular) is it possiblle thru the sql statement Thanks in advance, Reena

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      I guess you have to have linked table for type of course, Here is example how it should be

      select
      case when [type]=1 then 'MCA' when [type]=2 then 'Another Title' end + ' (' +
      coursename +')' as TypeAndCourse
      from courses


      I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

      A 1 Reply Last reply
      0
      • R reogeo2008

        Hi all, Can anybody help me in the following issue. I have a course table contains coursename and type I want to select course name and type and the display should like Coursename(type) th problem is my type values are integers.. For example 1 for Regular type ,2 for distance type so if type is 1 and coursename is MCA Output should be MCA(Regular) is it possiblle thru the sql statement Thanks in advance, Reena

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Create a table of CourseTypes with a PK field and a description. This is then an extensible solution if you need to add another type. Create a view with an inner join (assumes the type is required in the course table and that a course can only have 1 type) to the type table and include the course type description field (I name the view vwCourse) Or just do it the way the Blue one suggested!

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • B Blue_Boy

          I guess you have to have linked table for type of course, Here is example how it should be

          select
          case when [type]=1 then 'MCA' when [type]=2 then 'Another Title' end + ' (' +
          coursename +')' as TypeAndCourse
          from courses


          I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

          A Offline
          A Offline
          AditSheth
          wrote on last edited by
          #4

          Blue_Boy wrote:

          select case when [type]=1 then 'MCA' when [type]=2 then 'Another Title' end + ' (' + coursename +')' as TypeAndCourse from courses

          just little changes

          SELECT [Coursename] + '(' + case when [type] = 1 THEN 'Regular' when [type]=2 then 'Another Title' end + ')' as TypeAndCourse
          from courses

          but i am agree with "Mycroft Holmes" suggestion of creating new table..

          Where can we go to find God if we cannot see Him in our own hearts and in every living being -Swami Vivekananda

          1 Reply Last reply
          0
          • R reogeo2008

            Hi all, Can anybody help me in the following issue. I have a course table contains coursename and type I want to select course name and type and the display should like Coursename(type) th problem is my type values are integers.. For example 1 for Regular type ,2 for distance type so if type is 1 and coursename is MCA Output should be MCA(Regular) is it possiblle thru the sql statement Thanks in advance, Reena

            N Offline
            N Offline
            Niladri_Biswas
            wrote on last edited by
            #5

            Try this

            Declare @Course table ([Course Name] Varchar(100),[Course Type] Int)
            Insert Into @Course
            Select 'MCA',1 Union All
            Select 'MCA',2

            ;With CTE AS(
            Select [Course Type] = 1,[Course Description] = 'Regular' Union All
            Select [Course Type] = 2,[Course Description] = 'Distance'
            )

            Select
            Result =
            c.[Course Name]
            + '('
            + CTE.[Course Description]
            + ')'
            From @Course c
            Join CTE ON c.[Course Type] = CTE.[Course Type]

            If you are using Denali CTP 3, you can use the new Choose function

            Select
            Result =
            Concat(
            c.[Course Name]
            ,'('
            ,CTE.[Course Description]
            ,')'
            )
            From @Course c
            Join CTE ON c.[Course Type] = CTE.[Course Type]

            Output

            Result
            MCA(Regular)
            MCA(Distance)

            Niladri Biswas

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups