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. SQL string functions

SQL string functions

Scheduled Pinned Locked Moved Database
databasequestiontutorial
10 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.
  • Y Offline
    Y Offline
    Yevgeny Efter
    wrote on last edited by
    #1

    Hello! I am writing a stored procedure, and in my stored procedure, I need to work with a string. I found some functions in SQL: DATALENGTH, and SUBSTRING. But I can't find information about how to work with strings in SQL. What I need is to walk through a string, exemine every character and make the string smaller depending on the characters. First, how can I compare ntext strings? And, second, maybe there are more string functions in SQL? Thanks in advance!

    Have a nice day!

    A M 2 Replies Last reply
    0
    • Y Yevgeny Efter

      Hello! I am writing a stored procedure, and in my stored procedure, I need to work with a string. I found some functions in SQL: DATALENGTH, and SUBSTRING. But I can't find information about how to work with strings in SQL. What I need is to walk through a string, exemine every character and make the string smaller depending on the characters. First, how can I compare ntext strings? And, second, maybe there are more string functions in SQL? Thanks in advance!

      Have a nice day!

      A Offline
      A Offline
      A Wong
      wrote on last edited by
      #2

      You probably want to take a look at nvarchar and varchar types. You can also take a look at functions that might help you on msdn. http://msdn2.microsoft.com/en-us/library/ms186939.aspx\[[^](http://msdn2.microsoft.com/en-us/library/ms186939.aspx "New Window")]">

      Y 1 Reply Last reply
      0
      • Y Yevgeny Efter

        Hello! I am writing a stored procedure, and in my stored procedure, I need to work with a string. I found some functions in SQL: DATALENGTH, and SUBSTRING. But I can't find information about how to work with strings in SQL. What I need is to walk through a string, exemine every character and make the string smaller depending on the characters. First, how can I compare ntext strings? And, second, maybe there are more string functions in SQL? Thanks in advance!

        Have a nice day!

        M Offline
        M Offline
        Mark J Miller
        wrote on last edited by
        #3

        If you have SQL BOL installed here's the documetation for all string functions: ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/6940a83d-5374-4af3-bb27-5d89c8af83ac.htm Otherwise, here's the online help docs: http://msdn2.microsoft.com/en-us/library/ms181984.aspx[^]

        Mark's blog: developMENTALmadness.blogspot.com

        1 Reply Last reply
        0
        • A A Wong

          You probably want to take a look at nvarchar and varchar types. You can also take a look at functions that might help you on msdn. http://msdn2.microsoft.com/en-us/library/ms186939.aspx\[[^](http://msdn2.microsoft.com/en-us/library/ms186939.aspx "New Window")]">

          Y Offline
          Y Offline
          Yevgeny Efter
          wrote on last edited by
          #4

          My problem now is getting the last character of a ntext variable. I do it this way, but it doesn't seem to work: SUBSTRING(@strvar, DATALENGTH(@strvar), 0)

          Have a nice day!

          M 1 Reply Last reply
          0
          • Y Yevgeny Efter

            My problem now is getting the last character of a ntext variable. I do it this way, but it doesn't seem to work: SUBSTRING(@strvar, DATALENGTH(@strvar), 0)

            Have a nice day!

            M Offline
            M Offline
            Mark J Miller
            wrote on last edited by
            #5

            DECLARE @strvar VARCHAR(50) SET @strvar = 'hello world' SELECT SUBSTRING(@strvar, LEN(@strvar), 1)

            Mark's blog: developMENTALmadness.blogspot.com

            Y 1 Reply Last reply
            0
            • M Mark J Miller

              DECLARE @strvar VARCHAR(50) SET @strvar = 'hello world' SELECT SUBSTRING(@strvar, LEN(@strvar), 1)

              Mark's blog: developMENTALmadness.blogspot.com

              Y Offline
              Y Offline
              Yevgeny Efter
              wrote on last edited by
              #6

              Alright, now I try to compare it to a '\' using LIKE, and it never works. How can I check if this character is '\' or not?

              Have a nice day!

              M 1 Reply Last reply
              0
              • Y Yevgeny Efter

                Alright, now I try to compare it to a '\' using LIKE, and it never works. How can I check if this character is '\' or not?

                Have a nice day!

                M Offline
                M Offline
                Mark J Miller
                wrote on last edited by
                #7

                DECLARE @strvar VARCHAR(50) SET @strvar = 'hello world' SELECT CASE SUBSTRING(@strvar, LEN(@strvar), 1) WHEN '/' THEN 1 ELSE 0 END

                Mark's blog: developMENTALmadness.blogspot.com

                Y 1 Reply Last reply
                0
                • M Mark J Miller

                  DECLARE @strvar VARCHAR(50) SET @strvar = 'hello world' SELECT CASE SUBSTRING(@strvar, LEN(@strvar), 1) WHEN '/' THEN 1 ELSE 0 END

                  Mark's blog: developMENTALmadness.blogspot.com

                  Y Offline
                  Y Offline
                  Yevgeny Efter
                  wrote on last edited by
                  #8

                  Why do I need the SELECT? Won't WHILE(SUBSTRING(@strvar, LEN(@strvar), 1) NOT LIKE '\') work?

                  Have a nice day!

                  M 1 Reply Last reply
                  0
                  • Y Yevgeny Efter

                    Why do I need the SELECT? Won't WHILE(SUBSTRING(@strvar, LEN(@strvar), 1) NOT LIKE '\') work?

                    Have a nice day!

                    M Offline
                    M Offline
                    Mark J Miller
                    wrote on last edited by
                    #9

                    You don't need SELECT, I'm just using it like a Debug.Print to see the value returned. Why are you using LIKE? You aren't making any kind of pattern comparison. You are comparing the result with a constant expression. You should be doing it this way: SUBSTRING(@strvar, LEN(@strvar), 1) <> '\'

                    Mark's blog: developMENTALmadness.blogspot.com

                    Y 1 Reply Last reply
                    0
                    • M Mark J Miller

                      You don't need SELECT, I'm just using it like a Debug.Print to see the value returned. Why are you using LIKE? You aren't making any kind of pattern comparison. You are comparing the result with a constant expression. You should be doing it this way: SUBSTRING(@strvar, LEN(@strvar), 1) <> '\'

                      Mark's blog: developMENTALmadness.blogspot.com

                      Y Offline
                      Y Offline
                      Yevgeny Efter
                      wrote on last edited by
                      #10

                      Thank you!

                      Have a nice day!

                      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