Results 1 to 3 of 3

Thread: Numeric From nvarchar (SQL 2k) [Resolved]

  1. #1

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    Post Numeric From nvarchar (SQL 2k) [Resolved]

    I need to create this query that will grab the Duration field, but
    the problem is that the data is of nvarchar type and the actual
    data is like "**7NTS" or "***7NTS" or "14NTS" or "**21", etc. I
    want to use a loop to check each char position, but dont know if
    possible. Or strip out non-numeric data somehow. This is what I
    have so far.

    Code:
    SELECT
     CASE Duration
      WHEN ISNUMERIC(Duration) = 1 THEN
       Duration = Duration --?
      WHEN ISNUMERIC(Duration) = 0 THEN
       WHILE ISNUMERIC(SUBSTRING(Duration,1,1)) = 1 --Need to check each char ?
        BEGIN
         --SOMETHING HERE  
         IF SOMETHING THEN
          BREAK
         ELSE
          CONTINUE
        END
     END
    FROM Table1
    Last edited by RobDog888; Sep 15th, 2004 at 04:44 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,882
    Code:
    Declare @STR varchar(10)
    Declare @NUM varchar(10)
    Declare @VAL int
    
    Set @STR='**21NTS'
    Set @NUM=''
    
    While @STR<>''
    Begin
    	If IsNumeric(Left(@STR,1))=1 Set @Num=@Num+Left(@Str,1)
            Set @Str=Right(@Str,Len(@Str)-1)
    End
    
    Set @Val=Cast(@Num as int)
    
    Print @Val
    Put that into a USER DEFINED function and you can use it in-line in a SELECT statement.

  3. #3

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713
    Cool! Thanks szlamany.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width