?
Solved

charindex()

Posted on 2009-04-03
5
Medium Priority
?
1,498 Views
Last Modified: 2013-11-10
Hi,
i am trying to find out first part of my filename is numeric or not using followed function
DECLARE  @filename varchar(100)
SET @filename = ?
SELECT     isnumeric(substring(@filename, 1,(charindex('_', @filename,1)-1))) AS result1
when i give the filename directly and check it works well in SSMS,but when i pass the file name through an input param in SSIS it fails..any help appriciated..
0
Comment
Question by:ametuer999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24067234
Is the entire thing throwing an error?  I'm not familiar with passing parameters to SSIS, but perhaps the ? has to be in quotes.  I thought that you could read the variable another way though.


DECLARE  @filename varchar(100)
SET @filename = '?'
SELECT     isnumeric(substring(@filename, 1,(charindex('_', @filename,1)-1))) AS result1
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 24068581
For testing purposes add a "print @filename" statement to see what the variable is actually is.

Where is the question mark coming from anyway?
DECLARE  @filename varchar(100)
SET @filename = ?
print @filename
SELECT  isnumeric(substring(@filename, 1,(charindex('_', @filename,1)-1))) AS result1

Open in new window

0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 24073473
You could also check this using script rather than T-SQL. Then you don't need a database connection to check if your file is numeric.
0
 

Author Comment

by:ametuer999
ID: 24073726
could you please give me a sample code...like....
0
 
LVL 30

Accepted Solution

by:
nmcdermaid earned 1500 total points
ID: 24074426
Below is some very simple code.
It inspects a variable called FileName. It grabs the characters before an underscore and checks if they are numeric.
It uses dialog boxes to help you debug.
To use it:
1. Drag a Script Task on to your page
2. Double click it. Press the Script selector on the left. Press the Design Script button
3. Paste in the code below
4. Ensure you have a variable called FileName, with a value in it
5. Right click on the script task and press Execute Task
You'll see some message boxes come up and finally tell you whether the chars preceding the _ are numeric or not.
If this suits you let me know and we can integrate it further. I assume this relates to your other question about looping through files.
PS I am not a .Net coding expert, there may be optimisations on this code.

' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic
' The ScriptMain class is the entry point of the Script Task.
 
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
 
Public Class ScriptMain
 
    Public Sub Main()
        Dim sVariable As String
        Dim iInteger As Integer
        Dim bResult As Boolean
        Dim vars As Variables
 
        Dts.VariableDispenser.LockOneForRead("FileName", vars)
        sVariable = vars(0).Value.ToString
        vars.Unlock()
 
 
        MsgBox("Value of variable is [" & sVariable & "]")
 
        If sVariable.IndexOf("_") > 0 Then
            sVariable = Left(sVariable, sVariable.IndexOf("_"))
        Else
            sVariable = ""
        End If
 
        MsgBox("Value of string preceding _ is [" & sVariable & "]")
 
        bResult = iInteger.TryParse(sVariable, iInteger)
 
        MsgBox("Starts with numeric? " & bResult.ToString)
 
        Dts.TaskResult = Dts.Results.Success
    End Sub
 
 
End Class

Open in new window

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question