how to find same two special character in a fields in impala - hive

I have a case statement which derive specific portion of string field based on the condition.
example:
customer_id
7115878969-127881041-000000
650030087-1
130715315-1
2956599329-154074046-000000
127243855-1
Please find my current code below:
case
when substr(reverse(trim(customer_id)),1,instr(reverse(trim(customer_id)),'-')-1)='000000'
then split_part(customer_id, '-', 2)
when substr(reverse(trim(customer_id)),1,instr(reverse(trim(customer_id)),'-')-1)<>'000000'
then trim(substr(customer_id,1,instr(customer_id,'-')-1))
--else 'error'
end as PN
,
it check right side'000000'. but I need to change the condition based on the"-"
if two "-" then 127881041 middle portion of two"-"
if one "-" then 650030087
Thanks

Related

REPLACE not doing what I need in SQL

I've got a query pulling data from a table. In one particular field, there are several cases where it is a zero, but I need the four digit location number. Here is where I'm running into a problem. I've got
SELECT REPLACE(locationNbr, '0', '1035') AS LOCATION...
Two issues -
Whoever put the table together made all fields VARCHAR, hence the single quotes.
In the cases where there already is the number 1035, I get 1103535 as the location number because it's replacing the zero in the middle of 1035.
How do I select the locationNbr field and leave it alone if it's anything other than zero (as a VARCHAR), but if it is zero, change it to 1035? Is there a way to somehow use TO_NUMBER within the REPLACE?
SELECT CASE WHEN locationNbr='0' THEN '1035' ELSE locationNbr END AS LOCATION...
REPLACE( string, string_to_replace , replacement_string )
REPLACE looks for a string_to_replace inside a string and replaces it with a replacent_string. That is why you get the undesired behaviour - you are using the wrong function.
CASE WHEN condition THEN result1 ELSE result2 END
CASE checks a condition and if it is true it returns result1 and if it is not it will return result2. This is a simple example, you can write a case statement with more than one condition check.
Don't use replace(). Use case:
(case when locationNbr = '0' then '1035' else locationNbr end)
You can make use of length in Oracle:
select case when length(loacation) = 1 then REPLACE(loacation, '0', '1035') else loacation end as location
from location_test;

Error with substring seperating data by char(10)+char(13) line breaks

What I am attempting to do is separate my data by the line breaks into separate fields: Attn, Addr1Field, Addr2Field. I have found the location of both of the line breaks but the difference isn't the same for every row of data so I'm using the expression as the third option in my Substring() function. I'm getting the error Invalid length parameter passed to the LEFT or SUBSTRING function. In the attached image I hardcoded 34-20 to get my desired results but each row has the possibility to be different so I need to be able to use the expression.
Select
case
when LEFT(CONVERT(VARCHAR(MAX), soship.fmstreet),5)='ATTN:'
Then SUBSTRING(CONVERT(VARCHAR(MAX), soship.fmstreet), 7,CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet))-1)
Else ' '
End as AttnField,
case
when LEFT(CONVERT(VARCHAR(MAX), soship.fmstreet),5)='ATTN:'
Then SUBSTRING(CONVERT(VARCHAR(MAX), soship.fmstreet),CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet))+2,CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet),CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet))+1) -
CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet)))
Else ''
End as Addr1Field
My charindex was returning 0 so I wrote a Where clause to solve it.
Where
(CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet),CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet))+1) -
CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet)) > 0)

Concatenate inside SELECT CASE

I am moving a customer database which has a First Name, Last Name and Company Name. The current data will either have a First & Last Name or a Company name
In the new database I have a "Print Name" field which I need to combine the first and last name into, or if there is a company name present I need to use this value.
I have tried the bellow CASE WHEN expression to no avail.
SELECT
fstnam,
lstnam,
cmpnam,
CASE
WHEN csttbl.cmpnam = null THEN fstnam||' '||lstnam
ELSE csttbl.cmpnam
END as PrintName
FROM csttbl;
With the above query I only have a company name return in the print name column, not the combined First & Last Name. the returned data can be seen here https://drive.google.com/open?id=0B31ZRmFSX6rMYmNOczlRNE1lM2c
Any help would be appreciated
Change
csttbl.cmpnam = null
To
csttbl.cmpnam is null
RDBMS cannot compare null to any value, so using = with null is of no use. To compare nulls you should use is null which works on almost all RDBMS.
I would use coalesce():
SELECT fstnam, lstnam, cmpnam,
COALESCE(csttbl.cmpnam, fstnam|| ' ' || lstnam) as PrintName
FROM csttbl;
Didn't think of just concatonating the whole string as there is no First & Last name if there is a company name. COALESCE doesn't seem to work for me though so did the below
SELECT
fstnam,
lstnam,
cmpnam,
csttbl.cmpnam||fstnam|| ' ' ||lstnam as PrintName
FROM csttbl;

DB2 SQL - How can I display nothing instead of a hyphen when the result of my case statement is NULL?

All,
I'm writing a query that includes a CASE statement which compares two datetime fields. If Date B is > Date A, then I'd like the query to display Date B. However, if Date B is not > Date A, then the user who will be getting the report created by the query wants the column to be blank (in other words, not contain the word 'NULL', not contain a hyphen, not contain a low values date). I've been researching this today but have not come up with a viable solution so thought I'd ask here. This is what I have currently:
CASE
WHEN B.DTE_LNP_LAST > A.DTE_PROC_ACT
THEN B.DTE_LNP_LAST
ELSE ?
END AS "DATE OF DISCONNECT"
If I put NULL where the ? is, then I get a hyphen (-) in my query result. If I omit the Else statement, I also get a hyphen in the query result. ' ' doesn't work at all. Does anyone have any thoughts?
Typically the way nulls are displayed is controlled by the client software used to display query results. If you insist on doing that in SQL, you will need to convert the date to a character string:
CASE
WHEN B.DTE_LNP_LAST > A.DTE_PROC_ACT
THEN VARCHAR_FORMAT(B.DTE_LNP_LAST)
ELSE ''
END AS "DATE OF DISCONNECT"
Replace VARCHAR_FORMAT() with the formatting function available in your DB2 version on your platform, if necessary.
You can use the coalesce function
Coalesce (column, 'text')
If the first value is null, it will be replaced by the second one.

SQL - Conditionally joining two columns in same table into one

I am working with a table that contains two versions of stored information. To simplify it, one column contains the old description of a file run while another column contains the updated standard for displaying ran files. It gets more complicated in that the older column can have multiple standards within itself. The table:
Old Column New Column
Desc: LGX/101/rpt null
null Home
Print: LGX/234/rpt null
null Print
null Page
I need to combine the two columns into one, but I also need to delete the "Print: " and "Desc: " string from the beginning of the old column values. Any suggestions? Let me know if/when I'm forgetting something you need to know!
(I am writing in Cache SQL, but I'd just like a general approach to my problem, I can figure out the specifics past that.)
EDIT: the condition is that if substr(oldcol,1,5) = 'desc: ' then substr(oldcol,6)
else if substr(oldcol,1,6) = 'print: ' then substr(oldcol,7) etc. So as to take out the "desc: " and the "print: " to sanitize the data somewhat.
EDIT2: I want to make the table look like this:
Col
LGX/101/rpt
Home
LGX/234/rpt
Print
Page
It's difficult to understand what you are looking for exactly. Does the above represent before/after, or both columns that need combining/merging.
My guess is that COALESCE might be able to help you. It takes a bunch of parameters and returns the first non NULL.
It looks like you're wanting to grab values from new if old is NULL and old if new is null. To do that you can use a case statement in your SQL. I know CASE statements are supported by MySQL, I'm not sure if they'll help you here.
SELECT (CASE WHEN old_col IS NULL THEN new_col ELSE old_col END) as val FROM table_name
This will grab new_col if old_col is NULL, otherwise it will grab old_col.
You can remove the Print: and Desc: by using a combination of CharIndex and Substring functions. Here it goes
SELECT CASE WHEN CHARINDEX(':',COALESCE(OldCol,NewCol)) > 0 THEN
SUBSTRING(COALESCE(OldCol,NewCol),CHARINDEX(':',COALESCE(OldCol,NewCol))+1,8000)
ELSE
COALESCE(OldCol,NewCol)
END AS Newcolvalue
FROM [SchemaName].[TableName]
The Charindex gives the position of the character/string you are searching for.
So you get the position of ":" in the computed column(Coalesce part) and pass that value to the substring function. Then add +1 to the position which indicates the substring function to get the part after the ":". Now you have a string without "Desc:" and "Print:".
Hope this helps.