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. MS SQL Server 2005 UDF only returns first character from a string value

MS SQL Server 2005 UDF only returns first character from a string value

Scheduled Pinned Locked Moved Database
databasesql-serversysadmin
7 Posts 4 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
    Steven J Jowett
    wrote on last edited by
    #1

    I have written a small Scalar UDF that excepts a VarChar value and check to see if the value is NULL. If the value is null an empty string should be returned else the value passed to the UDF should be returned. If the value is NULL an empty string is returned, so that bit works fine, but is I do something like this:-

    SELECT dbo.NullToString('Hello world')

    The value returned by the UDF is 'H', where I would expect 'Hello world' Here's the UDF code, hopefully someone can enlighten me to my mistake.

    CREATE FUNCTION NullToString
    (
    @value VarChar(255)
    )
    RETURNS VarChar
    AS
    BEGIN
    DECLARE @ReturnValue VarChar(255)
    IF(@value Is NULL)
    BEGIN
    SET @ReturnValue = ''
    END
    ELSE
    BEGIN
    SET @ReturnValue = @value
    END

     RETURN @ReturnValue
    

    END
    GO

    Thanks

    Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

    C J 2 Replies Last reply
    0
    • S Steven J Jowett

      I have written a small Scalar UDF that excepts a VarChar value and check to see if the value is NULL. If the value is null an empty string should be returned else the value passed to the UDF should be returned. If the value is NULL an empty string is returned, so that bit works fine, but is I do something like this:-

      SELECT dbo.NullToString('Hello world')

      The value returned by the UDF is 'H', where I would expect 'Hello world' Here's the UDF code, hopefully someone can enlighten me to my mistake.

      CREATE FUNCTION NullToString
      (
      @value VarChar(255)
      )
      RETURNS VarChar
      AS
      BEGIN
      DECLARE @ReturnValue VarChar(255)
      IF(@value Is NULL)
      BEGIN
      SET @ReturnValue = ''
      END
      ELSE
      BEGIN
      SET @ReturnValue = @value
      END

       RETURN @ReturnValue
      

      END
      GO

      Thanks

      Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      Sounds like a ANSI->UTF problem. Try to use NVarChar(255) istead of VarChar(255).

      Greetings Covean

      1 Reply Last reply
      0
      • S Steven J Jowett

        I have written a small Scalar UDF that excepts a VarChar value and check to see if the value is NULL. If the value is null an empty string should be returned else the value passed to the UDF should be returned. If the value is NULL an empty string is returned, so that bit works fine, but is I do something like this:-

        SELECT dbo.NullToString('Hello world')

        The value returned by the UDF is 'H', where I would expect 'Hello world' Here's the UDF code, hopefully someone can enlighten me to my mistake.

        CREATE FUNCTION NullToString
        (
        @value VarChar(255)
        )
        RETURNS VarChar
        AS
        BEGIN
        DECLARE @ReturnValue VarChar(255)
        IF(@value Is NULL)
        BEGIN
        SET @ReturnValue = ''
        END
        ELSE
        BEGIN
        SET @ReturnValue = @value
        END

         RETURN @ReturnValue
        

        END
        GO

        Thanks

        Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        Im guessing that the default length of a VarChar is 1

        RETURNS VarChar

        should possibly be

        RETURNS VarChar(255)

        BTW, you know there is a built-in function for this:

        ISNULL(@someVariable,'')

        IsNull Function (T-SQL)[^]

        S 1 Reply Last reply
        0
        • J J4amieC

          Im guessing that the default length of a VarChar is 1

          RETURNS VarChar

          should possibly be

          RETURNS VarChar(255)

          BTW, you know there is a built-in function for this:

          ISNULL(@someVariable,'')

          IsNull Function (T-SQL)[^]

          S Offline
          S Offline
          Steven J Jowett
          wrote on last edited by
          #4

          Thanks for the quick reply, and no I did not know there was a build-in function, so thanks for pointing that out to me. You learn something new everyday Thanks again

          Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

          A 1 Reply Last reply
          0
          • S Steven J Jowett

            Thanks for the quick reply, and no I did not know there was a build-in function, so thanks for pointing that out to me. You learn something new everyday Thanks again

            Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

            A Offline
            A Offline
            Ashfield
            wrote on last edited by
            #5

            There is also the coalesce function, which returns the first non-null value from a list

            select coalesce(col1val,col2val,'')
            from table1

            I cannot remember how many values you can have in the list, but its quite a few. I find it useful instead of nesting isnull's

            Bob Ashfield Consultants Ltd Proud to be a Code Project MVP

            S 1 Reply Last reply
            0
            • A Ashfield

              There is also the coalesce function, which returns the first non-null value from a list

              select coalesce(col1val,col2val,'')
              from table1

              I cannot remember how many values you can have in the list, but its quite a few. I find it useful instead of nesting isnull's

              Bob Ashfield Consultants Ltd Proud to be a Code Project MVP

              S Offline
              S Offline
              Steven J Jowett
              wrote on last edited by
              #6

              Ashfield wrote:

              There is also the coalesce function, which returns the first non-null value from a list

              That's interesting, I wish I'd known about that one 12 months ago. I shell be making use of that. Thanks Bob

              Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

              A 1 Reply Last reply
              0
              • S Steven J Jowett

                Ashfield wrote:

                There is also the coalesce function, which returns the first non-null value from a list

                That's interesting, I wish I'd known about that one 12 months ago. I shell be making use of that. Thanks Bob

                Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

                A Offline
                A Offline
                Ashfield
                wrote on last edited by
                #7

                Pleased to have been of help

                Bob Ashfield Consultants Ltd Proud to be a Code Project MVP

                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