Converting number to text for concatenation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jinzuku
    New Member
    • Feb 2011
    • 12

    Converting number to text for concatenation

    Hi all,

    I'm trying to create a unique identifier field in my table but the two fields I'm trying to concatenate are numbers.

    When I write "nhsnumber + diffdate as a_sid" it tries to add the fields together rather than concatenating them.

    I've tried converting them into varchars and chars and it's still doing the same thing.

    How can I convert these to text so that they can concatenated?

    Thanks

    Sunny
  • C CSR
    New Member
    • Jan 2012
    • 144

    #2
    What language, what are the original datatypes, etc?

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32636

      #3
      I'd use CAST() to convert both your fields to string before using the concatenation character + (which is also the addition character for numerics of course).
      Last edited by NeoPa; Jan 17 '12, 09:26 PM.

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        It's because you convert after you add the values. Convert the values then concatenate them.

        Good Luck!!!


        ~~ CK

        Comment

        • Jinzuku
          New Member
          • Feb 2011
          • 12

          #5
          Thanks all for your help but I've managed to figure it out. The reason I was having issues was because I was trying to convert them to char or varchar, but what I really needed to convert them to was nvarchar and then concatenate them.

          Still not overly sure what the difference between varchar and nvarchar is but at least it works :D

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32636

            #6
            The difference is that char and varchar hold the characters as unicode values (16 bits) rather than the ASCII (8 bits) of the nchar and nvarchar types.

            Comment

            Working...