How to spilt a field in SQL

Hi
How can I spilt this field to return only the codes starting with AB?  The AB code could be in any position in the column.  The length of the numbers are always the same.
GW12345 | QW12345 | AB12345
Ive tried a case statement, but it only works if the AB code is the first code in the column.  It is ignoring the code if it is in 2nd or 3rd position
Thanks

You may check this as well:
declare @ch varchar(100)
set @ch='GW12345 | QW12345 | AB12345'
--Method 1
select LTRIM(fn.data) as data from
(select @ch) t(ch)
cross apply sample_split(t.ch,'|') fn
where LTRIM(fn.data) like 'AB%'
--Method 2
select data
from
(select replace(@ch,' | ','.')) t(ch)
cross apply (select parsename(ch,1) as data UNION select parsename(ch,2) UNION select parsename(ch,3)) fn(Data)
where data like 'AB%'
Sample_Split:
CREATE FUNCTION dbo.sample_Split
@RowData nvarchar(2000),
@SplitOn nvarchar(5)
RETURNS @RtnValue table
Id int identity(1,1),
Data nvarchar(100)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1
While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (data)
Select
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End
Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))
Return
END
Thanks,
Jay
<If the post was helpful mark as 'Helpful' and if the post answered your query, mark as 'Answered'>

Similar Messages

  • How to use Image field in SQL-7 for curvs

    I sampling data from a NI-card. There will be a lot of data so I thought I should use Image-field in SQL-7 to save the hole curve. How do you do that?

    I would suggest looking into the Database Connectivity Toolset. It has complete SQL functionality and uses ADO to connect to many types of databases. I have it and it comes with example programs which demonstrate how to write to and read from databases using SQL.
    J.R. Allen

  • How to get different field in two are more different table using open sql

    Dear all,
              This SenthilMani am very new into sap abap am having doubt in reports how get the different fields from different tables like mara,marc,mard using open sql and native sql program give me some tips to get the data .
    with regards,
    senthil

    HI ,
      1) If u want to select data from more the two table then u can use FOR ALL ENTRIES.
             EX ..Open sql
                       select matnr from mara into table t_mara.
                      select matnr werks from marc into table t_marc for all entries in t_mara where matnr = t_mara-matnr.
      2) U can join more than one table.
               ex:
                   select mara~matnr
                              marc~werks
                    from mara join marc into table t_maramarc
                     on maramatnr = marcmatnr
    3) Using  native sql  ...only u can use JOIN statement

  • How to populate image and text field in SQL Server

    I want to populate a table, i.e insert images in a table in SQL Server. I am using Java(JSP/Servlet). Can any one suggest me how to populate the image and text fields in SQL Server...of course, using Java. If possible, could you please give me a piece of code?
    Regards -
    Samit

    Hi,
    Please check the following link for information on inserting images in to sql server database.
    http://www.databasejournal.com/features/mssql/article.php/1475641
    Cheers,
    vidyut

  • How to add a new field from SQL to report

    Post Author: Auburn
    CA Forum: .NET
    The field I am wanting to add to my report is not an actual field in SQL.  The field is actually a calculation done in the Select statement like this:  SELECT (SUM(CASE KILSHEET.Contract WHEN 1 THEN ((TOT_CARC_WEIGHT / 100) * 1.75) ELSE 0 END)) AS LSS here is the actual full SELECT: SELECT Sum(KILSHEET.PAY) As Pay, Sum(KILSHEET.Dead_Pay) As Dead_Pay, Sum(KILSHEET.GRADE_PREM) As Grade_Prem, Sum(KILSHEET.SORT_ADJ) As Sort_Adj, Sum(KILSHEET.SLOWS_PAY) As Slows_Pay, Sum(KILSHEET.RESALES_PAY) As Resales_Pay, Sum(KILSHEET.PLANT_CHARGE) As Plant_Charge, Sum(KILSHEET.OTHER_PREM) As Other_Prem, Sum(KILSHEET.NPB) As NPB, Sum(KILSHEET.SLOW_NPB) AS Slow_NPB, Sum(KILSHEET.RESALE_NPB) AS RESALE_NPB, Sum(KILSHEET.NPPC) AS NPPC, Sum(KILSHEET.SLOW_NPPC) AS SLOW_NPPC, Sum(KILSHEET.RESALE_NPPC) AS RESALE_NPPC, Sum(KILSHEET.NUMB_HEAD) AS NUMB_HEAD, Sum(KILSHEET.WEIGHT_FEE) AS WEIGHT_FEE, Sum(KILSHEET.SLOW_WEIGHT_FEE) AS SLOW_WEIGHT_FEE, Sum(KILSHEET.RESALE_WEIGHT_FEE) AS RESALE_WEIGHT_FEE, Sum(KILSHEET.WEIGHT) AS WEIGHT, Avg(KILSHEET.PER_LEAN)AS PER_LEAN, AVG(KILSHEET.ACTUAL_YIELD_PER) AS ACTUAL_YIELD_PER, AVG(KILSHEET.AVG_BACKFAT) AS AVG_BACKFAT, Sum(KILSHEET.TOT_CARC_WEIGHT) AS TOT_CARC_WEIGHT, Sum(KILSHEET.DIY) AS DIY, Sum(KILSHEET.DEAD) AS DEAD, Sum(KILSHEET.SLOWS) AS SLOWS, Sum(KILSHEET.RESALES) AS RESALES, Sum(KILSHEET.CONDEMNED) AS CONDEMNED, Sum(KILSHEET.DEAD_WEIGHT) AS DEAD_WEIGHT, Sum(KILSHEET.CONDEMNED_WEIGHT) AS CONDEMNED_WEIGHT, Sum(KILSHEET.SLOWS_WEIGHT) AS SLOWS_WEIGHT, Sum(KILSHEET.RESALES_WEIGHT) AS RESALES_WEIGHT, SUM((Kilsheet.TOT_Carc_WEIGHT / 100) * MarketPrice.mkprice) As TotalPrice,(SUM(CASE KILSHEET.Contract WHEN 1 THEN ((TOT_CARC_WEIGHT / 100) * 1.75) ELSE 0 END)) AS LSS FROM KILSHEET LEFT OUTER JOIN marketprice ON KILSHEET.Contract = marketprice.contract AND DATEPART(week, KILSHEET.KILLDATE) = marketprice.mkweek AND DATEPART(year, KILSHEET.KILLDATE) = marketprice.mkyear " & condStr + " " + locStr The problem with this is that my report is a Summary report.  It has to summarize multiple fields and the LSS calculation only needs to be calculated for certain records, hence the CASE.  Is there a way to add the LSS field to the report without it being physically part of the database? I am really struggling with this and any help would be greatly appreciated.  I hope I described this ok.

    Post Author: blairwheadon
    CA Forum: .NET
    This is what SQL Expressions are for.  I suggest you try using those (visible in the Field Explorer) to capture custom SQL field logic.
    Alterantively you could use a SQL command with your hand-written SQL.
    Thanks, Blair

  • Hi, plz help me. i wanna store photos to sql database. so how to insert file field into insert record in dreamweaver cs3?

    I dont know how to insert file field into insert record in DW. i also want to know, insert dynamic table with image field. Plz help me anyone. Ty..

    ohh kk thank you bro. But How to store photos to server file. I wanna upload and display the photos in webpage. Can you send me the php code for this?

  • [Forum FAQ] How to use multiple field terminators in BULK INSERT or BCP command line

    Introduction
    Some people want to know if we can have multiple field terminators in BULK INSERT or BCP commands, and how to implement multiple field terminators in BULK INSERT or BCP commands.
    Solution
    For character data fields, optional terminating characters allow you to mark the end of each field in a data file with a field terminator, as well as the end of each row with a row terminator. If a terminator character occurs within the data, it is interpreted
    as a terminator, not as data, and the data after that character is interpreted and belongs to the next field or record. I have done a test, if you use BULK INSERT or BCP commands and set the multiple field terminators, you can refer to the following command.
    In Windows command line,
    bcp <Databasename.schema.tablename> out “<path>” –c –t –r –T
    For example, you can export data from the Department table with bcp command and use the comma and colon (,:) as one field terminator.
    bcp AdventureWorks.HumanResources.Department out C:\myDepartment.txt -c -t ,: -r \n –T
    The txt file as follows:
    However, if you want to bcp by using multiple field terminators the same as the following command, which will still use the last terminator defined by default.
    bcp AdventureWorks.HumanResources.Department in C:\myDepartment.txt -c -t , -r \n -t: –T
    The txt file as follows:
    When multiple field terminators means multiple fields, you use the below comma separated format,
    column1,,column2,,,column3
    In this occasion, you only separate 3 fields (column1, column2 and column3). In fact, after testing, there will be 6 fields here. That is the significance of a field terminator (comma in this case).
    Meanwhile, using BULK INSERT to import the data of the data file into the SQL table, if you specify terminator for BULK import, you can only set multiple characters as one terminator in the BULK INSERT statement.
    USE <testdatabase>;
    GO
    BULK INSERT <your table> FROM ‘<Path>’
     WITH (
    DATAFILETYPE = ' char/native/ widechar /widenative',
     FIELDTERMINATOR = ' field_terminator',
    For example, using BULK INSERT to import the data of C:\myDepartment.txt data file into the DepartmentTest table, the field terminator (,:) must be declared in the statement.
    In SQL Server Management Studio Query Editor:
    BULK INSERT AdventureWorks.HumanResources.DepartmentTest FROM ‘C:\myDepartment.txt’
     WITH (
    DATAFILETYPE = ‘char',
    FIELDTERMINATOR = ‘,:’,
    The new table contains like as follows:  
    We could not declare multiple field terminators (, and :) in the Query statement,  as the following format, a duplicate error will occur.
    In SQL Server Management Studio Query Editor:
    BULK INSERT AdventureWorks.HumanResources.DepartmentTest FROM ‘C:\myDepartment.txt’
     WITH (
    DATAFILETYPE = ‘char',
    FIELDTERMINATOR = ‘,’,
    FIELDTERMINATOR = ‘:’
    However, if you want to use a data file with fewer or more fields, we can implement via setting extra field length to 0 for fewer fields or omitting or skipping more fields during the bulk copy procedure.  
    More Information
    For more information about filed terminators, you can review the following article.
    http://technet.microsoft.com/en-us/library/aa196735(v=sql.80).aspx
    http://social.technet.microsoft.com/Forums/en-US/d2fa4b1e-3bd4-4379-bc30-389202a99ae2/multiple-field-terminators-in-bulk-insert-or-bcp?forum=sqlgetsta
    http://technet.microsoft.com/en-us/library/ms191485.aspx
    http://technet.microsoft.com/en-us/library/aa173858(v=sql.80).aspx
    http://technet.microsoft.com/en-us/library/aa173842(v=sql.80).aspx
    Applies to
    SQL Server 2012
    SQL Server 2008R2
    SQL Server 2005
    SQL Server 2000
    Please click to vote if the post helps you. This can be beneficial to other community members reading the thread.

    Thanks,
    Is this a supported scenario, or does it use unsupported features?
    For example, can we call exec [ReportServer].dbo.AddEvent @EventType='TimedSubscription', @EventData='b64ce7ec-d598-45cd-bbc2-ea202e0c129d'
    in a supported way?
    Thanks! Josh

  • How to Assign a Field value to a Variable??

    Dear All,
    Kindly let me know, if <b>"HOW TO ASSIGN A FIELD VALUE TO A VARIABLE????"</b>
    Situation is, we have a Field (OBJK-TASER) in which 2 tables (SER01 & SER03) are stored. And we want to access names of the Tables which are there in the field(TASER) in  (OBJK) table  to a variable and then passing that variable to the sql query as a table name to get data accessed dynamicaly.

    try this
    select TASER from OBJK into table it_table.
    loop at it_table.
    select field1 field2 from (it_table-table) into table it_fields.
    endloop

  • How to populate this fields in ce_bank_accounts?

    Hey there, first, i want to apologize for my bad english.
    i'm working with Cash management and i'm using this API ce_bank_pub because the bank are internals. i have the load from banks and branch, and kinda the bank accounts, but i can´t find how to populate this fields, that are mandatory for my requeriment:
    AUTORECON_AP_MATCHING_ORDER
    AUTORECON_AR_MATCHING_ORDER  
    RECON_ENABLE_OI_FLAG
    RECON_OI_FLOAT_STATUS
    RECON_OI_CLEARED_STATUS
    RECON_OI_MATCHING_CODE
    AUTORECON_AP_MATCHING_ORDER2
    this is my "flow of work", first, i load the data with SQL*Loader in a custom/personal table, after this, i have a cursor that select all the records of this table and do some validations, after this (and if all the records are ok) i use the API ce_bank_pub.
    i don't know if that API support those fields, I've read the documentation but can not find what I require.
    any help will be appreciate.
    thanks a lot.
    [SPANISH]
    Estoy trabajando con una carga de bancos, sucursales, cuentas bancarias y usos de cuentas, hasta ahorita todo bien, solo tengo problemas al poblar los campos que se muestran en la parte de arriba en la tabla ce_bank_accounts, estoy utilizando el API ce_bank_pub ya que los bancos son internos.
    mi procedimiento, o la manera que lo hago es ésta:
    cargo los datos con SQL*Loader a una tabla intermedia/temporal por decirlo así, posteriormente un cursor selecciona los datos para realizar las validaciones y las reglas de negocio y si los datos son correctos utilizo el API, eso es en general para las 4 cosas, pero los campos de arriba no se como cargarlo, modificarlos o si hay alguna otra manera para agregarselos.
    cualquier ayuda es apreciada.
    Muchas gracias.

    Hi Miguel,
    hope this helps ...
    AUTORECON_AP_MATCHING_ORDER - Order of matching statement lines to AP transactions, by transactions first or by batch first, lookup type BATCH_OR_TRX
    AUTORECON_AR_MATCHING_ORDER - Order of matching statement lines to AR transactions, by transactions first or by batch first, lookup type BATCH_OR_TRX
    AUTORECON_AP_MATCHING_ORDER2 - Order of matching statement lines to AP transactions, by transactions first or by batch first, lookup type BATCH_OR_TRX_OR_GROUP
    For all the above three fields, navigate to Lookups from Cash Management responsibility and find the lookup type BATCH_OR_TRX, this will have 3 values, B for Batch, G for Group and T for Transaction ..... pass any one of the three value as per your requirement ..
    RECON_OI_MATCHING_CODE - Open interface transactions matching code, T for matching by transaction number, D for matching by date and amount. Lookup type: OI_MATCHING_CRITERIA
    For the above field, navigate to Lookups from Cash Management responsibility and find the lookup type OI_MATCHING_CRITERIA, this will have 2 values D for Date and Amount, T for Transaction Number .... Pass any one of the two values as per your requirement ...
    RECON_ENABLE_OI_FLAG -  Flag to indicate whether to allow reconciliation of statement lines to open interface transactions, Y or N
    As per your requirement indicate Y or N ....
    RECON_OI_FLOAT_STATUS - Code to specify the float status for open interface transactions
    RECON_OI_CLEARED_STATUS  - Code to specify cleared status for open interface transactions
    I am not sure about the above two ...i.e. the values that needs to be passed ...
    Regards,
    Ivruksha

  • Query: How to add new fields in ABAP query?

    Hi All,
    Can any one tell me how to add new fields in the ABAP query output?
    We have to add the new fields using query design or we have
    to add those in the background program(automatically generated behind query) by selecting new fields in the code?

    What kind of report? Usually, you have to add the field in the table declaration and then also in the SQL query. After that, include the field in what kind of report you are using.

  • How to store jpeg images in SQL server using NI database Connectivity Toolset. Can anyone please help me in this regard.

    Please tell how to store jpeg images in SQL Server using NI Database Connectivity Toolset.

    http://www.w3schools.com/sql/sql_datatypes.asp
    You setup a field as BLOB and store the binary of the picture there. Depending on the database it can be called differently as you can see in the link, in SQL server there's even a Image datatype.
    /Y
    LabVIEW 8.2 - 2014
    "Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
    G# - Free award winning reference based OOP for LV

  • How to use primary key in sql combined from 2 columns

    table1
    primary key = orderid+custname
    orderid custname address
    8889 Adrian london
    8867 Clara manchester
    2229 Peter warwick
    table2
    primary key = orderid+custname
    orderid custname qty price
    8889 Adrian 10 2500
    8865 Adrian 20 9483
    8867 Clara 30 3993
    5869 Clara 15 1828
    2290 Peter 10 2102
    2298 Peter 13 3839
    then how do i query ?
    SQL> select t1.orderid, t1.custname, t1.address, t2.qty, t2.price from
    table1 t1, table2 where ? = ?
    what's the "?" should be ????
    it doesn't work if i enter the primary constraint key for each table.
    thanks.

    thanks, it works ;)
    btw, do you know how about the date field ?
    what if one of the field in table1 is 12/12/2005 14:30
    and table2 is 15/12/2005
    table1
    orderid orderdate
    1234 12/12/2005 14:30
    2234 15/12/2005 13:45
    table2
    orderid orderdate
    1234 12/12/2005
    2234 15/12/2005
    so the query
    select t1.orderid as t1 t1.orderdate , t2.orderid as t2, t2.orderdate from
    table1 t1, table2 t2 where t1.orderid = t2.orderid
    and to_char(to_date(t1.orderdate, 'dd/mm/yyyy hh24:mi'), 'dd/mm/yyyy') = t2.orderdate
    thanks.

  • CRS Editor - Retrieving Timestamp database field from SQL 2005

    Hello -
    For the life of me, I cannot retrieve a database timestamp field from SQL 2005.
    I have successfully used 'cast(Date as char(30)) as DateStr' to convert to string but the DBRead statement displays the original object only as java.lang.Timestamp.  Any posts regarding this topic usually require Java skill sets above my level (creating new java class for import,...).
    There has to be an easy way to do this without being a Java programmer, yes?
    Any help here would greatly be appreciated!
    Thanks
    Andy Backus
    941.587.7199

    Andy,
    I'm guessing you know that this isn't really a date/time value in MS SQL.  According to Microsoft: 
    The Transact-SQL timestamp data type is not the same as the timestamp data type defined in the SQL-92 standard. The SQL-92 timestamp data type is equivalent to the Transact-SQL datetime data type.
    But to your question how to get the timestamp value out in a string...You'll want to use the convert function:
    select convert(varchar(10),myTimestampField) from myTable
    That will return the hex value in the column in a string format.  Also, when you retrieve the data, are you looking at the original table (which specifies the column is a timestamp), or did you create a dummy table with the columns named and formatted the way the data will be returned that UCCX can look at for the schema?  If it doesn't have the correct schema to look at, you'll have problems.
    Cliff
    please rate helpful posts

  • How to store 999999999.00000000 in sql server through ssis package?

    I have a field with 999999999.00000000 value of datatype double ?
    How to store it in ms sql server 2005 db through ssis package ?
    give me details if I have to use derived columns and what to write over there.
    please write in details as I am new to SSIS.
    Thanks in Advance.

    The value is coming from informix db .
    Can u please suggest hw to write it in my package : 
    I have taken a OLE DB Source and wrote the follwing command :
     " select CASE WHEN T1.t_maxs IS NULL THEN 0.00 ELSE T1.t_maxs END as max_inv
    from  ttiitm001010 T1 ".
    Please give details, as i am new ...

  • How to set datetime datatype in sql server simultaneously

    Hi,
    How can I set date and time simultaneously in datetime field in sql server 2000 using jdbc.
    Thanks

    Actually I'm using datetime datatype in sql server.
    and when I set date than only date gets stored while
    when I set time only time gets stored in database.Hey buddy, go and read the API. In Timestamp your problem will be take care of.

Maybe you are looking for

  • SQL Server Database objects not show

    Hi,to all i do a connection to SQL SERVER with SQL Developer without any problems,but when i search to find table in tables level, i find any things. in SQL SERVER my db is restored from .bak file. in SQL Developer when connect i select my db from re

  • Oracle Determinations Engine Data Source Connector for Batch Processing?

    I have been trying to find +"Oracle Determinations Engine Data Source Connector"+. I have OPA 10.2 installed. Where can I find this in the installation directory? or Does it not come as a part of the standard installation?

  • Cant use my 5.1 Sound in vi

    Hello. i have a Creative XF-I Soundblaster Extreme music card SB0670 Ive recently Updated windows to vista and cant use the 5. sound. Iv'e downloaded the the newest drivers and then im in the Creative Console laucher i cant change my current settings

  • Need to know if soldered

    Hi, I would like to know if any of the components are sautered to the motherboard or the tower. This is because I would like to replace my mobo and case. Thank you,

  • I can't import photos from my iPhone to iPhotos.

    i cant import my iphone pictures to my iphoto. i've tried everything i've already read here like using another user in the same mac but it still wont work. My guess is that one or more of the files are corrupted and cant be imported to iphoto. It has