converting nvarchar to int

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmorand
    New Member
    • Sep 2007
    • 219

    converting nvarchar to int

    I'm trying to convert a data type of nvarchar to an int data type, but I keep getting errors when I try.

    Here's my latest try:
    [code=sql]cast(convert(in t,submitter) as int) as 'Submitter' [/code]

    Any thoughts on how I can do this?
  • dmorand
    New Member
    • Sep 2007
    • 219

    #2
    Originally posted by dmorand
    I'm trying to convert a data type of nvarchar to an int data type, but I keep getting errors when I try.

    Here's my latest try:
    [code=sql]cast(convert(in t,submitter) as int) as 'Submitter' [/code]

    Any thoughts on how I can do this?
    Nevermind, it does work....it's coldfusion that's being dumb.

    Comment

    • dmorand
      New Member
      • Sep 2007
      • 219

      #3
      Actually I'm wrong. Some of the values in the table are text, and some are numbers so the fields that have text in them are causing the query to crash.

      Any idea on how to only retrieve the fields in the rows that have numerical values in them?

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        Try something like:

        Code:
        select 
              case 
                  when isnumeric(submitter) = 1 then 
                          cast(submitter AS int)
                  else
                          NULL
             end
        
        AS 'Submitter'
        from YourTable
        Just change the else part as necessary.

        -- CK

        Comment

        • dmorand
          New Member
          • Sep 2007
          • 219

          #5
          Originally posted by ck9663
          Try something like:

          Code:
          select 
                case 
                    when isnumeric(submitter) = 1 then 
                            cast(submitter AS int)
                    else
                            NULL
               end
          
          AS 'Submitter'
          from YourTable
          Just change the else part as necessary.

          -- CK
          That's a good idea, thanks!

          Comment

          • dmorand
            New Member
            • Sep 2007
            • 219

            #6
            Originally posted by dmorand
            That's a good idea, thanks!
            Thanks it worked out perfectly!

            Comment

            Working...