In SQL Server I have a field that has delimited data (by space) in it.
E.g.
recid| Delimited data field
1| 1 2 3 4 5
2| 1 2 3 3 5
3| 1 1 1 1 1
I need to loop through all the records in the DB and interrogate the delimited data field and compare the third and fourth parts of data against each other and if they match, return the recid and the whole delimited field.
So from my example records 2 and 3 have matching data parts, so it would return:-
2|1 2 3 3 5
3|1 1 1 1 1
Because 3 3 matches, as does 1 1.
Thanks.
If it is always 1 digit and same format, you can try like following.
select * from #table
where SUBSTRING([data], 5, 1) = SUBSTRING([data], 7, 1)
If not (Numbers are not single digit), you can try like following.
;WITH cte
AS (SELECT F1.recid,
F1.[data],
O.splitdata,
Row_number()
OVER(
partition BY recid
ORDER BY (SELECT 1)) rn
FROM (SELECT *,
Cast('<X>' + Replace(F.data, ' ', '</X><X>') + '</X>' AS
XML)
AS
xmlfilter
FROM #table F)F1
CROSS apply (SELECT fdata.d.value('.', 'varchar(50)') AS
splitdata
FROM f1.xmlfilter.nodes('X') AS fdata(d)) O)
SELECT c1.recid,
c1.data
FROM cte c1
INNER JOIN cte c2
ON c1.recid = c2.recid
AND c1.rn = 3
AND c2.rn = 4
AND c1.splitdata = c2.splitdata
GROUP BY c1.recid,
c1.data
Online Demo
Need to split the data, give the row number and then compare.
Schema:
SELECT * INTO #TAB FROM (
SELECT 1, '1 2 3 4 5' UNION ALL
SELECT 2, '1 2 3 3 5' UNION ALL
SELECT 3, '1 1 1 1 1'
)A (recid , Delimited_data_field)
Solution :
;WITH CTE
AS (
SELECT recid
,Delimited_data_field
,ROW_NUMBER() OVER (PARTITION BY recid ORDER BY (SELECT 1)) RNO
,splt.X.value('.', 'INT') VAL
FROM (
SELECT recid
,Delimited_data_field
,CAST('<M>' + REPLACE(Delimited_data_field, ' ', '</M><M>') + '</M>' AS XML) DATA
FROM #TAB
) A
CROSS APPLY A.DATA.nodes('/M') splt(x)
)
SELECT C.recid
,C2.Delimited_data_field
FROM CTE C
INNER JOIN CTE C2 ON C.recid = C2.recid AND C.RNO = 3 AND C2.RNO = 4
AND C.VAL = C2.VAL
Result :
recid Delimited_data_field
2 1 2 3 3 5
3 1 1 1 1 1
Your question has two parts, find nth split and then compare. Your first approach should be to break the problem until you find built in functions that can do the job.
here is one method inner query return result after split and outer compares:
SELECT recid,Delimited from (
SELECT recid,Delimited, SUBSTRING(Delimited,
charindex(' ', Delimited, (charindex(' ', Delimited, 1))+2)+1,1)
third, SUBSTRING(Delimited, charindex(' ',Delimited,
(charindex(' ', Delimited, 1))+3)+1,1)
fourth FROM YourTable) tr
WHERE third = fourth
See simple substring and charindex can do the job.
Here is one more solution to that.
I tweaked the split function in this link (T-SQL: Opposite to string concatenation - how to split string into multiple records) a bit to make it usefule in your scenario.
Here is the function.
CREATE FUNCTION dbo.SplitAndGetNumberAt (#sep char(1), #s varchar(512), #pos int)
RETURNS INT
BEGIN
declare #val as varchar(10);
WITH Pieces(pn, start, stop) AS (
SELECT 1, 1, CHARINDEX(#sep, #s)
UNION ALL
SELECT pn + 1, stop + 1, CHARINDEX(#sep, #s, stop + 1)
FROM Pieces
WHERE stop > 0
)
SELECT #val = SUBSTRING(#s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END)
FROM Pieces where pn = #pos;
RETURN #val
END
Now you can use this function to get 3rd and 4th position of numbers and compare easily.
select recid, deldata
from so1
where dbo.SplitAndGetNumberAt (' ', deldata, 3) = dbo.SplitAndGetNumberAt (' ', deldata, 4)
Hope it will help.
If you have SQL Server 2016 or higher, you may try one approach using OPENJSON() to split your input data. The important part here is the fact, that when OPENJSON parses a JSON array the indexes of the elements in the JSON text are returned as keys (0-based).
Input:
CREATE TABLE #Table (
RecId int,
Data varchar(max)
)
INSERT INTO #Table
(RecId, Data)
VALUES
(1, '1 2 3 4 5'),
(2, '1 2 3 3 5'),
(3, '1 1 1 1 1')
Statement:
SELECT
t.RecId,
t.Data
FROM #Table t
CROSS APPLY (SELECT [value] FROM OPENJSON('["' + REPLACE(t.Data,' ','","') + '"]') WHERE [key] = 2) j3
CROSS APPLY (SELECT [value] FROM OPENJSON('["' + REPLACE(t.Data,' ','","') + '"]') WHERE [key] = 3) j4
WHERE j3.[value] = j4.[value]
Output:
RecId Data
2 1 2 3 3 5
3 1 1 1 1 1
Just for fun, sort of crazy coding:
DECLARE #Table Table (
recid INT,
DelimitedDataField VARCHAR(32)
)
INSERT #Table (recid, DelimitedDataField)
VALUES
(1, '1 2 3 4 5'),
(2, '1 2 3 3 5'),
(3, '1 1 1 1 1')
SELECT *
FROM #Table
WHERE
SUBSTRING (
STUFF(
STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'),
''
),
1,
CHARINDEX(' ', STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'), '')
),
''),
1,
CHARINDEX(' ', STUFF(
STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'),
''
),
1,
CHARINDEX(' ', STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'), '')
),
'')
)
) =
SUBSTRING (
STUFF(
STUFF(
STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'),
''
),
1,
CHARINDEX(' ', STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'), '')
),
''),
1,
CHARINDEX(' ', STUFF(
STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'),
''
),
1,
CHARINDEX(' ', STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'), '')
),
'')
),
''
),
1,
CHARINDEX(' ', STUFF(
STUFF(
STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'),
''
),
1,
CHARINDEX(' ', STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'), '')
),
''),
1,
CHARINDEX(' ', STUFF(
STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'),
''
),
1,
CHARINDEX(' ', STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'), '')
),
'')
),
''
))
)
AND SUBSTRING (
STUFF(
STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'),
''
),
1,
CHARINDEX(' ', STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'), '')
),
''),
1,
CHARINDEX(' ', STUFF(
STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'),
''
),
1,
CHARINDEX(' ', STUFF(
DelimitedDataField + ' - - -',
1,
CHARINDEX(' ', DelimitedDataField + ' - - -'), '')
),
'')
)
) <>'-'
have something kinda weird here. I have a database that's called FLDOC. In it has a column called SENTENCE that contains 7 numbers that represent a length of time.
example:
0050000
0750000
0000600
0040615
0000110
In those 7 digits is a length of type since the digits represent YYYMMDD
So what I'd like is a script that can convert it to something like this:
5Y 00M 00D
75Y 00M 00D
6M (or 000Y 6M 00D is fine as well)
4Y 6M 15D etc etc
thanks in advance...
CONCAT is new to SQL Server 2012. If you have previous version of SQL Server, you could do something like this instead to achieve your desired output:
SELECT sentence
,(
CASE
WHEN cast(left(sentence, 3) AS INT) > 0
THEN cast(cast(left(sentence, 3) AS INT) AS VARCHAR(3)) + 'Y '
ELSE cast(left(sentence, 3) AS VARCHAR(3)) + 'Y '
END +
CASE
WHEN cast(substring(sentence, 4, 2) AS INT) > 0
THEN cast(cast(substring(sentence, 4, 2) AS INT) AS VARCHAR(2)) + 'M '
ELSE cast(substring(sentence, 4, 2) AS VARCHAR(2)) + 'M '
END +
CASE
WHEN cast(right(sentence, 2) AS INT) > 0
THEN cast(cast(right(sentence, 2) AS INT) AS VARCHAR(3)) + 'D'
ELSE cast(right(sentence, 2) AS VARCHAR(3)) + 'D'
END
) AS new_sentence
FROM FLDOC;
SQL Fiddle Demo
UPDATE
To answer your question below in the comments, you could maybe just write a update statement like this:
update FLDOC
set sentence = (
CASE
WHEN cast(left(sentence, 3) AS INT) > 0
THEN cast(cast(left(sentence, 3) AS INT) AS VARCHAR(3)) + 'Y '
ELSE cast(left(sentence, 3) AS VARCHAR(3)) + 'Y '
END +
CASE
WHEN cast(substring(sentence, 4, 2) AS INT) > 0
THEN cast(cast(substring(sentence, 4, 2) AS INT) AS VARCHAR(2)) + 'M '
ELSE cast(substring(sentence, 4, 2) AS VARCHAR(2)) + 'M '
END +
CASE
WHEN cast(right(sentence, 2) AS INT) > 0
THEN cast(cast(right(sentence, 2) AS INT) AS VARCHAR(3)) + 'D'
ELSE cast(right(sentence, 2) AS VARCHAR(3)) + 'D'
END
)
Try this query
select convert(varchar(10),left(example,3))+'Y '+
convert(varchar(10),Substring(example,4,3))+'M '+
convert(varchar(10),Right(example,3))+'D'+ from tablename
You can do this with Concat as well:
Select Concat
(
Left(SENTENCE, 3), 'Y ',
SubString(SENTENCE, 4, 2), 'M ',
Right(SENTENCE, 2), 'D'
)
From Table
To condense the expression as in your example, this can be used as well:
Select Concat
(
Case When (IsNumeric(Left(SENTENCE, 3)) = 1 And Left(SENTENCE, 3) <> '000')
Then Convert(Varchar (3), Convert(Int, Left(SENTENCE, 3))) + 'Y ' End,
Case When (IsNumeric(SubString(SENTENCE, 4, 2)) = 1 And SubString(SENTENCE, 4, 2) <> '00')
Then Convert(Varchar (2), Convert(Int, SubString(SENTENCE, 4, 2))) + 'M ' End,
Case When (IsNumeric(Right(SENTENCE, 2)) = 1 And Right(SENTENCE, 2) <> '00')
Then Convert(Varchar (2), Convert(Int, Right(SENTENCE, 2))) + 'D' End
)
From Table
Im using a SQL server database and im having a hard time with my case statement. What I'm trying to do is insert a concatenation of attributes into an id field (state_issue_teacher_id) when it equals '' (empty field). The problem is that I have some empty id fields that I need to fill with a unique id that is created by a concatenation of attributes through the case statement.
The empty rows above I need to fill with a concatenation of last_name+first_name+gender+race_ethnicity_code+high_degree_code+position_code+assignment_code.
Here is what I have so far:
SELECT
state_issue_teacher_id,
region_code
+ county_code
+ district_code AS district_code ,
last_name ,
first_name ,
assigned_fte = CASE assign_fte
WHEN '' THEN 0
ELSE CAST(assign_fte AS NUMERIC(18,
2))
END ,
CASE WHEN state_issue_teacher_id = '' THEN
RTRIM([last_name]) + '_' + RTRIM([first_name]) + '_' + RTRIM([gender])
+ '_' + RTRIM([race_ethnicity_code]) + '_' + high_degree_code + '_'
+ position_code + '_' + assignment_code
ELSE state_issue_teacher_id
END,
year_time
FROM dbo.example
When concatenating like that you need to account for two possible problems.
First are all of the fields you are concatenating together varchar (or nvarchar) or are some of them int or numeric? If your datatypes are not varchar or nvarchar, then you need to convert them to use them in a concatentation.
The next possible problem is if one or more of the values can be null. You will need to coalesce any value that could be null with a varchar value (usually a space or empty string for a concatenations although sometimes the value "unknown" works too)) or the concatenation will return null as null concatenated with anything else is null.
SELECT
state_issue_teacher_id,
region_code
+ county_code
+ district_code AS district_code ,
last_name ,
first_name ,
assigned_fte = CASE assign_fte
WHEN '' THEN 0
ELSE CAST(assign_fte AS NUMERIC(18,
2))
END ,
CASE WHEN state_issue_teacher_id = '' THEN
RTRIM(coalesce([last_name],'ValueYouUseInsteadOfNull')) + '_' +
RTRIM(coalesce([first_name],'ValueYouUseInsteadOfNull')) + '_' +
RTRIM(coalesce([gender],'ValueYouUseInsteadOfNull')) + '_' +
RTRIM(coalesce([race_ethnicity_code],'ValueYouUseInsteadOfNull')) + '_' +
coalesce(high_degree_code,'ValueYouUseInsteadOfNull') + '_' +
coalesce(position_code,'ValueYouUseInsteadOfNull') + '_' +
coalesce(assignment_code,'ValueYouUseInsteadOfNull')
ELSE state_issue_teacher_id
END,
year_time
FROM dbo.example
Odds are, you have null in some of those fields.
Give this query a try.
Thank you for everyone that answered the question. I got it to work with the below query.
SELECT
state_issue_teacher_id,
region_code
+ county_code
+ district_code AS district_code ,
last_name ,
first_name ,
gender,
race_ethnicity_code,
high_degree_code,
position_code,
assignment_code,
assigned_fte = CASE assign_fte
WHEN '' THEN 0
ELSE CAST(assign_fte AS NUMERIC(18,
2))
END ,
state_issue_teacher_id =
CASE state_issue_teacher_id WHEN '' THEN
RTRIM([last_name]) + '_' + RTRIM([first_name]) + '_' + RTRIM([gender])
+ '_' + RTRIM([race_ethnicity_code]) + '_' + high_degree_code + '_'
+ position_code + '_' + assignment_code
ELSE state_issue_teacher_id
END,
year_time
FROM dbo.example
How about?:
update example
set state_issue_teacher_id =
RTRIM([last_name]) + '_' + RTRIM([first_name]) + '_' + RTRIM([gender])
+ '_' + RTRIM([race_ethnicity_code]) + '_' + high_degree_code + '_'
+ position_code + '_' + assignment_code
WHERE isnull(state_issue_teacher_id, '') = ''
i have to transform some query i´m using in SQL to Oracle code. I´m having a lot of trouble with tis. Does anyone know any Query transformer o something like that?. Can someone translate some part of this code for me?.
This is the code:
SELECT PRUEBA = CASE (SELECT TIMEATT FROM READER WHERE PANELID = DEVID AND READERID =
MACHINE) WHEN '1' THEN 'P10' ELSE 'P20' END
+ '0001'
+ CAST(YEAR(EVENT_TIME_UTC)AS VARCHAR)
+ Right('0' + Convert(VarChar(2), Month(EVENT_TIME_UTC)), 2)
+ Right('0' + Convert(VarChar(2), DAY(EVENT_TIME_UTC)), 2)
+ Right('0' + Convert(VarChar(2), DATEPART(HOUR,EVENT_TIME_UTC)), 2)
+ Right('0' + Convert(VarChar(2), DATEPART(MINUTE,EVENT_TIME_UTC)), 2)
+ Right('0' + Convert(VarChar(2), DATEPART(SECOND,EVENT_TIME_UTC)), 2)
+ CAST(YEAR(EVENT_TIME_UTC)AS VARCHAR)
+ Right('0' + Convert(VarChar(2), Month(EVENT_TIME_UTC)), 2)
+ Right('0' + Convert(VarChar(2), DAY(EVENT_TIME_UTC)), 2)
+ Right('0' + Convert(VarChar(2), DATEPART(HOUR,EVENT_TIME_UTC)), 2)
+ Right('0' + Convert(VarChar(2), DATEPART(MINUTE,EVENT_TIME_UTC)), 2)
+ Right('0' + Convert(VarChar(2), DATEPART(SECOND,EVENT_TIME_UTC)), 2)
+ Right('00000000' + Convert(VarChar(8), CARDNUM), 8)
+ Right('00000000' + Convert(VarChar(8), (SELECT SSNO FROM EMP WHERE ID = EMPID)), 8),
FROM events
WHERE eventid = 0 AND eventid = 0
and machine in (11) AND DEVID IN (1,2)
and CARDNUM <> 0 AND EMPID <> 0
and EVENT_TIME_UTC between '2006-02-16' AND '2007-02-09'
Many thanks for your help, i´ll keep looking.
Try this:
SELECT CASE (SELECT timeatt FROM reader WHERE panelid = devid AND readerid =
machine)
WHEN '1' THEN 'P10'
ELSE 'P20'
END
|| '0001'
|| To_char(event_time_utc, 'RRRRMMDDHH24MISS')
|| To_char(event_time_utc, 'RRRRMMDDHH24MISS')
|| Lpad(cardnum, 8, '0')
|| Lpad((SELECT ssno
FROM emp
WHERE id = empid), 8, '0') AS prueba
FROM events
WHERE eventid = 0
AND eventid = 0
AND machine IN ( 11 )
AND devid IN ( 1, 2 )
AND cardnum <> 0
AND empid <> 0
AND event_time_utc BETWEEN TO_DATE('2006-02-16', 'RRRR-MM-DD') AND TO_DATE('2007-02-09', 'RRRR-MM-DD')
I have recently had to make the same conversion from a life in tSQL to plSQL (oracle). A couple of "gotcha's" in the code you posted:
1) In tSQL the plus sign (for concatenation)+ is replaced in plSQL with double pipe ||
2) Most of the time you need a "Reference Cursor" (REF CURSOR) declared to put your results into like
PROCEDURE DEMO_SELECT_4_SO(
//other parameters followed by//
P_RESULT OUT REF CURSOR)
IS
BEGIN
OPEN P_RESULT FOR
SELECT
//fields///
FROM
a_table
WHERE
//you want..//
OR (as with a scalar result like your query) a single parameter of the correct type, like:
PROCEDURE DEMO_SELECT_4_SO(
//other parameters followed by//
P_RESULT OUT varchar2(60))
IS
BEGIN
SELECT
//concatenated fields///
INTO
P_RESULT
FROM
a_table
WHERE
//you want..//
NOTICE That select into in plSQL assigns the selected value to the target parameter and does not create a table as it would in tSQL
3) RIGHT (or LEFT) are SUBSTR functions in plSQL
I have found a lot of utility out of this link http://www.techonthenet.com/oracle/index.php for clear explanations of plSQL.