I want to replace an empty string in BigQuery with a '0'. I can't get it to work nohow.
create temp table doodle_ns
(col_with_ns string)
;
insert doodle_ns select '';
select replace(col_with_ns, '', '0')
from doodle_ns;
Result set shows a blank value:
Firstly, read comment by Jeremy
Meantime, I feel what you really want is something like below
select * replace (
if(col_with_ns = '', '0', col_with_ns) as col_with_ns
)
from doodle_ns
Funny, cause this does work...
select regexp_replace(col_with_ns, '', '0')
from doodle_ns;
I wonder if this is actually a bug.
Related
I have column called File with values 'Mens_Purchaser_Segment_Report' and 'Loyalist_Audience_Segment_Report'. I want to capture everything that comes before word Segment.
I used query:
select
TRIM(file,regexp_substr(file, '_Segment_Report.*')) as new_col
Output:
Mens_Purch
Loyalist_Audi
How do I capture everything before Segment?
Tried below but same results-->
TRIM(file,regexp_substr(file, 'S.*'))
TRIM(file,regexp_substr(file, '_S.*'))
You didn't specify if the trailing text is always _Segment_Report, you're asking for any text before _Segment. Depending on that various solutions can be used, see below.
create or replace table foo(s string) as select * from values
('Mens_Purchaser_Segment_Report'),
('Loyalist_Audience_Segment_Report');
-- If you know the suffix you want to remove is always exactly '_Segment_Report'
select s, replace(s, '_Segment_Report', '') from foo;
-- If you know the suffix you want to remove starts with '_Segment' but can have something after
-- - approach 1, where we replace the _Segment and anything after it with nothing
select s, regexp_replace(s, '_Segment.*', '') from foo;
-- - approach 2, where we extract things before _Segment
-- Note: it will behave differently if there are many instances of '_Segment'
select s, regexp_substr(s, '(.*)_Segment.*', 1, 1, 'e') from foo;
try
using regexp_replace
select regexp_replace(fld1, 'Segment', '') from (
select 'Mens_Purchaser_Segment_Report and Loyalist_Audience_Segment_Report' fld1 from dual );
When I compared text like this:
'a' = 'a '
the result is True - but I was expecting it to be false.
Do you know why the result is true? And can I do something to solve this issue?
The LIKE operator does not pad the trailing spaces:
SELECT * FROM basePath WHERE 'a' LIKE 'a '
More information can be found here: https://dba.stackexchange.com/questions/56876/sql-server-auto-trim-of-varchar-value-in-equal-comparison-but-not-like-compariso
You can use DATALENGTH()
Example
SELECT * FROM TableName WHERE DATALENGT('a') = DATALENGTH('a ')
DATALENGT('a') return the result as 1
and DATALENGTH('a ') return the result as 2, includes the space text.
Please check the db<>fiddle for reference.
I have a SQL query which includes the line:
WHERE
[TraceableItem].[IdentificationNo] LIKE N'015933%'
I would like this to match the following numbers:
015933
00015933
000000000015933
But not allow any non-zero characters. How could I do this?
--Some test data
DECLARE #sample TABLE
(
number_as_string VARCHAR(20)
)
INSERT INTO #sample
VALUES
('015933') -- okay
,('00015933') -- okay
,('000000000015933')-- okay
,(' 00015933') -- dont return as this doesnt start with a 0
,('25') -- dont return wrong number
,('string') -- dont return as its a string
,('st15933') -- dont return as it starts with a string.
,('001000015933') -- dont return as this is the number 1000015933
SELECT
*
FROM
#sample as s
WHERE
--only consider rows that are a number
--stops CONVERT exception being thrown on lines that do no convert
ISNUMERIC(s.number_as_string) = 1
AND
--Convert to INT wipes out the leading 0's, but also spaces
CONVERT(INT,s.number_as_string) LIKE '15933%'
AND
--must start with a number, i.e. check it doesn't start with a space.
--LEFT(s.number_as_string,1) NOT LIKE '[^0-9]'
--This version is easier to read as its not a double NOT logic like the version above
--Thanks to #Robert Kock
LEFT(s.number_as_string,1) BETWEEN '0' AND '9'
Gives the result
number_as_string
----------------
015933
00015933
000000000015933
You probably want to first convert to int and back to string as suggested by Neeraj Agarwal. But then take the left five characters and compare for exact equality to '15933'
where '15933' = left(convert(varchar(50),convert(int,
TraceableItem.IdentificationNo
)),5)
You can see it at work in the sample below, where it captures everything you desire and a little more, but doesn't capture the case presented by Harry Adams in the comments to Neeraj.
select *
from (values
('015933'),
('00015933'),
('000000000015933'),
('0001593399'),
('15933'),
('001000015933')
) vals (v)
where '15933' = left(convert(varchar(50),convert(int, v)),5)
I don't like converting to a number for this purpose. But one method is to "trim" the leading zeros away. For an exact match:
where replace(ltrim(replace([TraceableItem].[IdentificationNo], '0', ' ')), ' ', '0') = '15933'
For LIKE:
where replace(ltrim(replace([TraceableItem].[IdentificationNo], '0', ' ')), ' ', '0') LIKE '15933%'
You can also express this with LIKE/NOT LIKE:
where TraceableItem].[IdentificationNo] like '%15933%' and
TraceableItem].[IdentificationNo] not like '%[^0]%15933%'
You can use cast to convert to an int and back to a character string provided the string consists of digits only, e.g.:
select cast(cast("00015933" as int) as varchar(24))
I have strings like these:
JAPANNO
CHINANO
BROOKLYNNO
I want to delete the 'NO' from all of the strings. I tried this:
rtrim(string, 'NO')
but for example in the case of BROOKLYNNO, I got this:
BROOKLY.
It deletes all the N-s from the end. How can I delete just the pattern of 'NO'?
I know I can do it with substr, but the TechOnTheNet says there is a way to delete a pattern with RTRIM, and I really want to know the way.
Thank you in advance!
We may consider doing a regex replacement via REGEXP_REPLACE, if you give a context for when NO should be removed and when it should not. For example, if you wanted to remove NO from the ends of your strings only, we could do the following:
UPDATE yourTable
SET col = REGEXP_REPLACE(col, 'no$', '', 1, 0, 'i');
You could use TRIM(TRAILING ... FROM):
SELECT col_name,
REPLACE(TRIM(TRAILING '^' FROM REPLACE(col_name, 'NO', '^')), '^', 'NO') AS res
FROM tab;
DBFiddle Demo
Have a look at this, maybe?
declare #string varchar(150) = 'BROOKLYNNO'
select LEN(#string)
select LEFT(#string,(LEN(#string)-2))
You can then update your column with the output from the final select statement, which trims the last two letters from the string.
I suppose it might be worth asking how you're getting the data that you have here, strings appended with "NO"?
i have 10 table and more than 10000 record that contain 
how can search  and replace this in DB?
since the  equal 0xEF,0xBB,0xBF how can search this?
i use this code
WITH foo(myvarbincolumn) AS
(
SELECT text from BPM_Letters
)
SELECT *
FROM foo
WHERE CONVERT(VARCHAR(max), myvarbincolumn) COLLATE Arabic_CI_AS
LIKE '%' + CONVERT(NVARCHAR(max), CHAR(0xEF)+CHAR(0xBB)+CHAR(0xBF)) + '%'
I found this code in stackoverflow but it s incomplete.
script of BPM_Letters
this code not find any record!
please help me
I wrote a query to find that weird character via the query below:
SELECT cast(LEFT(text,1) AS VARBINARY(MAX)) from BPM_Letters
and the result was 0xFFFE. So I wrote this query and it worked perfectly:
UPDATE BPM_Letters Set text=REPLACE(text,0xFFFE,'');
What about this CTE:
StripBOM AS
(
SELECT CASE
WHEN LEFT(text,3) = 0xEFBBBF
THEN CONVERT(varbinary(max),SUBSTRING(text, 4, LEN(text)))
ELSE text
END AS text
FROM BPM_Letters
)
It should provide you with a new table where all BOM characters have been stripped off.
P.S. This code assumes 'text' field is of type varbinary.
Here's a simpler answer that builds on the other ones:
UPDATE BPM_Letters SET text=substr(text, 4) WHERE left(text, 3) = 0xEFBBBF;
I've tested this, and it works.