Related
I have 111-1001-0000-0000 this record in one column and 453200-0000-000 in second column
I want output as 111-1001-0000-453200-0000-0000-000
That means 111-1001-0000 from 1st column and 453200 from 2nd column and again 0000 from 1st column and 0000-000
from 2nd column
I tried below query but getting 111-1001-453200-0000-0000-000.
-0000 is missing from 1st column
Declare #1stcolumn nvarchar(30),#2ndcolumn nvarchar(30)
set #1stcolumn='111-1001-0000-0000'
set #2ndcolumn='453200-0000-000'
select substring(#1stcolumn,1,charindex(right(#1stcolumn,charindex('-',reverse(#1stcolumn))),#1stcolumn))
+substring(#2ndcolumn,1,charindex('-',#2ndcolumn))+reverse(substring(reverse(#1stcolumn),0,charindex('-',reverse(#1stcolumn))))
+'-'+substring(#2ndcolumn,charindex('-',#2ndcolumn)+1,len(#2ndcolumn))
find the position where to split column 1 and column2. Use LEFT() and RIGHT() to split the string and then concatenate back in the order that you want
; with tbl as
(
select col1 = #1stcolumn, col2 = #2ndcolumn
)
select *,
c1.s1 + '-' + c2.s1 + '-' +c1.s2 + '-' + c2.s2
from tbl t
cross apply
(
select s1 = left(col1, p - 1),
s2 = right(col1, len(col1) - p)
from (
-- find the position of 3rd '-' by cascading charindex
select p = charindex('-', col1,
charindex('-', col1,
charindex('-', col1) + 1) + 1)
) p
) c1
cross apply
(
select s1 = left(col2, p - 1),
s2 = right(col2, len(col2) - p)
from (
select p = charindex('-', col2)
) p
) c2
A little modification in first substring. To get correct length I used LEN .
select substring(#1stcolumn,1,(Len(#1stcolumn) - charindex('- ',REVERSE(#1stcolumn)) + 1))
+substring(#2ndcolumn,1,charindex('-',#2ndcolumn))
+reverse(substring(reverse(#1stcolumn),0,charindex('-',reverse(#1stcolumn))))
+'-'+substring(#2ndcolumn,charindex('-',#2ndcolumn)+1,len(#2ndcolumn))
I'd probably do with with PARSENAME as it's quite concise then:
WITH YourTable AS(
SELECT '111-1001-0000-0000' AS Column1,
'453200-0000-000' AS Column2)
SELECT CONCAT_WS('-',PN.C1P1,PN.C1P2,PN.C1P3,PN.C2P1,PN.C1P4,PN.C2P2,PN.C2P3) AS NewString
FROM YourTable YT
CROSS APPLY (VALUES(REPLACE(YT.Column1,'-','.'),REPLACE(YT.Column2,'-','.')))R(Column1,Column2)
CROSS APPLY (VALUES(PARSENAME(R.Column1,4),PARSENAME(R.Column1,3),PARSENAME(R.Column1,2),PARSENAME(R.Column1,1),PARSENAME(R.Column2,3),PARSENAME(R.Column2,2),PARSENAME(R.Column2,1)))PN(C1P1,C1P2,C1P3,C1P4,C2P1,C2P2,C2P3);
WITH
test AS
(
select '111-1001-0000-0000' as col1, '453200-0000-000' as col2
)
,cte as
(
select
col1,
col2,
substring
(
col1,
0,
len(col1)-charindex('-',reverse(col1))
) as part1,
substring
(
col2,
0,
len(col2)-charindex('-',col2) - 1
) as part2
from test
),
cte2 as
(
select
part1,
part2,
substring
(
reverse(col1),
0,
charindex('-',reverse(col1))
) as part3,
substring
(
col2,
charindex('-',col2)+1,
len(col2)-charindex('-',col2)+1
) as part4
from cte
)
select part1+'-'+part2+'-'+part3+'-'+part4
from cte2
Thank you in advance.
I want to remove string after . including ., but length is variable and string can be of any length.
1)Example:
Input:- SCC0204.X and FRK0005.X and RF0023.X and ADF1010.A and HGT9010.V
Output: SCC0204 and FRK0005 and RF0023 and ADF1010.A and HGT9010.V
I tried using the charindex but as the length keeps on changing i wasn't able to do it. I want to trim the values with ending with only X
Any help will be greatly appreciated.
Assuming there is only one dot
UPDATE TABLE
SET column_name = left(column_name, charindex('.', column_name) - 1)
For SELECT
select left(column_name, charindex('.', column_name) - 1) AS col
from your_table
Hope this helps. The code only trims the string when the value has a decimal "." in it and if that value is equal to .X
;WITH cte_TestData(Code) AS
(
SELECT 'SCC0204.X' UNION ALL
SELECT 'FRK0005.X' UNION ALL
SELECT 'RF0023.X' UNION ALL
SELECT 'ADF1010.A' UNION ALL
SELECT 'HGT9010.V' UNION ALL
SELECT 'SCC0204' UNION ALL
SELECT 'FRK0005'
)
SELECT CASE
WHEN CHARINDEX('.', Code) > 0 AND RIGHT(Code,2) = '.X'
THEN SUBSTRING(Code, 1, CHARINDEX('.', Code) - 1)
ELSE Code
END
FROM cte_TestData
If the criteria is only to replace remove .X then probably this should also work
;WITH cte_TestData(Code) AS
(
SELECT 'SCC0204.X' UNION ALL
SELECT 'FRK0005.X' UNION ALL
SELECT 'RF0023.X' UNION ALL
SELECT 'ADF1010.A' UNION ALL
SELECT 'HGT9010.V' UNION ALL
SELECT 'SCC0204' UNION ALL
SELECT 'FRK0005'
)
SELECT REPLACE (Code,'.X','')
FROM cte_TestData
Use LEFT String function :
DECLARE #String VARCHAR(100) = 'SCC0204.XXXXX'
SELECT LEFT(#String,CHARINDEX('.', #String) - 1)
I think your best bet here is to create a function that parses the string and uses regex. I hope this old post helps:
Perform regex (replace) in an SQL query
However, if the value you need to trim is constantly ".X", then you should use
select replace(string, '.x', '')
Please check the below code. I think this will help you.
DECLARE #String VARCHAR(100) = 'SCC0204.X'
IF (SELECT RIGHT(#String,2)) ='.X'
SELECT LEFT(#String,CHARINDEX('.', #String) - 1)
ELSE
SELECT #String
Update: I just missed one of the comments where the OP clarifies the requirement. What I put together below is how you would deal with a requirement to remove everything after the first dot on strings ending with X. I leave this here for reference.
;WITH cte_TestData(Code) AS
(
SELECT 'SCC0204.X' UNION ALL -- ends with '.X'
SELECT 'FRK.000.X' UNION ALL -- ends with '.X', contains multiple dots
SELECT 'RF0023.AX' UNION ALL -- ends with '.AX'
SELECT 'ADF1010.A' UNION ALL -- ends with '.A'
SELECT 'HGT9010.V' UNION ALL -- ends with '.V'
SELECT 'SCC0204.XF' UNION ALL -- ends with '.XF'
SELECT 'FRK0005' UNION ALL -- totally clean
SELECT 'ABCX' -- ends with 'X', not dots
)
SELECT
orig_string = code,
newstring =
SUBSTRING
(
code, 1,
CASE
WHEN code LIKE '%X'
THEN ISNULL(NULLIF(CHARINDEX('.',code)-1, -1), LEN(code))
ELSE LEN(code)
END
)
FROM cte_TestData;
FYI - SQL Server 2012+ you could simplify this code like this:
SELECT
orig_string = code,
newstring =
SUBSTRING(code, 1,IIF(code LIKE '%X', ISNULL(NULLIF(CHARINDEX('.',code)-1, -1), LEN(code)), LEN(code)))
FROM cte_TestData;
With SUBSTRING you can achieve your requirements by below code.
SELECT SUBSTRING(column_name, 0, CHARINDEX('.', column_name)) AS col
FROM your_table
If you want to remove fixed .X from string you can also use REPLACE function.
SELECT REPLACE(column_name, '.X', '') AS col
I have a field that holds an account code. I've managed to extract the first 2 parts OK but I'm struggling with the last 2.
The field data is as follows:
812330/50110/0-0
812330/50110/BDG001-0
812330/50110/0-X001
I need to get the string between the second "/" and the "-" and after the "-" .Both fields have variable lengths, so I would be looking to output 0 and 0 on the first record, BDG001 and 0 on the second record and 0 and X001 on the third record.
Any help much appreciated, thanks.
You can use CHARINDEX and LEFT/RIGHT:
CREATE TABLE #tab(col VARCHAR(1000));
INSERT INTO #tab VALUES ('812330/50110/0-0'),('812330/50110/BDG001-0'),
('812330/50110/0-X001');
WITH cte AS
(
SELECT
col,
r = RIGHT(col, CHARINDEX('/', REVERSE(col))-1)
FROM #tab
)
SELECT col,
r,
sub1 = LEFT(r, CHARINDEX('-', r)-1),
sub2 = RIGHT(r, LEN(r) - CHARINDEX('-', r))
FROM cte;
LiveDemo
EDIT:
or even simpler:
SELECT
col
,sub1 = SUBSTRING(col,
LEN(col) - CHARINDEX('/', REVERSE(col)) + 2,
CHARINDEX('/', REVERSE(col)) -CHARINDEX('-', REVERSE(col))-1)
,sub2 = RIGHT(col, CHARINDEX('-', REVERSE(col))-1)
FROM #tab;
LiveDemo2
EDIT 2:
Using PARSENAME SQL SERVER 2012+ (if your data does not contain .):
SELECT
col,
sub1 = PARSENAME(REPLACE(REPLACE(col, '/', '.'), '-', '.'), 2),
sub2 = PARSENAME(REPLACE(REPLACE(col, '/', '.'), '-', '.'), 1)
FROM #tab;
LiveDemo3
...Or you can do this, so you only go from left side to right, so you don't need to count from the end in case you have more '/' or '-' signs:
SELECT
SUBSTRING(columnName, CHARINDEX('/' , columnName, CHARINDEX('/' , columnName) + 1) + 1,
CHARINDEX('-', columnName) - CHARINDEX('/' , columnName, CHARINDEX('/' , columnName) + 1) - 1) AS FirstPart,
SUBSTRING(columnName, CHARINDEX('-' , columnName) + 1, LEN(columnName)) AS LastPart
FROM table_name
One method way is to download a split() function off the web and use it. However, the values end up in separate rows, not separate columns. An alternative is a series of nested subqueries, CTEs, or outer applies:
select t.*, p1.part1, p12.part2, p12.part3
from table t outer apply
(select t.*,
left(t.field, charindex('/', t.field)) as part1,
substring(t.field, charindex('/', t.field) + 1) as rest1
) p1 outer apply
(select left(p1.rest1, charindex('/', p1.rest1) as part2,
substring(p1.rest1, charindex('/', p1.rest1) + 1, len(p1.rest1)) as part3
) p12
where t.field like '%/%/%';
The where clause guarantees that the field value is in the right format. Otherwise, you need to start sprinkling the code with case statements to handle misformated data.
Hi i want to sort alphanumeric data in ascending order .
data like this :
1mac , apple , dom , 007bind , pcworld , 8basic , nothing.
But here I want the result such as :
apple, dom ,nothing, pcworld , 007bind , 1mac , 8basic
can anyone tell me the sql query to show such result.
Any help will be appreciated.
Thanks In Advance..
SELECT your_field FROM your_table ORDER BY (your_field + 0 <> 0 OR your_field = '0') ASC, your_field + 0, your_field
You have to check if your database vendor supports such a collation and set the collation accordingly.
http://www.collation-charts.org
E.g. oracle has http://www.collation-charts.org/oracle10g/ora10g.CL8MSWIN1251.GENERIC_BASELETTER.html
Here's one way to do it:
select col from sometable where left(col, 1) < '0' or left(col, 1) > '9' order by col
union all
select col from sometable where left(col, 1) >= '0' and left(col, 1) <= '9' order by col
I have a field with something like okbogkbogkbokgbokgobkgobkofkgbokfgbo&name=fokdofkd&okfaos
I'd like to extract the name = value from the data.
How can I do that using SQL Server 2008 in efficient way?
thank you
Try this
;
With MyTable as
(
SELECT 'okbogkbogkbokgbokgobkgobkofkgbokfgbo&name=fokdofkd&okfaos' FullString
UNION
SELECT 'fkgbokfgbo&name=fokdofkd&okfaos' FullString
UNION
SELECT 'okbogkbogkbokgbok' FullString
),
PatternIndex as
(
Select
FullString + '&' FullString,
CharIndex ('&name=', FullString) + 1 LocationOfEqualSign,
CharIndex ('&', FullString, CharIndex ('&name=', FullString)+1) as NextBreak
from MyTable
),
SplitNameValue AS
(
Select
FullString,
CASE
WHEN NextBreak <> 0 THEN
SubString (FullString, LocationOfEqualSign, NextBreak-LocationOfEqualSign)
ELSE '' END
as NameValuePair
From PatternIndex
)
SELECT *
FROM SplitNameValue
Returns
FullString NameValuePair
--------------------------------------------------------- ---------------------------------------------------------
fkgbokfgbo&name=fokdofkd&okfaos name=fokdofkd
okbogkbogkbokgbok
okbogkbogkbokgbokgobkgobkofkgbokfgbo&name=fokdofkd&okfaos name=fokdofkd
(3 row(s) affected)
SELECT 'name=' + SUBSTRING(field, CHARINDEX('name=', field), CHARINDEX('&', SUBSTRING(field, CHARINDEX('name=' ,field))))
Something along the lines of...
select substring(ColumnName, charindex('name=',ColumnName,1),len(ColumnName))
This will work I think:
Declare #String varchar(100)
Set #String = 'okbogkbogkbokgbokgobkgobkofkgbokfgbo&name=fokdofkd&okfaos'
Select SubString(#String,CharIndex('name=',#String),len(#String))
This produces:
'name=fokdofkd&okfaos'