I've this request for substitute a character string like
SELECT
REPLACE (REPLACE ( Dcpt.[desc], 'Compañía', 'Compañía /'), 'Producción', ' Producción /' )
FROM Dcpt
That works fine but sometimes the end user wrote Producción with the single letter o without accent. I tried to replace the letter ó with the meta character _ or %. It didn't solve my issue.
_ and % have no significance in REPLACE. These are only used by LIKE and PATINDEX.
Use an accent insensitive COLLATE clause (AI) to have o and ó treated interchangeably
SELECT
REPLACE (REPLACE ( Dcpt.[desc] COLLATE Latin1_General_100_CS_AI, N'Compañía', N'Compañía /'), N'Producción', N' Producción /' )
FROM Dcpt
Change CS to CI if you also want case insensitivity.
Try Follow:
SELECT
REPLACE (REPLACE ( Dcpt.[desc], N'Compañía', N'Compañía /'), N'Producción', N' Producción /' )
FROM Dcpt
Related
I am trying to convert text column to array[text] column in table i have column entry like
['Nikolai Rimsky-Korsakov', 'Jascha Heifetz', 'Arpárd Sándor']
but this is one string or text format I want to convert it into a real array of a string so that I can access a particular name in the above column.
I tried converting the type from this link by setting it to type to text[] but the column is just becoming one element of an array like this.
[ "['Nikolai Rimsky-Korsakov', 'Jascha Heifetz', 'Arpárd Sándor']" ]
But what I wanted is to type Array[text] for tat column to able to access particular names.
Use the function translate() to replace square brackets with curly ones and remove single-quotes:
translate(str, '[]''', '{}')::text[]
See the full example in Db<>fiddle.
Section 8.15.2. Array Value Input of PostgreSQL documentation describes the general look of array to be
'{ val1 delim val2 delim ... }'
So you need to trim your '[' and ']' characters and replace them with '{' and '}'.
Then you can cast to text array (text[]) and enjoy the results.
SELECT
replace(
replace(
'{'||trim(BOTH '[]' FROM test.sample)||'}',
'\',
'\\'
),
'"',
'\"'
)::text[] AS names
FROM
(
SELECT '[''Nikolai Rimsky-Korsakov'', ''Jascha Heifetz'', ''Arpárd Sándor'', ''Joe "Wingy" Manone'']'::text AS sample
) test
EDIT 2
To handle cases when there " and '' characters in your input we must escape it with \.
SELECT
replace(
replace(
'{'||trim(BOTH '[]' FROM test.sample)||'}',
'\',
'\\'
),
'"',
'\"'
)::text[] AS names
FROM
(
SELECT '[''Nikolai Rimsky-Korsakov'', ''Jascha Heifetz'', ''Arpárd Sándor'', ''Joe "Wingy" Manone'']'::text AS sample
) test
EDIT 3
To remove quotes from names:
SELECT
replace(
replace(
replace(
'{'||trim(BOTH '[]''' FROM test.sample)||'}',
'\', '\\' ),
'"', '\"'),
''', ''', ',')::text[] AS names
FROM
(
SELECT '[''Nikolai Rimsky-Korsakov'', ''Jascha Heifetz'', ''Arpárd Sándor'', ''Joe "Wingy" Manone'']'::text AS sample
) test
The problem solved by removing nested square bracket from my CSV file before populating table and that using translate function with little bit of change thank
alter artist type text[] using translate(artist, '[]"', '{}')::text[] ; ```
Let's say I have a string:
DECLARE #text VARCHAR(20) = 'abr_akad-ab#ra';
and I want to remove all _-# characters from the text.
Normally I would user REPLACE function to that, something like:
SELECT REPLACE(REPLACE(REPLACE(#text, '-', ''), '_', ''),'#','')
Can I do that with single TRANSLATE statement somehow?
You can try the following query:
DECLARE #text AS VARCHAR(20) = 'abr_akad-ab#ra';
SELECT REPLACE(TRANSLATE(#text, '_-#', '###'), '#', '')
it will return the output as abrakadabra
Working demo on db<>fiddle
You'll still need use REPLACE at some point, as SQL Server requires that the length of parameters 2 and 3 for TRANSLATE are the same length. As such an expression like the below will error:
TRANSLATE(YourColumn, '-_#','')
Therefore what you could do it replace them all with a different character using TRANSLATE, and then replace that one character:
REPLACE(TRANSLATE(YourColumn, '-_#','|||'),'|','')
I just want to get the right number format here in germany, so i need to show commas as decimal separator instead of points. But this...
DECLARE #euros money
SET #euros = 1025040.2365
SELECT CONVERT(varchar(30), #euros, 1)
Displays 1,025,040.24 instead of 1.025.040,24 (or 1025040,24). In C# it would be simple to provide the appropriate CultureInfo but how to do it in T-SQL?
Do i really need to use REPLACE? But even if, how to replace 1,025,040.24 correctly?
To provide the appropriate culture info, in SQL 2012 there is the FORMAT() function. Here's an example:
declare #f float = 123456.789;
select
[raw] = str(#f,20,3)
,[standard] = cast(format(#f, 'N', 'en-US') as varchar(20))
,[German] = cast(format(#f, 'N', 'de-DE') as varchar(20))
returns
raw |standard |German |
---------------------|-----------|-----------|
123456.789 |123,456.79 |123.456,79 |
You can also specify in the second parameter a custom format string with the same rules as for .NET.
Docs: https://msdn.microsoft.com/en-US/library/hh213505.aspx
Well, as far as I know, there are no culture-specific options for convert available.
So you can do it using replaces (yes, it looks a bit ugly...)
select
replace(replace(replace(convert(varchar(30), #euros, 1), ',', '|'), '.', ','), '|', '.')
Idea: first change comma to something, then change dot to comma, and then "something" back to dot.
DECLARE #euros money
SET #euros = 1025040.2365
SELECT REPLACE(CONVERT(varchar(30), #euros, 0), '.', ',')
should do it (at least to get 1025040,24)
You could use replace something like this:
DECLARE #euros money
SET #euros = 1025040.2365
SELECT REPLACE(REPLACE(CONVERT(varchar(30), #euros, 1),',',''),'.',',');
SQL Fiddle Demo
You can first replace thousand separator comma(,) to a Zero length string (''), and then you can replace Decimal('.') to comma(',') in the same select statement.
I am not a SQL expert so please forgive me if this is SQL 101 :).
In a select statement there are 2 replace functions. They look for a Servername and it's admin share d$ by it's UNC path. Example '\SERVERNAME\d$'
It then replaces '\SERVERNAME\d$' with 'D:'.
Here is the query currently:
select Replace(p.Path,'\\SERVERNAME\d$','D:') as searchpath
,p.path as fullpath
,s.ShareName
,s.SharePath
,p.Member
,p.Access
From Paths As p
Left Outer Join Shares as s on
Replace(p.Path,'\\SERVERNAME\d$','D:') Like s.SharePath + '\%'
Up until now it has always been d$.
Today my needs have changed and I need the query to find ANY servername UNC path admin share regardless of share letter (c$, d$, e$, f$...etc) and replace it with it's respective drive letter (D:, E:, F:... etc).
My thought is replace function could find the $ and look one character to the left of it to get the proper share letter, then use that for the replace. The issue I have, not being a SQL professional, is that I know SQL can likley do what I need it to do...I just don't know how to get there. I've googled and found some examples, but haven't had any luck in getting them to work.
Any help would be greatly appreciated.
You can use a combination of STUFF, PATINDEX, LEN to get what you want.
Sample Query
DECLARE #ReplaceChar VARCHAR(100) = '[prefixcharacters]\\SERVERNAME\d$[postcharacter]'
DECLARE #SearchString VARCHAR(100) = '\\SERVERNAME\_$'
SELECT
STUFF(#ReplaceChar,PATINDEX('%' + #SearchString + '%',#ReplaceChar),LEN(#SearchString),
UPPER(SUBSTRING(#ReplaceChar,PATINDEX('%' + #SearchString + '%',#ReplaceChar) + LEN(#SearchString) - 2,1)) + ':') as searchpath
WHERE PATINDEX('%' + #SearchString + '%',#ReplaceChar) > 0
Output
[prefixcharacters]D:[postcharacter]
Alternate Query
You can shorten the query if you want to get the previous character before $ as per your title. Something like this
DECLARE #ReplaceChar VARCHAR(100) = '[prefixcharacters]\\SERVERNAME\d$[postcharacter]'
DECLARE #SearchString VARCHAR(100) = '\\SERVERNAME\_$'
SELECT
STUFF(#ReplaceChar,
PATINDEX('%'+#SearchString+'%',#ReplaceChar),
LEN(#SearchString),
UPPER(SUBSTRING(#ReplaceChar,CHARINDEX('$',#ReplaceChar) -1,1)) + ':')
WHERE PATINDEX('%'+#SearchString+'%',#ReplaceChar) > 0
In this query
STUFF replaces your pattern with with the character before $ + ':'
Start of pattern is identified by PATINDEX('%'+#SearchString+'%',#ReplaceChar)
D is identified by getting the charindex of '$' and then getting the previous character using SUBSTRING
What about ¸
select Replace(SUBSTRING(p.path, 14, Len(#spath)-14),'$',':') as searchpath
,p.path as fullpath
,s.ShareName
,s.SharePath
,p.Member
,p.Access
From Paths As p
Left Outer Join Shares as s on
Replace(SUBSTRING(p.path, 14, Len(#spath)-14),'$',':') Like s.SharePath + '\%
select as searchpath
DECLARE #str nvarchar (100)
SET #str = '\\SERVERNAME\d$'
IF #str LIKE '\\SERVERNAME\_$'
SET #str = UPPER(SUBSTRING(#str, 14, 1)) + ':'
SELECT #str
Starting from previous, something like
select UPPER(SUBSTRING(p.path, 14, 1)) + ':' as searchpath
,p.path as fullpath
,s.ShareName
,s.SharePath
,p.Member
,p.Access
From Paths As p
Left Outer Join Shares as s on
SUBSTRING(p.path, 14, 1) + ':' Like s.SharePath + '\%'
I am no mysql expert either :)
Based on the logic you mentioned in the last part of the question, I have used concat and substring to get to the drive letter in the column.
Hope this helps
select replace(path, concat(substring(path, 1, locate('$', path) - 2), substring(path, locate('$', path) - 1, 1) , '$'), concat(substring(path, locate('$', path) - 1, 1) , ':')) as searchpath ...
The remaining part of the query would be the same.
I've a strings such as:
Games/Maps/MapsLevel1/Level 1.swf
Games/AnimalWorld/Animal1.1/Level 1.1.swf
Games/patterns and spatial understanding/Level 13.5/Level 13.5.swf
I want to get only file name without its extension(String After last Slash and before Last dot), i.e Level 1 and Level 1.1 and Level 13.5, Even I want to remove all the white spaces and the final string should be in lower case i.e the final output should be
level1
level1.1
level13.5 and so on..
I tried following query but i got Level 1.swf, How do i change this Query?
SELECT SUBSTRING(vchServerPath, LEN(vchServerPath) - CHARINDEX('/', REVERSE(vchServerPath)) + 2, LEN(vchServerPath)) FROM Games
SELECT (left((Path), LEN(Path) - charindex('.', reverse(Path))))
FROM
(
SELECT SUBSTRING(vchServerPath,
LEN(vchServerPath) - CHARINDEX('/', REVERSE(vchServerPath)) + 2,
LEN(vchServerPath)) Path
FROM Games
) A
This would work, I kept your inner substring which got you part way and I added the stripping of the dot.
I have included a sql fiddle link for you to see it in action sql fiddle
Edited:
Following will remove the white space and returns lower case...
SELECT REPLACE(LOWER((left((Path), LEN(Path) - charindex('.', reverse(Path))))), ' ', '')
FROM
(
SELECT SUBSTRING(vchServerPath,
LEN(vchServerPath) - CHARINDEX('/', REVERSE(vchServerPath)) + 2,
LEN(vchServerPath)) Path
FROM Games
) A
Try this:
select
case
when vchServerPath is not null
then reverse(replace(substring(reverse(vchServerPath),charindex('.',reverse(vchServerPath))+1, charindex('/',reverse(vchServerPath))-(charindex('.',reverse(vchServerPath))+1)),' ',''))
else ''
end
This should work fine; with extension removed.
select
REVERSE(
SUBSTRING(
reverse('Games/patterns and spatial understanding/Level 13.5/Level 13.5.swf'),
5,
(charindex('/',
reverse('Games/patterns and spatial understanding/Level 13.5/Level 13.5.swf')) - 5)
))