I have a string "I love McDonald's burgers. it's the best."
and I would like to insert it into a column breaking them into 15 character strings.
Hence I need the result as string inserted in 3 rows
I love McDonald
's burgers. it'
s the best.
But if I use ' ' to include the ', an extra ' is present in the string which will affect my calculation of 15 character breakage.
Is there any other way to include ' without having to use one more ' to escape it?
Please help.
You don't need to add an extra ' if you're breaking the string into a variable:
DECLARE
mcdonald_string VARCHAR2(50) := 'I love McDonald''s burgers. it''s the best.';
BEGIN
WHILE LENGTH(mcdonald_string) > 0 LOOP
INSERT INTO your_table(your_field) VALUES (SUBSTR(mcdonald_string,1,15));
mcdonald_string := SUBSTR(mcdonald_string,16);
END LOOP;
COMMIT;
END;
Doubling the quotation marks within a complicated literal,
particularly one that represents a SQL statement, can be tricky. You
can also use the following notation to define your own delimiter
characters for the literal. You choose a character that is not present
in the string, and then do not need to escape other single quotation
marks inside the literal:
-- q'!...!' notation allows the of use single quotes
-- inside the literal
string_var := q'!I'm a string, you're a string.!';
http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/fundamentals.htm#sthref339
Related
How do I count the number of words in a string with Snowflake? I cant seem to find anything on Google directly.
Try doing this. It assumes that a space separates every word:
SELECT ARRAY_SIZE(SPLIT(string,' '));
This UDF will handle punctuation, multiple spaces between words, line breaks, etc. It's not completely bulletproof, but should work for most use cases.
create or replace function WORD_COUNT(SENTENCE string)
returns number
language sql
as
$$
array_size(split(trim(regexp_replace(regexp_replace(SENTENCE, '[^A-Za-z0-9]', ' '), '[ ]{2,}', ' ')), ' '))
$$;
select word_count('The quick brown--fox jumps over.the;lazy?dog.');
I have a VARCHAR value that looks like this:
5.95 $ Additional fees
How can I remove everything left from character '$' (including that character) ? So that I get the following result:
Additional fees
The '$' is always present.
STUFF and CHARINDEX would be the simpliest way, in my opinion:
SELECT STUFF(YourColumn,1, CHARINDEX('$',YourColumn),'')
FROM (VALUES('5.95 $ Additional fees'))V(YourColumn);
Note that as $ has a whitespace afterwards, the value returned will have a leading whitespace (' Additional fees'). You could use TRIM (or LTRIM and RTRIM on older versions of SQL Server) to remove this, if it isn't wanted.
I haven't assumed that the portion string to be replaced is CHARINDEX('$',YourColumn)+1, as we have one sample. As far as we know, you could also have values such as '10.99$Base Cost'. If the +1 was used, it would return 'ase Cost' for such a value.
Hello do it like below syntax
declare #temp nvarchar(max)='5.95 $ Additional fees'
select SUBSTRING(#temp,charindex('$',#temp)+1,len(#temp)-1)
You can use SUBSTRING get the particular string and CHARINDEX function to get index of special character, in your case $.
DECLARE #Var VARCHAR(100)
SET #Var = '5.95 $ Additional fees'
SELECT SUBSTRING(#Var, CHARINDEX('$', #Var) + 1, LEN(#Var) - LEN(LEFT(#Var, CHARINDEX('$', #Var))))
I need to add comma after every six digits ,but I don't know its length and I can't use loops.
Thanks in advance.
I've tried REGEXP_REPLACE DB2 function, but it doesn't recognize my column as string.
For example , I need to replace "123456123456" with "123456, 123456".
Try this:
select rtrim(xmlcast(xmlquery('fn:replace($s, "([0-9]{6})", "$1, ")' passing str as "s") as varchar(4000)), ', ')
from table(values ('123456123456')) t(str);
I have a database that has multiple columns populated with various numeric fields. While trying to populate from a CSV, I must have mucked up assigning delimited fields. The end result is a column containing It's Correct information, but also contains the next column over's data- seperated by a comma.
So instead of Column UPC1 containing "958634", it contains "958634,95877456". The "95877456" is supposed to be in the UPC2 column, instead UPC2 is NULL.
Is there a way for me to split on the comma and send the data to UPC2 while keeping UPC1 data before the comma in tact?
Thanks.
You can do this with string functions. To query the values and verify the logic, try this:
SELECT
LEFT(UPC1, CHARINDEX(',', UPC1) - 1),
SUBSTRING(UPC1, CHARINDEX(',', UPC1) + 1, 1000)
FROM myTable;
If the result is what you want, turn it into an update:
UPDATE myTable SET
UPC1 = LEFT(UPC1, CHARINDEX(',', UPC1) - 1),
UPC2 = SUBSTRING(UPC1, CHARINDEX(',', UPC1) + 1, 1000);
The expression for UPC1 takes the left side of UPC1 up to one character before the comma.
The expression for UPC2 takes the remainder of the UPC1 string starting one character after the comma.
The third argument to SUBSTRING needs some explaining. It's the number of characters you want to include after the starting position of the string (which in this case is one character after the comma's location). If you specify a value that's longer than the string SUBSTRING will just return to the end of the string. Using 1000 here is a lot easier than calculating the exact number of characters you need to get to the end.
I have a table with details of family members staying in a particular locality. Since these are government data, it has lot mistakes. Like in one column 'houseno', there a 2 values 'Ti 303' and '303' which are same house numbers.
In the end, I want Ti 303 to be updated with '303'. (As these are family members living in same house)
Similarly 'P-101' and 'P/101' are same houseno's and I want it to be converted to either 'P-101' or 'P/101'. I tried difference, substring etc but of now use to me. Please help!
You just need to strip out the characters to compare the content?
CREATE FUNCTION dbo.FN_GetNumberPart (#strMixedString VARCHAR(200))
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE #NumberPart INT
-- Get the next non numeric character position
SET #NumberPart = PATINDEX('%[^0-9]%', #strMixedString)
-- While there are non numeric characters remaining
WHILE #NumberPart > 0
BEGIN
-- Remove the non numeric character from the string
SET #strMixedString = STUFF(#strMixedString, #NumberPart , 1, '' )
-- Get the next non numeric character position
SET #NumberPart = PATINDEX('%[^0-9]%', #strMixedString)
END
-- Spit out the cleansed string
RETURN ISNULL(#strMixedString,0)
END
GO
SELECT dbo.FN_GetNumberPart(HouseNo)
from TblAddresses
You should use the REPLACE command. For the two examples give you could hard code it as follows:
select REPLACE('Ti 303','Ti ','')
select REPLACE('P-101','P-','P/')
You would use REPLACE in your UPDATE command and not as a SELECT obviously.
If you have a list of strings to replace in a column with an update then you could put these into a table. Then use this in your REPLACE command for the string pattern to be replaced.