Pulling data out of a text field - sql

I have a notetext field that will have 2 fields in it: price and desc
The price is never the same amount of characters and is in text format
Example: (51445 text description) or (9801 text description)
How do I set it up to pull the amount out and then have it formatted with 2 decimals?
I have tried: LEFT(note_text,CHARINDEX(' ', note_text) - 1) AS Incoming_Cust_Prc but this isn't working. I am getting an error
ORA-00904 LEFT invalid identifier

The error message suggests that the DBMS is Oracle rather than SQL Server in which there are functions such as LEFT() and CHARINDEX(). Thus, use Oracle functions as
SELECT TO_CHAR(SUBSTR(note_text,1,INSTR(note_text,' ')), 'fm999G999D00') AS Incoming_Cust_Prc,
SUBSTR(note_text,INSTR(note_text,' ')+1,LENGTH(note_text)) AS Incoming_Cust_Desc
FROM t
in order to get two seperate columns price and description.
Demo

Related

How to circumvent error Message 241 (Conversion failed when converting date and/or time from character string)

I am simply trying to sort by my acquisition date column that has dates in this type of format: '04/02/2019' etc... I have created and column named ACQ_DATE_CONVERTED in an new table and I usual get the results such as: 2019-04-02. Instead I am getting the
error Message 241 (Conversion failed when converting date and/or time from character string)
I have tried the following:
SELECT [ITEM], [ACQ_DATE],
CONVERT(DATE,[ACQ_DATE]) AS ACQ_DATE_CONVERTED
,' ' AS MFG
INTO [ABC].[dbo].[My_Store_records_CONVERTED]
FROM [ABC].[dbo].[My_Store_records]
After my results I then order by ACQ_Date_Converted.
SELECT [ITEM], [ACQ_DATE]
FROM [ABC].[dbo].[My_Store_records_CONVERTED]
ORDER by [ACQ_DATE_CONVERTED]
My expected results should look something like this in the table:
Column_A Column_B ColumnC
Rows ITEM ACQ_DATE ACQ_DATE_CONVERTED
1. ITEM_1 04/09/2007 2007-04-09
2. Store item 01/26/2008 2008-01-26
etc...
Can you please try the following query:
REPLACE(CONVERT(VARCHAR(10), [ACQ_DATE], 111), '/', '-') AS ACQ_DATE_CONVERTED
Since you are converting from ACQ_DATE, so you can use ORDER BY [ACQ_DATE], it will work.
Demo on DB Fiddle
Thank you everyone that helped with input for this issue.
I discovered that I had a hidden character in all of the cells for the column I wanted to ORDER by; therefore, causing every line to fail. However, with the below I was able to overcome this stumbling block. I hope that anyone having this issue will find my solution below helpful.
REMOVING THE HIDDEN CHARACTER.
, REPLACE([ACQ_DATE], CHAR(10),'') AS [ACQ_DATE_CONVERTED]
,' ' AS MFG
INTO [ABC].[dbo].[My_Store_records_CONVERTED]
FROM [ABC].[dbo].[My_Store_records]
and sorting the new table by ACQ_DATE_Converted;
ALTER TABLE [ABC].[dbo].[My_Store_records_CONVERTED] ALTER COLUMN [ACQ_DATE_CONVERTED] DATE NULL;
and then of course;
ORDER by [ACQ_DATE_CONVERTED]
Best Regards,
Dex

SQL Query to select a value between two known strings

I need a SQL query to get the value between two known strings in a text column.
The column name is d_info and the table name is Details.
The text is an XML fragment, but stored as a text value.
What I need is to get the value between the bookends <nettoeinkommen> and </nettoeinkommen> which is 718 in this example.
I also need the output to be saved in new column named income with data type float(8).
land>DE</land></wohnanschrift><taetigkeit>rentner</taetigkeit><dkbkundenstatus><bestandskunde>false</bestandskunde></dkbkundenstatus><haushaltsangaben><einnahmen><einkommen><nettoeinkommen>718</nettoeinkommen></einkommen><kindergeld>0</kindergeld><vermietungverpachtungnetto>0</vermietungverpachtungnetto><elterngeld>0</elterngeld><rentenunbefristet>0</rentenunbefristet><unselbststaendigetaetigkeit>740</unselbststaendigetaetigkeit><geringfuegigebeschaeftigung>0</geringfuegigebeschaeftigung></einnahmen><ausgaben><warmmiete>550</warmmiete><ratenimmobilienfinanzierung>0</ratenimmobilienfinanzierung>
I tried this code:
SELECT cast(SUBSTRING(d_info, CHARINDEX('<nettoeinkommen>', d_info)
, CHARINDEX('</nettoeinkommen>', d_info) - CHARINDEX('<nettoeinkommen>', d_info)) as float(8)) as income
from dbo.Details
But it's returning an Error converting data type varchar to real.
When I remove the cast function, the script works but it returns <nettoeinkommen>718 instead of only 718.
Thanks.
It is starting at the start of the tag not the end of it.
SELECT cast(
SUBSTRING(
d_info,
CHARINDEX('<nettoeinkommen>', d_info) + len('<nettoeinkommen>'),
CHARINDEX('</nettoeinkommen>', d_info) - (CHARINDEX('<nettoeinkommen>', d_info) + len('<nettoeinkommen>'))
) as float(8)) as income
from dbo.Details
you might even have these defined in variables:
SELECT cast(
SUBSTRING(
d_info,
CHARINDEX(#startTag, d_info) + len(#startTag),
CHARINDEX(#endTag, d_info) - (CHARINDEX(#startTag,d_info)+ len(#startTag))
) as float(8)) as income
from dbo.Details
I think the code is much easier to understand with the variables.
You need to add the length of your opening tag from the start index and subtract from the length of your substring statement:
SUBSTRING(d_info, CHARINDEX('<nettoeinkommen>', d_info)+16,
CHARINDEX('</nettoeinkommen>', d_info) - CHARINDEX('<nettoeinkommen>', d_info)-16)
As it seems, you are querieing plain xml data, for such purpose sql-server provides xquery functionality:
SELECT CAST(r.d_info AS XML).value('(/haushaltsangaben/einnahmen/einkommen/nettoeinkommen)[1]', 'decimal(19,2)')
FROM
(
SELECT '<taetigkeit>rentner</taetigkeit>
<dkbkundenstatus>
<bestandskunde>false</bestandskunde>
</dkbkundenstatus>
<haushaltsangaben>
<einnahmen>
<einkommen>
<nettoeinkommen>718</nettoeinkommen>
</einkommen>
</einnahmen>
</haushaltsangaben>' AS d_info
) AS r
If you intend to query more info from your source, you will end up with a bunch of stacked substring, patindex functions or even your own defined functions. This should be more readable and mantainable.
Using XQuery: https://learn.microsoft.com/en-us/sql/t-sql/xml/query-method-xml-data-type
As for your initial issue The SUBSTRING function in SQL returns the subset from a string starting from a given index for a specific length. For example SELECT SUBSTRING('whatever',5,4) returns 'ever'.
In case of CHARINDEX it gives the index for the first found match of a given pattern within a string. Example SELECT CHARINDEX('ever','whatever') should return 5, as 'ever' starts at the fifth position in 'whatever').
Now in your case you need to add the length of '<nettoeinkommen>' to the starting charindex and substract the length of '</nettoeinkommen>' from the length of the substring:
Also consider using decimal or numeric type instead of float, if you need to precise calculations: https://technet.microsoft.com/en-us/library/ms187912(v=sql.105).aspx

How to check remove numbers from a string?

I'm using an SSIS package to bring data through data from one table to another. However, I have a predicament where a field in the table(GroupName) brings through data with numbers at the end. This comes in two forms, either the string will be a name and then a set of numbers less than 4 characters in length. (E.g - Group Name 22)
Or it will come as a name and four numeric characters. (E.g Group Name 2012). Now I'd like to do a check on the data in SQL to see if the length of numeric characters at the end of the string is less than 4. If so, remove the numbers.
Can anyone help
You can use patindex
SELECT
SUBSTRING('Group Name 2012'
,PATINDEX('%[0-9]%'
,'Group Name 2012')
,LEN('Group Name 2012')) as NumberOnly
,LEN( SUBSTRING('Group Name 2012'
,PATINDEX('%[0-9]%'
,'Group Name 2012')
,LEN('Group Name 2012'))) as Numberlength
Alternatively add a Derived column transformation with
NumericCheck= RIGHT(stringvariable,4)
and then in a separate Derived column transformation
(DT_I4)Numeric_check == (DT_I4)Numeric_check ? 1 : 0
Note: You will need to Configure the Error output to "Ignore Failure" for this check. Then have a conditional split which sends the zero values to be updated via an OLE DB Command

Locate Cause of IBM DB2 CAST Failure

I need to work on an IBM DB2 database.
The LOCATION field is a CHARACTER(8) field of numbers.
To sort the table, the column is cast to an INTEGER:
SELECT LOCATION, PARTNO, INSTOCK
FROM INVENTORY
ORDER BY CAST(LOCATION AS INTEGER)
Currently, this fails with:
ERROR [22018] [IBM][DB2/AIX64] SQL0420N Invalid character found in a character string argument of the function "INTEGER".
Is there a quick way to determine which row is failing?
IBM's solution is to "Insure that the results set for the query item that the cast it being applied to does not contain non numeric SQL constants when casting to a numeric type."
That wasn't really helpful.
Thinking someone inserted a letter O or lower case L, I tried this:
SELECT DISTINCT LOCATION
FROM LOCATIONS
WHERE LOCATION LIKE '%l%' OR LOCATION LIKE '%O%'
ORDER BY LOCATION
Zero records returned.
That wasn't really helpful.
That's IBM error messages and documentation in a nutshell.
One place to start is the TRANSLATE() function.
SELECT LOCATION, PARTNO, INSTOCK
FROM INVENTORY
WHERE TRANSLATE(LOCATION, '', ' 0123456789') <> ''
You can add other characters, like -, ., etc. depending on what you find.

convert text to number in ms access 2010 select statement

I need to run a query on a massive (650 MB) data table in MS Access 2010. All the data types for the fields are text, but some fields contain all numbers, even though the data type is text. I therefore have to convert from text to long int in order to select, for example, all the records who have a value greater than 10 in a specified field. But whenever I do this, I get a type mismatch error, presumably due to the comparison of a numerical value of a text field. Here is the SQL for the query which should select from fields that are all of the text data type.
SELECT [Organization legal name], [Number of Group Practice members], City, State
FROM massivetable
WHERE Clng([Number of Group Practice members])>10
AND State='CT';
I have tried to convert the datatype of the field to long integer in design view, but the operation fails with a message saying there is not enough memory to perform the operation.
Can anyone show me how to fix the code above so that it selects all the records while values greater than 10, without throwing a type conversion error?
Use IsNumeric function:
SELECT [Organization legal name], [Number of Group Practice members], City, State
FROM massivetable
WHERE Iif(IsNumeric([Number of Group Practice members]), Clng([Number of Group Practice members]), 0) >10
AND State='CT';