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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. using replace function, Please help

using replace function, Please help

Scheduled Pinned Locked Moved Database
databasetutorialhelpquestion
5 Posts 3 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.
  • S Offline
    S Offline
    SharonRao
    wrote on last edited by
    #1

    Hello all, I have a products table (see below) Product State Apple |1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|2 0|21|22|23|24|25|26|27| I have to display the state data on a webform replacing the numbers with actual state names for example replace 1 with auckland, 2 with northland and so on. How to do this in SQL. the returned data from my sql query should look like this string, how do i write the query to acheive this. Auckland
    Northland
    Waikatoo
    Please help. Cheers, Shilpa.

    Thanks in Advance for your help. Best Regards

    J E 2 Replies Last reply
    0
    • S SharonRao

      Hello all, I have a products table (see below) Product State Apple |1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|2 0|21|22|23|24|25|26|27| I have to display the state data on a webform replacing the numbers with actual state names for example replace 1 with auckland, 2 with northland and so on. How to do this in SQL. the returned data from my sql query should look like this string, how do i write the query to acheive this. Auckland
      Northland
      Waikatoo
      Please help. Cheers, Shilpa.

      Thanks in Advance for your help. Best Regards

      J Offline
      J Offline
      John ph
      wrote on last edited by
      #2

      tblProductsS (table) (columns) Product_ID int Product_Name Nvarchar State Nvarchar tblState (table) (Columns) State_Id int State_Name Nvarchar sql ---- DECLARE @SQLQuery AS NVARCHAR(2000) DECLARE @SubQuery AS NVARCHAR(100) select @SubQuery=Replace(State,'|',',') from tblProductsS where Product_ID = 1 --@ProductID SET @SQLQuery = 'Select State_Name from tblstate where State_ID IN (' SET @SQLQuery = @SQLQuery + @SubQuery SET @SQLQuery = @SQLQuery + ')' EXEC sp_executesql @SQLQuery


      Regards
      John


      1 Reply Last reply
      0
      • S SharonRao

        Hello all, I have a products table (see below) Product State Apple |1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|2 0|21|22|23|24|25|26|27| I have to display the state data on a webform replacing the numbers with actual state names for example replace 1 with auckland, 2 with northland and so on. How to do this in SQL. the returned data from my sql query should look like this string, how do i write the query to acheive this. Auckland
        Northland
        Waikatoo
        Please help. Cheers, Shilpa.

        Thanks in Advance for your help. Best Regards

        E Offline
        E Offline
        edukulla
        wrote on last edited by
        #3

        We can do this, if you can give us more information about other tables like a)Where is the state information stored? Is it in a separate table and how "product" table and this table are related? b)What input you pass to the query EX: you just wanted to pass the number Ex: 1 or 2 etc right? I can provide you currently the abstract code it will look some thing like this. Suppose the input request from webform to db is '15' Declare @check_cond int Select @check_cond = PATINDEX('%15%',product state) from products Now, @check_cond contains the position of your input in the product state column of products table. patindex returns zero if it does not match your input. Thanks!

        Santhosh Kumar Edukulla

        J 1 Reply Last reply
        0
        • E edukulla

          We can do this, if you can give us more information about other tables like a)Where is the state information stored? Is it in a separate table and how "product" table and this table are related? b)What input you pass to the query EX: you just wanted to pass the number Ex: 1 or 2 etc right? I can provide you currently the abstract code it will look some thing like this. Suppose the input request from webform to db is '15' Declare @check_cond int Select @check_cond = PATINDEX('%15%',product state) from products Now, @check_cond contains the position of your input in the product state column of products table. patindex returns zero if it does not match your input. Thanks!

          Santhosh Kumar Edukulla

          J Offline
          J Offline
          John ph
          wrote on last edited by
          #4

          PATINDEX function returns the starting position of the pattern within the string being searched. I don't think this is neccessary...


          Regards
          John


          E 1 Reply Last reply
          0
          • J John ph

            PATINDEX function returns the starting position of the pattern within the string being searched. I don't think this is neccessary...


            Regards
            John


            E Offline
            E Offline
            edukulla
            wrote on last edited by
            #5

            Patindex is used to check whether the given 'id' is in products table. Then Use the same input id to query the productstate table to know the statename accordingly. Thanks!

            Santhosh Kumar Edukulla

            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