invalid length parameter passed to the left or substring function charindex - tsql

I need to translate the Oracle SQL-code
select URL, TO_NUMBER(NVL( substr(URL,INSTR(URL,'pageId=') + 7, INSTR(URL,'&')- (INSTR(URL,'pageId=') + 7) ) , substr(URL,INSTR(URL,'pageId=') + 7) )) PageId
from remotelink
WHERE INSTR(URL,'pageId')>0;
to T-SQL (MS SQL-Server 2012)
My translated Code looks like this:
select URL, CAST(ISNULL( substring(URL,CHARINDEX('pageId=', URL) + 7, CHARINDEX('&', URL)- (CHARINDEX('pageId=', URL) + 7) ) , substring(URL,CHARINDEX('pageId=', URL) + 7, datalength(URL)) )AS numeric) PageId
from JIRA_TEST.jiraschema.remotelink
WHERE CHARINDEX('pageId', URL) >0
I receive the error
"invalid length parameter passed to the left or substring function"
Any hints?

Related

nvarchar to time conversion

I have to develop system which is accept data (in database) from another system. The problem I face is all DATE and TIME data is in NVARCHAR. I have to convert to DATE and TIME in order to calculate the duration. I am using SQL Server 2008 R2.
Data example:
STR_YMD STR_HMS
--------------------
20150101 151000
20090807 123009
20130113 145602
20090515 061700
How could I convert this data into following DATETIME data?
STR_DT
--------------------
2015-01-01 15:10:00
2009-08-07 12:30:09
2013-01-13 14:56:02
2009-05-15 06:17:00
Hopefully someone could help me. Thanks in advance
Bet
Something like this ?
SELECT CAST(SUBSTRING(STR_YMD, 1, 4) + '/' + SUBSTRING(STR_YMD, 5, 2) + '/' +
SUBSTRING(STR_YMD, 7, 2) + ' ' + SUBSTRING(STR_HMS, 1, 2) + ':' +
SUBSTRING(STR_HMS, 3, 2) + ':' + SUBSTRING(STR_HMS, 5, 2) AS DateTime)
EDIT : You can use LEFT function to be compatible with short time formats :
SELECT CAST(SUBSTRING(STR_YMD, 1, 4) + '/' + SUBSTRING(STR_YMD, 5, 2) + '/' +
SUBSTRING(STR_YMD, 7, 2) + ' ' + SUBSTRING(LEFT(STR_HMS+'000000', 6), 1, 2) + ':' +
SUBSTRING(LEFT(STR_HMS+'000000', 6), 3, 2) + ':' +
SUBSTRING(LEFT(STR_HMS+'000000', 6), 5, 2) AS DateTime)

query throwing error in web report writer

I have a query/view that is created to be used in a report. When I run in the SMS and in SSRS it run fine. But when I connect the view to the tool that generates our reports it throws the following error. Incorrect syntax near '.8'. When I contact the support for this product they say it has to do with how we calculate the 8thgradyear . I have placed the code below. Any suggestions.
SELECT
dbo.studemo.suniq,
dbo.studemo.ident,
dbo.studemo.lastname,
dbo.studemo.firstname,
dbo.studemo.emailaddr AS stuemail,
dbo.studemo.birthdate,
dbo.track.schoolc,
dbo.school.schname,
dbo.stustat.graden,
dbo.stustat.edate,
dbo.zstustat.descript AS status,
RTRIM(dbo.facdemo.lastname) + ' ' + dbo.facdemo.firstname AS advisor,
dbo.track.schyear,
SUM(8) - dbo.stustat.graden + dbo.track.schyear AS [8thgradyear],
sf.Email, LOWER(sf.Username) AS [user],
LOWER(RIGHT(SUM(8) - dbo.stustat.graden + dbo.track.schyear, 2) + LEFT(dbo.studemo.firstname, 1) + REPLACE(REPLACE(REPLACE(dbo.studemo.lastname, '-', ''), ' ', ''), '''', '') + RIGHT(dbo.studemo.ident, 3)) AS newuser,
CONVERT(varchar(8), dbo.studemo.birthdate,1) AS password,
'STUDENTS' + '/' + (CASE WHEN track.schoolc IN ('19', '43', '17', '23') THEN 'Middle' ELSE 'Elementary' END) + '/' + dbo.school.schname AS neworg,
sf.OU, sf.LastLoginTime
FROM dbo.studemo INNER JOIN
dbo.stustat ON dbo.studemo.suniq = dbo.stustat.suniq INNER JOIN
dbo.track ON dbo.stustat.trkuniq = dbo.track.trkuniq INNER JOIN
dbo.zstustat ON dbo.stustat.stustatc = dbo.zstustat.stustatc INNER JOIN
dbo.facdemo ON dbo.stustat.funiq = dbo.facdemo.funiq LEFT OUTER JOIN
dbo.vw_google_OU AS sf ON sf.Firstname = dbo.studemo.firstname AND sf.Lastname = dbo.studemo.lastname INNER JOIN
dbo.school ON dbo.school.schoolc = dbo.track.schoolc
WHERE (dbo.stustat.stustatc IN
(SELECT stustatc
FROM dbo.zstustat AS zstustat_1
WHERE (snstatus IN ('A', 'M', 'P')))) AND (dbo.stustat.xdate IS NULL OR
dbo.stustat.xdate < dbo.stustat.edate) AND (dbo.track.schoolc NOT IN ('P34', 'P24', '802', '801'))
GROUP BY dbo.studemo.suniq, dbo.studemo.ident, dbo.studemo.lastname, dbo.studemo.firstname, dbo.studemo.birthdate, RIGHT(dbo.studemo.ident, 3), dbo.track.schoolc,
dbo.stustat.graden, dbo.zstustat.descript, RTRIM(dbo.facdemo.lastname) + ' ' + dbo.facdemo.firstname, dbo.stustat.edate, dbo.studemo.gradyear, dbo.track.schyear,
sf.Email, CONVERT(varchar(8), dbo.studemo.birthdate, 1), sf.Username, dbo.school.schname, sf.OU, dbo.studemo.emailaddr, sf.LastLoginTime
SUM function was introduced in SQL Server 2008 and your tool must be using and older version of SQL Server.

How to use datetime value in an SSIS expression builder to formulate SQL command?

I am trying to use Ado.Net Sql command to select data from a table with a date filter.
SELECT COLUMN1
, COLUMN2
FROM TABLENAME
WHERE DATE_INSERTED > #[User::LastInsertDate]
Both Date_Inserted and #[User::LastInsertedDate] are of type DateTime. When I try to evaluate expression in the expression builder l get the following error;
The expression might contain an invalid token, an incomplete token, or
an invalid elemnt, it might not be well-formed, or might be missing
part of a required element such as a parenthesis.
Problem diagnosis from original revision of accepted answer:
Here is my understanding of your question. I believe that you created
two variables under package scope. A variable named LastInsertDate
of DateTime data type and another variable named SqlQuery of
String data type to store the SQL SELECT command.
You set the EvaluateAsExpression property on variable SqlQuery to
True. You then entered the following command SELECT COLUMN1, COLUMN2 FROM TABLENAME WHERE DATE_INSERTED > #[User::LastInsertDate]
When you clicked EvaluateAsExpression, you got the following error
message:
Expression cannot be evaluated. Additional information: Attempt to
parse the expression "SELECT COLUMN1, COLUMN2 FROM TABLENAME WHERE
DATE_INSERTED > #[User:LastInsertDate]" failed. The expression might
contain an invalid token, an incomplete token, or an invalid element.
It might not be well-formed, or might be missing part of a required
element such as a parenthesis.
The issue here is that the value you are trying to store in the variable SqlQuery is not enclosed within double quotes. The string value of the dynamic query should be enclosed within double quotes.
While enclosing the text in double quotes, you cannot use the datetime variable LastInsertDate as it is. You need to convert the date time variable to string but if you simply convert datetime value to string, you might land into unexpected format. To be on the safe side, I would recommend using DATEPART function to translate the datetime value to string of format YYYY-MM-DD hh:mi:ss. Here is the complete expression that will do that.
"SELECT COLUMN1, COLUMN2 FROM TABLENAME WHERE DATE_INSERTED > '" +
(DT_STR, 4, 1252) DATEPART("yyyy", #[User::LastInsertDate])
+ "-" + RIGHT("0" + (DT_STR, 2, 1252) DATEPART("mm", #[User::LastInsertDate]), 2)
+ "-" + RIGHT("0" + (DT_STR, 2, 1252) DATEPART("dd", #[User::LastInsertDate]), 2)
+ " " + RIGHT("0" + (DT_STR, 2, 1252) DATEPART("hh", #[User::LastInsertDate]), 2)
+ ":" + RIGHT("0" + (DT_STR, 2, 1252) DATEPART("mi", #[User::LastInsertDate]), 2)
+ ":" + RIGHT("0" + (DT_STR, 2, 1252) DATEPART("ss", #[User::LastInsertDate]), 2)
When you click Evaluate Expression, you will see the string with the date time value in the Evaluated value section.
Hope that helps.

Is it possible to refactor this statement to remove the subquery?

I'm trying to take data from one column, MyTable.SSN, and copy it to another in the same table, MyTable.SSNWithDashes, just formatted differently. If MyTable.SSN doesn't have exactly 9 digits, I don't care to process it at all.
I've tried this:
IF( SELECT LEN( [SSN] ) FROM [MyTable] ) = 9
UPDATE [MyTable] SET [SSNWithDashes] = LEFT( [SSN], 3 ) + '-' + SUBSTRING( [SSN], 4, 2 ) + '-' + RIGHT( [SSN], 4 )
ELSE
UPDATE [MyTable] SET [SSNWithDashes] = NULL
While this works, it throws an error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
While I do understand what the warning is saying, I'm not really sure how to go about this differently.
How can I refactor this to remove that warning (and perhaps read a little cleaner)?
UPDATE dbo.[MyTable]
SET [SSNWithDashes] = CASE
WHEN LEN(SSN) = 9 THEN
LEFT([SSN],3) + '-' + SUBSTRING([SSN],4,2) + '-' + RIGHT([SSN],4)
ELSE NULL
END;
Assuming your SSNWithDashes is already NULL then
UPDATE dbo.[MyTable]
SET [SSNWithDashes] = LEFT([SSN],3) + '-' + SUBSTRING([SSN],4,2) + '-' + RIGHT([SSN],4)
WHERE LEN(SSN) = 9
Every other rows remain NULL

Get just the File Name out of Url

I'm trying to get a substring of the .aspx filename here but not quite sure how to go about it.
So I don't want the filepath, just the filename:
e.g. http://somesite.com/directory1/directory2/SomePage.aspx
I just need SomePage.aspx
You can;
;with EG(fn) as (
select 'http://somesite.com/directory1/directory2/SomePage.aspx' union
select '' union
select '/xxx.php'
)
select
right(fn, charindex('/', reverse(fn) + '/') - 1)
from EG
-------------
xxx.php
SomePage.aspx
(1st occurrence of / in a reversed url; add a / to handle empty/no-slash strings)