how to convert any char to - in sql server? - sql

i have one column in sql table like this
codel
-------------
124/500/319/1
500/2698794/3
130.500.2804508.1
800/283478/2
155-305-340007-1
130.500.2686821.1
how i can convert this code to
codel
-------------
124-500-319-1
500-2698794-3
130-500-2804508-1
800-283478-2
155-305-340007-1
130-500-2686821-1
thank you for read my qustion

In SQL Server 2017, you can also do it using TRANSLATE like following.
SELECT TRANSLATE(codel,'/.','--') AS Codel FROM YOUR_TABLE

Try below - using replace() function
select replace(replace(codel,'/','-'),'.','-') from tablename

try like below by using replace
SELECT REPLACE(codel, '/', '-');
go
SELECT REPLACE(codel, '.', '-');

Related

Substringing from a text a number

From the following string value of session i will like to keep with only the part when the first number big or the last | beginning
session
nea|fact|za|ninja|web|14ff95092e3x1d214cd2
nea|fact|za|ninja|web|15001274f5ex323c9f96
nea|fact|za|ninja|web|1502897832ax418ecf1a
nea|fact|za|ninja|web|150399c1418x215f0e52
nea|fact|za|ninja|web|1503b3cdf02x386fc450
ta|fact|za|ninja|web|1503b3cdf02x386fc450
ta|fact|za|ninja|web|1503b3cdf02x386fc450
expected result
14ff95092e3x1d214cd2
15001274f5ex323c9f96
1502897832ax418ecf1a
150399c1418x215f0e52
1503b3cdf02x386fc450
1503b3cdf02x386fc450
1503b3cdf02x386fc450
if your db mysql then SUBSTRING_INDEX will help you
select SUBSTRING_INDEX(session, "|", -1);
Example:
select SUBSTRING_INDEX('nea|fact|za|ninja|web|14ff95092e3x1d214cd2', "|", -1);
returned: 14ff95092e3x1d214cd2
In mysql string related function
For Sql server your query will be
SELECT RIGHT(session , CHARINDEX ('|' ,REVERSE(session))-1)
In Oracle -
SELECT SUBSTR(session, '|', -1)
FROM TABLE_NAME;
For sql server:
declare #test varchar(1000) = 'nea|fact|za|ninja|web|14ff95092e3x1d214cd2'
SELECT RIGHT(#test , CHARINDEX ('|' ,REVERSE(#test))-1)

How to remove a specific character from the string

Using SQL Server 2008
String like: 'C/123232323' or '/343434343443' or 'C2323232322'
From the string i want to remove C and /
Tried Query
Select replace ('/1233434', 'C/', '')
The above query is working if C/ both is there. if / only there then the replace is not working. if C only there then the replace is not working. How to achieve for both condition
Expected output
123232323
343434343443
2323232322
Need Query output
You can achieve this by nesting replace() like so:
select replace(replace('C/12341234','/',''),'C','')
Probably not the prettiest but it works :)
You could use two nested REPLACE:
WITH SampleData(string) AS(
SELECT 'C/123232323' UNION ALL
SELECT '/343434343443' UNION ALL
SELECT 'C2323232322'
)
SELECT REPLACE(REPLACE(string,'C',''),'/','')
FROM SampleData
Use Patindex + Substring
DECLARE #str VARCHAR(50)='/343434343443' ---or '/343434343443' or 'C2323232322'
SELECT Substring(#str, Patindex('%[0-9]%', #str), Len(#str))

select all character right of forward slash in sql server?

Am using following query but this to string no form how many number but actually don't know about how many character after '/' and before ?
String data like :
12/KH/123/1234
output:-
1234
Query:-
SELECT SUBSTRING(ColName,9,CHARINDEX('/',ColName,4))
FROM TABLE
Use Left, Reverse and Charindex function. Try this.
DECLARE #set VARCHAR(100)= '12/KH/123/1234'
SELECT Reverse(LEFT(Reverse(#set), Charindex('/', Reverse(#set)) - 1))
Try this.It may help you
SELECT SUBSTRING('12/KH/123/1234' ,11,CHARINDEX('/','12/KH/123/1234' ,4))

Extract required data with details from SQL data table

I have below data in SQL Database
Path
------------
Mb\Dbi\Abc
Mb\Dbi\Abc\123
Mb\Dbi\Dks
Mb\Dbi\Abc\Hig
Mb\Dbi\Abc\123\Xyz
Mb\Dbi\Abc
Mb\Dbi\Abc\Hig
Mb\Dbi\Abc\123
Mb\Dbi\Hig
Mb\Dbi\Dks\67H
I want to extract the above data in below format, Here "Mb\Dbi" remains constant and need to extract distinct Names after that and also need their exact value path.
Sr. Name Value
1 Abc Mb\Dbi\Abc
2 Abc\123 Mb\Dbi\Abc\123
3 Dks Mb\Dbi\Dks
4 Abc\Hig Mb\Dbi\Abc\Hig
5 Abc\123\Xyz Mb\DbiAbc\123\Xyz
6 Dks\67H Mb\Dbi\Dks\67H
What will be the query/stored procedure /function to accomplish this?
Thanks
You haven't specified what database server you are using.
Either way, you need to search for a replace function.
In SQL Server, the function is replace, you can find the definition here: http://msdn.microsoft.com/es-es/library/ms186862(v=sql.105).aspx
Your query will look like this in SQL Server:
Select replace(subquery.path,'Mb\Dbi','') AS Name, subquery.path as Value from (Select distinct path from {yourtable}) subquery
Regards
If You also want to generate the Serial Number:
SELECT (ROW_NUMBER() OVER ( ORDER BY [Path])) AS [Sr.]
,REPLACE ([Path],'Mb\Dbi','') AS [Name]}
,[Path] AS [Value]}
FROM tbl_PathValues
Or you can have the target table with a column predefined as an Identity column.
Try something like:
SELECT RIGHT(Value, (SELECT LENGTH(Value) - 6))
Try somthing like this,
SELECT RIGHT(DB.Path,SELECT LENGTH(DB.Path) - 6) AS 'Name', DB.Path AS 'Value' FROM DB;
You need to insert your data into Database by escaping '\'. Further following query will solve your problem:
Solution is for MySQL
SELECT #rownum := #rownum + 1 AS 'Sr.',SUBSTRING(path, 8) AS `name`, path AS VALUE FROM test_path, (SELECT #rownum := 0) r;
Hope it helps...

Oracle Query - Get only strings in the select field

Maybe this sounds a little bit crazy, but I need to come up with a query to retrieve only letters out of an alphanumeric field.
For example:
TABLE
1234ADD
3901AC
1812OPA
82711AUU
RESULTS EXPECTED
ADD
AC
OPA
AUU
Thank you!
Looks like you only want to remove numbers. You can use REGEXP_REPLACE for that in 10g or 11g:
SELECT REGEXP_REPLACE( your_column, '[0-9]*', '' ) FROM your_table;
SELECT REGEXP_REPLACE('1234ADD 3901AC 1812OPA 82711AUU', '[0-9]', '')
FROM dual
Try
SELECT TRANSLATE('1234ADD 3901AC 1812OPA 82711AUU', 'A1234567890', 'A') FROM dual;
and in general see: http://www.psoug.org/reference/translate_replace.html