I am trying to execute the following SQL query:
SELECT TMP.*,COUNT(*) OVER () AS rCount
FROM (
SELECT venueID,
venueName AS venueName,
venueSpanish AS spanish,
venueAddress + ', ' + venueCity + ', ' + venueState + ' ' + venueZip AS venueAddress,
venueLatLong AS coordinates,
CONVERT(VARCHAR, venueEventDate, 101) + ' # ' + CONVERT(VARCHAR,venueTime) AS dateAndTime,
SUBSTRING(venueLatLong, 1, CHARINDEX(',', venueLatLong)-1) AS Lat,
SUBSTRING(venueLatLong, CHARINDEX(',', venueLatLong) + 1, 1000) AS Lng,
(round(3959 * acos (cos(radians('35.0935409')) *
cos(radians(SUBSTRING(venueLatLong, 1, CHARINDEX(',', venueLatLong)-1))) *
cos(radians(SUBSTRING(venueLatLong, CHARINDEX(',', venueLatLong) + 1, 1000)) -
radians('-85.0856761')) +
sin(radians('35.0935409')) *
sin(radians(SUBSTRING(venueLatLong, 1, CHARINDEX(',', venueLatLong)-1)))), 1, 1)) AS distance
FROM meetUpMarkers) TMP
WHERE distance < 30
However, I am getting this as an error when doing so:
Msg 537, Level 16, State 2, Line 1
Invalid length parameter passed to the LEFT or SUBSTRING function.
Any help would be great to solve this issue!
The problem could be with the arguments passed to the substring function. This condition is being repeated in the query.
CHARINDEX(',', venueLatLong)
If the venueLatLong doesn't contain ,, this argument to the substring is invalid.
This can be avoided by including a where clause.
WHERE CHARINDEX(',', venueLatLong) > 0
It's probably this line:
SUBSTRING(venueLatLong, 1, CHARINDEX(',', venueLatLong)-1) AS Lat
there's no comma in venueLatLong and this results in -1 for the length
Why don't you store Latitude and Longitude in two numeric columns instead of a VarChar?
Related
I have a code like the following:
INSERT INTO [TranslateValidate]..[Policy] ([BirthDate],[FirstName],[LastName])
SELECT
[Table2309].[DOB],
SUBSTRING(Full_Name, CHARINDEX(',', Full_Name) + 2, LEN(Full_Name)),
SUBSTRING(Full_Name, 0, CHARINDEX(',', Full_Name))
FROM [Table2309] AS [Table2309]
WHERE [Table2309].[clientid] = (SELECT
MIN(clientid)
FROM Table2309 T
WHERE T.Date_of_Birth = Table2309.Date_of_Birth
AND T.Substring(Full_Name, CHARINDEX(',', Full_Name) + 2, LEN(Full_Name)) = Table2309.Substring(Full_Name, CHARINDEX(',', Full_Name) + 2, LEN(Full_Name))
AND T.Substring(Full_Name, 0, CHARINDEX(',', Full_Name)) = Table2309.Substring(Full_Name, 0, CHARINDEX(',', Full_Name))
This would have an error message
Cannot find either column "c" or the user-defined function or aggregate "c.Substring", or the name is ambiguous.
If I add [] for Substring part, this would should error message
Invalid column name 'Substring(Full_Name,...
How do I resolve this problem?
You have an extra comma at the end of your select statement:
SELECT
[Table2309].[DOB],
SUBSTRING(Full_Name, CHARINDEX(',', Full_Name) + 2, LEN(Full_Name)),
SUBSTRING(Full_Name, 0, CHARINDEX(',', Full_Name))**,**
Remove this comma and it should work fine.
I am trying to convert from one row to multiple row based on specific character
I copied a script from someone which was working with his/her example but not working for me which is as below
;WITH tmp(NOTEINDX, DataItem, TXTFIELD) AS
(
SELECT
NOTEINDX,
LEFT(TXTFIELD, CHARINDEX('#', TXTFIELD + '#') - 1),
STUFF(TXTFIELD, 1, CHARINDEX('#', TXTFIELD + '#'), '')
FROM SY03900
UNION all
SELECT
NOTEINDX,
LEFT(TXTFIELD, CHARINDEX('#', TXTFIELD + '#') - 1),
STUFF(TXTFIELD, 1, CHARINDEX(',', TXTFIELD + '#'), '')
FROM tmp
WHERE
TXTFIELD > ''
)
SELECT
NOTEINDX,
DataItem
FROM tmp
ORDER BY NOTEINDX
However I am getting below error
Msg 402, Level 16, State 1, Line 6
The data types text and varchar are incompatible in the add operator.
I'm using SQL Server 2008
Thanks,
Hatim
Please assist me with the SQL query shown below.
This is the error I get:
System.Exception: TransformerException ---> Library.Utilities.TransformerException: Transformer transforming [0000000000085153227:documentId] using [4:transformId] with time remaining [00:00:54.4517645:TimeSpan] output document is [0000000000085153231:documentId] ---> and so on...
I'm training to retrieve this value 85153227 only.
Please assist
SELECT
SUBSTRING(message, 0, CHARINDEX('System.Exception', message)) + ' ' +
CASE
WHEN CHARINDEX('Transformer transforming [', message) > 0
THEN SUBSTRING(message, CHARINDEX('[',message), LEN(message)-CHARINDEX('[', message))
ELSE ''
END
FROM
dbo.Documents
Thank you
DECLARE # AS VARCHAR(MAX)=
'System.Exception: TransformerException ---> Library.Utilities.TransformerException: Transformer transforming [0000000000085153227:documentId] using [4:transformId] with time remaining [00:00:54.4517645:TimeSpan] output document is [0000000000085153231:documentId]'
SELECT
CAST(
SUBSTRING(#,
CHARINDEX('Transformer transforming [',#,0) + LEN('Transformer transforming [')
, CHARINDEX(':documentId]',#,0) - ( CHARINDEX('Transformer transforming [',#,0) + LEN('Transformer transforming [') ))
AS BIGINT)
--OUTPUT
--------------------
85153227
(1 row affected)
It's hard to determine an exact response to this because you did not identify if the exception is always structured the same. With what is provided, this is what I would do.
Removing the leading string of characters with STUFF; STUFF([Message], 1, CHARINDEX(N'Transformer transforming [', [Message]) + 25, N'')
Then assuming that the string of characters with the number is 19 characters long, I then do a SUBSTRING to find the first non-zero character. SUBSTRING({field}, PATINDEX('%[^0]%', {field} + '.'), LEN({field})) substituting step 1 into this string for {field} (I tried to simplify the code here).
Here is the SQL statement showing this.
IF OBJECT_ID(N'tempdb..#t') IS NOT NULL DROP TABLE #t
SELECT CAST('System.Exception: TransformerException ---> Library.Utilities.TransformerException: Transformer transforming [0000000000085153227:documentId] using [4:transformId] with time remaining [00:00:54.4517645:TimeSpan] output document is [0000000000085153231:documentId] ' AS NVARCHAR(MAX))
AS [Message]
INTO #t
SELECT *,
STUFF([Message], 1, CHARINDEX(N'Transformer transforming [', [Message]) + 25, N'') AS BeginningCharactersStripped,
SUBSTRING(LEFT(STUFF([Message], 1, CHARINDEX(N'Transformer transforming [', [Message]) + 25, N''), 19), PATINDEX('%[^0]%', LEFT(STUFF([Message], 1, CHARINDEX(N'Transformer transforming [', [Message]) + 25, N''), 19) + '.'), LEN(LEFT(STUFF([Message], 1, CHARINDEX(N'Transformer transforming [', [Message]) + 25, N''), 19)))
AS Number
FROM #t
WHERE [Message] like N'%Transformer transforming [[]%'
Based on what you provided I modified your query a bit to get the output you want. You may need to adjust depending on your data.
SELECT substring(message,0,charindex('System.Exception',message)) + ' ' +
CASE WHEN charindex('Transformer transforming [',message) > 0
THEN Convert(Varchar(8000), Convert(BigInt, Substring(message,charindex('[',message) + 1, charindex(':documentid]',message)-(CharIndex('[',message)+1)))) ELSE '' END
FROM dbo.Documents
You can try this:
with MsgWithZeroesTbl as
(SELECT 'System.Exception ' +
CASE
WHEN charindex('Transformer transforming [',message) >= 0
THEN substring(message,charindex('[',message) + 1, charindex(':',message, charindex('[',message))- (charindex('[',message) + 1)) ELSE ''
END as MsgWithZeros
FROM dbo.Documents )
Select substring(MsgWithZeros, patindex('%[^0]%',MsgWithZeros), 10) from MsgWithZeroesTbl
I came up with:
DECLARE #string varchar(8000) =
'System.Exception: TransformerException ---> Library.Utilities.TransformerException: Transformer transforming [0000000000085153227:documentId] using [4:transformId] with time remaining [00:00:54.4517645:TimeSpan] output document is [0000000000085153231:documentId] --->'
SELECT cast(substring(#string, p1.p, p2.p-p1.p) as bigint)
FROM (VALUES (patindex('%[0-9]%', #string))) p1(p)
CROSS APPLY (VALUES (charindex(':', #string, p1.p+1))) p2(p);
Output:
85153227
Just wandering, how can I replace the last comma in the string to "and" in SQL Server.
I have the following valuable:
#test = 'a,b,c,f,w'
How can I replace the last comma in the string to "and" as the output:
'a,b,c,f and w'
That is weird. You can do:
set #test = left(#test, len(#test) - charindex(',', reverse(#test))) + ' and ' + stuff(#test, 1, len(#test) - charindex(',', reverse(#test)) + 1, '')
Another way to achieve it :
SELECT reverse(STUFF(reverse(#test), CHARINDEX(',', reverse(#test)), 1, ' dna '))
While trying to execute the below query
Declare #t table (id int, string varchar(1000))
INSERT INTO #t (id, string)
SELECT 1, 'zxzzxx,ppppbppp,trtrtr,tyyt,hgghh,fefew,rewr,rwerer'
;WITH test (id, lft, rght, idx)
AS
(
SELECT t.id
,LEFT(t.string, CHARINDEX(', ', t.string) - 1)
,SUBSTRING(t.string, CHARINDEX(', ', t.string) + 2, DATALENGTH(t.string))
,0
FROM #t t
UNION ALL
SELECT c.id
,CASE WHEN CHARINDEX(', ', c.rght) = 0 THEN c.rght ELSE LEFT(c.rght, CHARINDEX(', ', c.rght) - 1) END
,CASE WHEN CHARINDEX(', ', c.rght) > 0 THEN SUBSTRING(c.rght, CHARINDEX(', ', c.rght) + 2, DATALENGTH(c.rght))
ELSE '' END
,idx + 1
FROM test c
WHERE DATALENGTH(c.rght) > 0
)
select id, lft from test
I am getting the below error
Msg 537, Level 16, State 2, Line 8
Invalid length parameter passed to the LEFT or SUBSTRING function.
but the same works for SELECT 1, 'the, quick, brown, fox, jumped, over, the, lazy, dog'
Please help
It seems to be a space missing between your words.
You are currently looking for charindex of ', ' not ','.
And the string does not have any match of ', '.
This Error message happens usually when you do these steps;
When you use Substring,
left, right functions.
When you use CharIndex (used in
the field, the selected word or word
search, or the length of a character
to be inadequate)
The return value is returned to each server in the query expression, the result of the transaction 0 (zero) returns, if error returns -1
This will not result in errors for the server to return a value of -1 or are compared objects.