Salesforce Marketing cloud Automation to remove leading zeros - sql

I have a text field with data- 00000001000 and -00000001000
I am trying to remove all the leading zeros from string field.
I tried using REPLACE function:
REPLACE(LTRIM(REPLACE([dollarbalance], '0', ' ')), ' ', '0')
...but it is not giving expected result.
In Query Studio query runs but in Automation it is failing. Also in Query Studio minus sign is not getting ignored and I am getting the same result for value -00000001000
Thanks
I tried
REPLACE(LTRIM(REPLACE([dollarbalance], '0', ' ')), ' ', '0')
and
substring(DOLLARBALANCE, patindex('%[^0]%',DOLLARBALANCE),5)

Since you haven't provided any details about the datatypes inbound or outbound, I'd suggest experimenting outside of SFMC, since this really isn't a question specific to the platform.
declare #dollarbalance varchar(50) = '- 000000000'
select
#dollarbalance dollarbalanceRaw
, REPLACE(LTRIM(REPLACE(#dollarbalance, '0', ' ')), ' ', '0') dollarbalanceTemp
, convert(decimal(19,2),#dollarbalance) dollarbalanceDec
, convert(decimal(19,0),#dollarbalance) dollarbalanceNoDec
dollarbalanceRaw
dollarbalanceTemp
dollarbalanceDec
dollarbalanceNoDec
- 000000000
-0000000000
0.00
0
Try it yourself: fiddle
Reference:
Convert function

Related

Replace some parts of string and update the column in STANDARD SQL

I've a year column in a table called 'Movie', some of the year values are not in the standard format(few of them are like 'I 1999', 'XV 199', etc.(space is common in those years)). So I wanted to remove ' ', 'X', 'V', 'I' and replace them with '' so that I can convert them to the standard format of the year.
I was able to do the replacing part by this command:
SELECT (REPLACE(REPLACE(REPLACE((REPLACE(year, ' ', '')),'X',''),'V',''),'I','')) FROM Movie
But I'm unable to update my column(Getting error, command not executed). Below is the command I tried.
UPDATE Movie
SET year = (REPLACE(REPLACE((REPLACE(year, ' ', '')),'X',''),'V',''),'I',''))
WHERE year LIKE '% %'
Please let me know how can I update my 'year' column. Please note I can only use STANDARD SQL commands.
Update: Attached the screenshot of the error. I am executing the SQL commands using pandas.read_sql_query() method. All standard SQL commands works nicely. But error encountered in this case.please note I've removed extra parenthesis.
You are overusing parentheses, so something does not match.
Use this:
SET year = REPLACE(REPLACE(REPLACE(REPLACE(year, ' ', ''), 'X', ''), 'V', ''), 'I', '')

SQL I need to extract a stored procedure name from a string

I am a bit new to this site but I have looked an many possible answers to my question but none of them has answered my need. I have a feeling it's a good challenge. Here it goes.
In one of our tables we list what is used to run a report this can mean that we can have a short EXEC [svr1].[dbo].[stored_procedure] or "...From svr1.dbo.stored_procedure...".
My goal is to get the stored procedure name out of this string (column). I have tried to get the string between '[' and ']' but that breaks when there are no brackets. I have been at this for a few days and just can't seem to find a solution.
Any assistance you can provide is greatly appreciated.
Thank you in advance for entertaining this question.
almostanexpert
Considering the ending character of your sample sentences is space, or your sentences end without trailing ( whether space or any other character other than given samples ), and assuming you have no other dots before samples, the following would be a clean way which uses substring(), len(), charindex() and replace() together :
with t(str) as
(
select '[svr1].[dbo].[stored_procedure]' union all
select 'before svr1.dbo.stored_procedure someting more' union all
select 'abc before svr1.dbo.stored_procedure'
), t2(str) as
(
select replace(replace(str,'[',''),']','') from t
), t3(str) as
(
select substring(str,charindex('.',str)+1,len(str)) from t2
)
select
substring(
str,
charindex('.',str)+1,
case
when charindex(' ',str) > 0 then
charindex(' ',str)
else
len(str)
end - charindex('.',str)
) as "Result String"
from t3;
Result String
----------------
stored_procedure
stored_procedure
stored_procedure
Demo
With the variability of inputs you seem to have we will need to plan for a few scenarios. The below code assumes that there will be exactly two '.' characters before the stored_procedure, and that [stored_procedure] will either end the string or be followed by a space if the string continues.
SELECT TRIM('[' FROM TRIM(']' FROM --Trim brackets from final result if they exist
SUBSTR(column || ' ', --substr(string, start_pos, length), Space added in case proc name is end of str
INSTR(column || ' ', '.', 1, 2)+1, --start_pos: find second '.' and start 1 char after
INSTR(column || ' ', ' ', INSTR(column || ' ', '.', 1, 2), 1)-(INSTR(column || ' ', '.', 1, 2)+1))
-- Len: start after 2nd '.' and go until first space (subtract 2nd '.' index to get "Length")
))FROM TABLE;
Working from the middle out we'll start with using the SUBSTR function and concatenating a space to the end of the original string. This allows us to use a space to find the end of the stored_procedure even if it is the last piece of the string.
Next to find our starting position, we use INSTR to search for the second instance of the '.' and start 1 position after.
For the length argument, we find the index of the first space after that second '.' and then subtract that '.' index.
From here we have either [stored_procedure] or stored_procedure. Running the TRIM functions for each bracket will remove them if they exist, and if not will just return the name of the procedure.
Sample inputs based on above description:
'EXEC [svr1].[dbo].[stored_procedure]'
'EXEC [svr1].[dbo].[stored_procedure] FROM TEST'
'svr1.dbo.stored_procedure'
Note: This code is written for Oracle SQL but can be translated to mySQL using similar functions.

How to remove the begining and ending characters if those are '0' in SQL Server

I currently have a table named DATA it has entries like the following:
abc000
ab000cde
000abc
I just want to remove all 0 from beginning and the end. If 0 comes in between the character then it will remain same.
This also works for leading and trailing zeros at the same time:
declare #s varchar(15) = '00abc00efg000'
select substring(#s,
patindex('%[^0]%', #s),
len(#s)-patindex('%[^0]%', reverse(#s))-patindex('%[^0]%', #s)+2);
Description: this is substring from first nonzero symbol till first nonzero symbol in reversed string.
Say your data exists in column called Col1, then this expression should do it
select CASE
WHEN RIGHT(col1 , 1) = '0'
THEN SUBSTRING(col1,0,PATINDEX('%[A-Z1-9]%',REVERSE(col1)))
WHEN LEFT(col1 , 1) = '0'
THEN SUBSTRING(col1,PATINDEX('%[A-Z1-9]%',col1),LEN(col1))
ELSE
Col1
END AS 'ParsedCol1'
FROM Data
I use this trick:
SELECT
REPLACE(REPLACE(RTRIM(LTRIM(REPLACE(REPLACE(col1, ' ', CHAR(8)), '0', ' '))), ' ', '0'), CHAR(8), ' ')
FROM
yourTable
This may works for SQL, as this removes leading & trailing "000" from your string.
SELECT TRIM(BOTH ‘000’ FROM
‘your_data_table_column’);
See more
http://www.mydigitallife.info/remove-or-trim-first-or-last-few-characters-in-mysql-database-with-sql/
http://www.w3resource.com/mysql/string-functions/mysql-trim-function.php

REPLACE not working on sql table column

I've tried select REPLACE(' this is a user name', ' ', '') and it gives me 'thisisausername' which is supposed to be.
My problem is, when I try to use REPLACE on selecting a table column, it doesn't work!
My query:
SELECT REPLACE(UserName, ' ', '') as UserName FROM MY_TABLE
it still gives me usernames with spaces! Am I doing something stupid?
#AlexK. it's 160 for unicode(left(field, 1))
160 is Unicode NO-BREAK SPACE so that's what you need to replace:
replace(UserName, char(160), '')
You could update everything replacing char(160) with a whitespace ' ' and then just use your original query in the future (perhaps also ensuring such values cannot be entered in the future)

SQL query for search criteria

In my table there are codes which are basically decimal values. For example the code column contains data like 001.0, 00.10, 002.0, 00.20 etc. I have to write a SQL query which shows following result while searching the code values from UI:
If the user searches 0010, I have to show all the values 001.0, 00.10
if the user searches 001.0, I have to show values like 001.0, 001.1 etc.
if the user searches 00.10, I have to show value 00.10, 00.12 etc.
Please help me in writing SQL query for the above criteria.
Not everything is entirely clear about your rules, but I hope that the following will serve you as a passable starting point at least.
Apparently, the search term should be processed in two ways, depending on whether it contains the decimal point. So first you need to know whether the point is present or not. That could be done using the CHARINDEX() function: if it returns 0, the decimal point is absent from the search term, otherwise it is there. Thus, your complete condition will be structured like this:
WHERE CHARINDEX('.', #SearchTerm) = 0 AND … /* condition to match '0010'-like input */
OR CHARINDEX('.', #SearchTerm) > 0 AND … /* condition to match input containing '.' */
The condition to search for the term without . might look like this:
REPLACE(Code, '.', '') = #SearchTerm /* or maybe LIKE #SearchTerm + '%'? */
The condition for the case when the search term does contain . seems to me trickier than the previous one, but anyway, here's what I came up with:
Code LIKE REPLACE(RTRIM(REPLACE(#SearchTerm, '0', ' ')), ' ', '0') + '%'
How it works, step by step, using '001.0' and '00.10' as examples:
Initial value '001.0' '00.10'
REPLACE(#SearchTerm, '0', ' ') ' 1. ' ' .1 '
RTRIM(REPLACE(#SearchTerm, '0', ' ')) ' 1.' ' .1'
REPLACE(RTRIM(REPLACE(#SearchTerm, '0', ' ')), ' ', '0') '001.' '00.1'
REPLACE(RTRIM(REPLACE(#SearchTerm, '0', ' ')), ' ', '0') + '%' '001.%' '00.1%'
So, as you can see, the expression evaluates to a mask that should satisfy your rules for search terms containing ..
Pulling all the conditions together, we get the following WHERE clause:
WHERE CHARINDEX('.', #SearchTerm) = 0 AND REPLACE(Code, '.', '') = #SearchTerm
OR CHARINDEX('.', #SearchTerm) > 0 AND
Code LIKE REPLACE(RTRIM(REPLACE(#SearchTerm, '0', ' ')), ' ', '0') + '%'