ISNULL is still returning NULL values - sql

I've this query:
SELECT cod_commessa, (ISNULL(ccomm.costi, 0) + ISNULL(ccomm.imp_costi, 0) - ISNULL(ccomm.rid_costi, 0)) -
((SELECT ISNULL(costi, 0) AS Expr1
FROM t_costi_commesse AS tbs
WHERE (cod_commessa = ccomm.cod_commessa) AND (anno = YEAR(DATEADD(m, - 1, CAST(CAST(ccomm.anno AS varchar) + REPLICATE('0',
2 - LEN(CAST(ccomm.mese AS varchar))) + CAST(ccomm.mese AS varchar) + '01' AS smalldatetime)))) AND (mese = MONTH(DATEADD(m, - 1,
CAST(CAST(ccomm.anno AS varchar) + REPLICATE('0', 2 - LEN(CAST(ccomm.mese AS varchar))) + CAST(ccomm.mese AS varchar)
+ '01' AS smalldatetime))))) +
(SELECT ISNULL(imp_costi, 0) AS Expr1
FROM t_costi_commesse AS tbs
WHERE (cod_commessa = ccomm.cod_commessa) AND (anno = YEAR(DATEADD(m, - 1, CAST(CAST(ccomm.anno AS varchar) + REPLICATE('0',
2 - LEN(CAST(ccomm.mese AS varchar))) + CAST(ccomm.mese AS varchar) + '01' AS smalldatetime)))) AND (mese = MONTH(DATEADD(m, - 1,
CAST(CAST(ccomm.anno AS varchar) + REPLICATE('0', 2 - LEN(CAST(ccomm.mese AS varchar))) + CAST(ccomm.mese AS varchar)
+ '01' AS smalldatetime))))) -
(SELECT ISNULL(rid_costi, 0) AS Expr1
FROM t_costi_commesse AS tbs
WHERE (cod_commessa = ccomm.cod_commessa) AND (anno = YEAR(DATEADD(m, - 1, CAST(CAST(ccomm.anno AS varchar) + REPLICATE('0',
2 - LEN(CAST(ccomm.mese AS varchar))) + CAST(ccomm.mese AS varchar) + '01' AS smalldatetime)))) AND (mese = MONTH(DATEADD(m, - 1,
CAST(CAST(ccomm.anno AS varchar) + REPLICATE('0', 2 - LEN(CAST(ccomm.mese AS varchar))) + CAST(ccomm.mese AS varchar)
+ '01' AS smalldatetime)))))) AS 'costi'
FROM t_costi_commesse AS ccomm INNER JOIN
t_commesse AS comm ON ccomm.cod_commessa = comm.codcema
This query is returning values correctly, but sometimes I have some values in column 'costi' that still are NULL, even if I apply ISNULL to every value. The weird thing is that sometimes a NULL value is correctly replaced with 0, but sometimes not.
I'm also sure that the original value is NULL (and not maybe a string 'NULL'), because SQL Server Management Studio changes correctly the background color.
So, I can understand if my query is incorrect or if ISNULL in not working properly for an unknown reason. Any suggestion?

Move your ISNULLs that you're applying within your subqueries to the outside.
This:
select 1,(select object_id from sys.objects where 1=0)
Produces:
1, NULL
Despite the fact that object_id isn't nullable, because the subquery returns no rows. You can't fix it in the select of the subquery because there are no rows. So this:
select 1,(select ISNULL(object_id,0) from sys.objects where 1=0)
doesn't fix the issue but this:
select 1,ISNULL((select object_id from sys.objects where 1=0),0)
does.

Related

Finding matching values in a field, that a seperated by spaces

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 + ' - - -'), '')
),
'')
)
) <>'-'

SQL Server : generate a different value for each output row

I am running a query in which I need the SCORE to have a different output for each row. I am able to do it per execution but I have had no luck with RAND as I get a convert from varchar to int error. I need variable number per execution within a range with no decimals. Is there anyway to do this? Any help is appreciated.
SELECT
'Row_Number^FEED_date^database_id^station_id^Form_Type^FBCS_CLAIM_ID^FBCS_LINE_ID^Pay^SCORE^score_date^reason_code^reason_description'
UNION ALL
SELECT
CAST(ROW_NUMBER() OVER ( ORDER BY ( SELECT 1 ) ) AS VARCHAR(20)) + '^' +
CONVERT(VARCHAR, getdate(), 112)+ '^' +
'FBCSTrans'+ '^' + ---took off the 1 so if problems put it back
stationnumber+ '^' +
case when claimtype = 0 then 'HCFA 1500' else 'UB 1450' end+ '^' +
CAST(Claims.claimid AS VARCHAR(50)) + '^' +
CAST(LineID AS VARCHAR(50)) + '^' +
'Y'+ '^' +
**RAND() * 100000 AS random_number** + '^' +
CONVERT(VARCHAR, getdate(), 112)+ '^' +
'' + '^' + ''
FROM
Claims
JOIN
ClaimLines ON Claims.Claimid = Claimlines.Claimid
JOIN
Facilities ON Claims.FacilityID = Facilities.FacilityID
WHERE
Claims.ClaimId IN (SELECT TOP(100000) ClaimId
FROM CLAIMS
WHERE Claims.RecordStatus = 55 AND facilityid = 40)
USE [DATABASE NAME]
SELECT 'Row_Number^FEED_date^database_id^station_id^Form_Type^FBCS_CLAIM_ID^FBCS_LINE_ID^Pay^SCORE^score_date^reason_code^reason_description'
UNION ALL
SELECT
CAST(ROW_NUMBER() OVER ( ORDER BY ( SELECT 1 ) ) AS VARCHAR(20)) + '^' +
CONVERT(VARCHAR, getdate(), 112)+ '^' +
'DATABASE NAME'+ '^' + ---took off the 1 so if problems put it back
stationnumber+ '^' +
case when claimtype = 0 then 'HCFA 1500' else 'UB 1450' end+ '^' +
CAST(Claims.claimid AS VARCHAR(50)) + '^' +
CAST(LineID AS VARCHAR(50)) + '^' +
'Y'+ '^' +
CAST (FLOOR(RAND(CHECKSUM(NEWID()))*(1000-0+1)+100) As NVARCHAR) + '^' +
CONVERT(VARCHAR, getdate(), 112)+ '^' +
'' + '^' + ''
FROM Claims
JOIN ClaimLines on Claims.Claimid = Claimlines.Claimid
JOIN Facilities ON Claims.FacilityID = Facilities.FacilityID
WHERE Claims.ClaimId IN (SELECT TOP(100000)ClaimId FROM CLAIMS WHERE Claims.RecordStatus = 55 and facilityid=40)

Reformatting data in column

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

case and concatenation, sql

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, '') = ''

transform sql query to oracle

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.