I have column values = 1,2,3 AND 0,1 across 2 records.
When selecting both records from a view I am trying to remove the '1,' and '1'
My attempt is below;
CAST(CASE WHEN (column like '%1%') THEN (ReturnTheValueWithout 1 OR 1,)
ELSE column END AS VARCHAR) AS NewColumnName
You can use REPLACE:
SELECT REPLACE(REPLACE(column,'1,','')),',1','')
FROM TableName
Result:
2,3
0
Sample result in SQL Fiddle.
One way to do this is using REPLACE with other string functions like LEFT and STUFF
Query
SELECT LEFT(STUFF(REPLACE(',' + Col1 + ',',',1,',','),1,1,''),LEN(STUFF(REPLACE(',' + Col1 + ',',',1,',','),1,1,''))-1)
Test Script
DECLARE #v varchar(50) = '1,2,3'
--SET #v = '0,1'
SELECT LEFT(STUFF(REPLACE(',' + #v + ',',',1,',','),1,1,''),LEN(STUFF(REPLACE(',' + #v + ',',',1,',','),1,1,''))-1)
Output
0
2,3
Edit
A shorter version with REVERSE
SELECT REVERSE(STUFF(REVERSE(STUFF(REPLACE(',' + Col1 + ',',',1,',','),1,1,'')),1,1,''))
FROM...
SQL Fiddle
Solution1:
DECLARE #s VARCHAR(20) = '1,11,2,1,3,4,1'
SELECT REPLACE(CASE WHEN LEFT(#s, 2) = '1,'
AND RIGHT(#s, 2) = ',1'
THEN SUBSTRING(#s, 3, LEN(#s) - 4)
WHEN LEFT(#s, 2) = '1,' THEN RIGHT(#s, LEN(#s) - 2)
WHEN RIGHT(#s, 2) = ',1' THEN LEFT(#s, LEN(#s) - 2)
ELSE #s
END, ',1,', ',')
Output:
11,2,3,4
Solution2:
SELECT SUBSTRING(REPLACE(',' + #s + ',', ',1,', ','), 2, LEN(REPLACE(',' + #s + ',', ',1,', ','))-2)
Shorter version:
SELECT SUBSTRING(s, 2, LEN(s) - 2) FROM (SELECT REPLACE(',' + #s + ',', ',1,', ',') s)t
Solution3:
Most short:
SELECT REPLACE(',' + REPLACE(#s, ',1,', ',,1,') + ',', ',1,', '')
Solution4:
Above solutions failed here and there on some complex string. The following works on
DECLARE #s VARCHAR(120) = '1,31,11,2,1,3,4,1234,1,1,1,1,1,1,1,1,1,sfds,23,12,11,1'
SELECT REPLACE(REPLACE(REPLACE(CASE WHEN LEFT(#s, 2) = '1,'
AND RIGHT(#s, 2) = ',1'
THEN SUBSTRING(#s, 3, LEN(#s) - 4)
WHEN LEFT(#s, 2) = '1,' THEN RIGHT(#s, LEN(#s) - 2)
WHEN RIGHT(#s, 2) = ',1' THEN LEFT(#s, LEN(#s) - 2)
ELSE #s
END, ',1,', ',,1,,'), ',1,', ''), ',,', ',')
Output:
31,11,2,3,4,1234,sfds,23,12,11
Related
I have specific time scenario which can be 4 digit, 6 digit or can be NULL or string as mentioned below. Here in scenario 3 & 4 my method of calculating datetime is not working and coming as NULL
Is there any way to get date with 00:00:00:000 as time for case 3 & 4?
& for 1 it should be 10:02:00:000
DECLARE #DATE VARCHAR(10) =CAST(GETDATE() AS DATE)
DECLARE #Time1 VARCHAR(10) = '1002'
DECLARE #Time2 VARCHAR(10) = '160634'
DECLARE #Time3 VARCHAR(10) = '0900XX'
DECLARE #Time4 VARCHAR(10) = ''
SELECT TRY_CONVERT(DATETIME, #DATE +' '
+LEFT(ltrim(#Time1), 2)
+ ':' + SUBSTRING(#Time1, 3, 2)
+ ':' + RIGHT(rtrim(#Time1), 2)) , TRY_CONVERT(TIME, #Time1), #Time1 AS Time
SELECT TRY_CONVERT(DATETIME, #DATE +' '
+LEFT(ltrim(#Time2), 2)
+ ':' + SUBSTRING(#Time2, 3, 2)
+ ':' + RIGHT(rtrim(#Time2), 2)) , TRY_CONVERT(TIME, #Time2), #Time2 AS Time
SELECT TRY_CONVERT(DATETIME, #DATE +' '
+LEFT(ltrim(#Time3), 2)
+ ':' + SUBSTRING(#Time3, 3, 2)
+ ':' + RIGHT(rtrim(#Time3), 2)) , TRY_CONVERT(TIME, #Time3), #Time3 AS Time
SELECT TRY_CONVERT(DATETIME, #DATE +' '
+LEFT(ltrim(#Time4), 2)
+ ':' + SUBSTRING(#Time4, 3, 2)
+ ':' + RIGHT(rtrim(#Time4), 2)) , TRY_CONVERT(TIME, #Time4), #Time4 AS Time
I would, personally, "pad out" the values to be 6 digits, inject the : characters, and then use TRY_CONVERT. Then you use ISNULL to return midmight for failed converions:
SELECT ISNULL(TRY_CONVERT(time(0),STUFF(STUFF(RIGHT('000000' + V.VarcharTime,6),5,0,':'),3,0,':')),'00:00:00')
FROM (VALUES(#Time1),
(#Time2),
(#Time3),
(#Time4))V(VarcharTime);
If 1002 is meant to be 10:02:00 rather than 00:10:02 then pad on the right, rather than the left:
SELECT ISNULL(TRY_CONVERT(time(0),STUFF(STUFF(LEFT(V.VarcharTime+'000000',6),5,0,':'),3,0,':')),'00:00:00')
FROM (VALUES(#Time1),
(#Time2),
(#Time3),
(#Time4))V(VarcharTime);
..................
DECLARE #Time4 VARCHAR(10) = ''
select #Time1 = concat(cast(#Time1 as varchar(8)), replicate('0', 8));
select #Time2 = concat(cast(#Time2 as varchar(8)), replicate('0', 8));
select #Time3 = concat(cast(#Time3 as varchar(8)), replicate('0', 8));
select #Time4 = concat(cast(#Time4 as varchar(8)), replicate('0', 8));
SELECT TRY_CONVERT(DATETIME, #DATE +' '......................
I have a set of data for example:
Part no Custom Format
1128005 \Machines\3D\PartNo(2)\PartNo(4)xx\PartNo(7)
11.88.006 \Machines\3D\PartNo(2)\PartNo+3(2)xx\PartNo+6(3)
I want to replace the variable set in the custom format define in it. The result i am looking for is
For Part no
1128005
the result is
\Machines\3D\11\1128xx\1128005
11.88.006
\Machines\3D\11\88xx\006
Any ideas?
Thanks
Regards
If my understanding is correct, below script should be what you want? But because it processes row by row, it can be very slow when dealing with millions of rows.
--declare #input varchar(50) = '11.88.006', #pattern varchar(255) = '\Machines\3D\PartNo(2)\PartNo+3(2)xx\PartNo+6(3)'
declare #input varchar(50) = '1128005', #pattern varchar(255) = '\Machines\3D\PartNo(2)\PartNo(4)xx\PartNo(7)'
declare #tblPattern table (tmpKey int identity(1,1), result varchar(50), Position varchar(50), SkipCnt int, CharLength int, Sufix varchar(50))
declare #i int = 1, #output varchar(max) = '\Machines\3D\'
Declare #PatternXml XML
Set #PatternXml = N'<root><r>' + REPLACE(replace(#pattern, '\Machines\3D\', ''), '\', '</r><r>') + '</r></root>'
insert into #tblPattern(result)
select r.value('.', 'VARCHAR(MAX)') as t
from #PatternXml.nodes('//root//r') as records(r)
update #tblPattern set Position = REPLACE(result, 'PartNo', '')
, SkipCnt = CASE WHEN CHARINDEX('+', result, 1) > 0 THEN SUBSTRING(result, CHARINDEX('+', result, 1) + 1, CHARINDEX('(', result, 1) - CHARINDEX('+', result, 1) - 1) ELSE 0 END
, CharLength = SUBSTRING(result, CHARINDEX('(', result, 1) + 1, CHARINDEX(')', result, 1) - CHARINDEX('(', result, 1) - 1 )
, Sufix = SUBSTRING(result, CHARINDEX(')', result, 1) + 1, LEN(result))
while #i <= 3
begin
select #output += SUBSTRING(#input, 1 + SkipCnt, CharLength) + Sufix + '\'
from #tblPattern
where tmpKey = #i
set #i += 1
end
select #output = STUFF(#output, len(#output), 1, '')
select #output
Query:
SELECT filePath FROM Table WHERE filePath IS NOT NULL
It'll return something like \\server\folder1\folder2\filename.tif
I need to query it to replace "\\server\folder1\folder2\" with a variable that's in the stored procedure (#Path) and end the format of the file like .jpg.
So the result would be something like #Path + '.jpg'
How would I go about doing this?
You can use a combination of string functions like REVERSE, SUBSTRING, LEFT and CHARINDEX:
CREATE TABLE YourTable(filePath VARCHAR(2000))
INSERT INTO YourTable VALUES('\\server\folder1\folder2\filename.tif');
DECLARE #path VARCHAR(2000) = 'path\to\folder\'
SELECT
[File] = REVERSE(LEFT(REVERSE(filePath), CHARINDEX('\', REVERSE(filePath),0) - 1)),
[File without ext] =
SUBSTRING(
REVERSE(LEFT(REVERSE(filePath), CHARINDEX('\', REVERSE(filePath), 0) - 1)),
0,
CHARINDEX(
'.',
REVERSE(LEFT(REVERSE(filePath), CHARINDEX('\', REVERSE(filePath), 0) - 1)),
0
)
),
[Final String] =
#path +
SUBSTRING(
REVERSE(LEFT(REVERSE(filePath), CHARINDEX('\', REVERSE(filePath), 0) - 1)),
0,
CHARINDEX(
'.',
REVERSE(LEFT(REVERSE(filePath), CHARINDEX('\', REVERSE(filePath), 0) - 1)),
0
)
) +
'.jpg'
FROM YourTable
Use a combination of REVERSE and CHARINDEX to locate the last \:
DECLARE #filePath VARCHAR(500) = '\\server\folder1\folder2\filename.tif'
DECLARE #newPath VARCHAR(500) = '\\server2\newpath\'
SELECT #newPath +
SUBSTRING(#filePath, LEN(#filePath) - CHARINDEX('\', REVERSE(#filePath))+2, LEN(#filePath))
I would like to split a string by commas (,) or pipe (|) to each character in SQL SERVER. Example 'APPLE'. Expected result: 'A|P|P|L|E'. Preferably without creating function.
You can do it with CTE:
DECLARE #s NVARCHAR(MAX) = 'APPLE'
DECLARE #result NVARCHAR(MAX)
;WITH cte(N, S) AS
(
SELECT 1 AS N, SUBSTRING(#s, 1, 1)
UNION ALL
SELECT N + 1, SUBSTRING(#s, N + 1, 1)
FROM cte
WHERE N < LEN(#s)
)
SELECT #result = COALESCE(#result + '|', '') + S FROM cte
SELECT #result
Output:
A|P|P|L|E
Or even shorter version:
DECLARE #s NVARCHAR(MAX) = 'APPLE'
;WITH cte(N, S, D) AS
(
SELECT 1 AS N, SUBSTRING(#s, 1, 1), D = SUBSTRING(#s, 1, 1)
UNION ALL
SELECT N + 1, SUBSTRING(#s, N + 1, 1), D = D + '|' + SUBSTRING(#s, N + 1, 1)
FROM cte
WHERE N < LEN(#s)
)
SELECT TOP 1 D FROM cte
ORDER BY N DESC
You could use a concept like the "Tally Table String Splitter" to achieve what you want.
http://www.sqlservercentral.com/articles/Tally+Table/72993/
DECLARE #txt varchar(50) ='APPLE'
;WITH cte(x) as
(
SELECT top (len(#txt)) ';'
+ substring(#txt, row_number() over (order by (select 1)), 1)
FROM master..spt_values x1
cross join
master..spt_values x2
FOR XML PATH('')
)
SELECT stuff(x, 1, 1, '')
FROM CTE
Result
A;P;P;L;E
declare #Dimension = '1 1/4' varchar(20)
I want to change Dimension = '1 1/4' to Dimension = '1.2500'. I do not know how to split
a varchar into 2 varchar and change one part and then recombine then into a single varchar
UPDATE VF_CasINV_Cost
SET #Dimension = CASE
when (#Dimension like '%1/4') then
(left(#Dimension, charindex(' ', #Dimension, 1) - 1) *
(substring(#Dimension, 1 + charindex(' ', #Dimension, 1), len(#Dimension)))
end
where #Dimension like '%1/4'
what would be great to know how to parse the fraction and recal it into decimal on the fly
declare #x varchar(100)
select #x = '15 3/165'
select
convert(int, substring(#x, 1, charindex(' ', #x))) + (
convert(decimal(12,4), substring(#x, charindex(' ', #x) + 1, charindex('/', #x) - charindex(' ', #x) - 1)) /
convert(decimal(12,4), substring(#x, charindex('/', #x) + 1, len(#x) - charindex('/', #x)))
)
Payload beat me to it but one way is to;
declare #Dimension varchar(20) = '1 1/8'
declare #sql nvarchar(512) = 'set #out=' + replace(#Dimension, ' ',' + ') + '.00'
declare #result decimal(9,4)
execute sp_executesql #sql, N'#value varchar(30), #out decimal(9,4) output', #value=#Dimension, #out=#result output
select #result
isolate the fraction with substring and charindexing, then execute a select.
set #stringquery = SELECT "SELECT "+substring(#Dimension,1+charindex(' ',#Dimension,1),len(#Dimension);
execute sp_executesql #stringquery
This should handle and fraction after a space.
actually, thinking about it - to do the whole equation, just replace the space with a + sign and run it through sp_executesql, this should (I havnt tested this right at this point) convert 1 1/4 to 1+1/4 and the standard math engine will do the division before the addition so it will end up as 1.25.
This script can handle more variations in dimension (x in the table)
declare #t table(x varchar(20))
insert #t values('1 1/2')
insert #t values('2')
insert #t values('2/3')
insert #t values('22 1/3')
select coalesce(cast(case when isnumeric(n1)=1 then n1 else 0 end + n2 / n3 as real), n1)
from
(
select case when isnumeric(x) = 1 then x else
left(x, patindex('%_ %', x)) end n1,
cast(stuff(stuff(x, patindex('%/%', x), charindex('/',reverse(x)), ''), 1,charindex(' ',x),'') as real) n2,
case when patindex('%_/%', reverse(x)) = 0 then null else
cast(right(x, patindex('%_/%', reverse(x))) as real) end n3
from #t
) a