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.
Related
I am trying to split a row when the ';' character appears. I have a list with projects, where one of the columns states which district the project belongs to. However some projects appears in multiple districts and is therefore written like "1;2;3" (District 1, 2 and 3). I want to create three rows form this and split on the ';'.
The error message says:
Msg 240, Level 16, State 1, Line 13
Types don't match between the anchor and the recursive part in column "DataItem" of recursive query "tmp".
I tried the split_string, but discovered my server is 2014 and lacks compatibility.
WITH tmp(Oppdragsnr, Kommune, DataItem, Kommunenr) AS
(
SELECT
Oppdragsnr,
Kommune,
LEFT(Kommunenr, CHARINDEX(';', Kommunenr + ';') - 1),
STUFF(Kommunenr, 1, CHARINDEX(';', Kommunenr + ';'), '')
FROM
oppdragene
UNION all
SELECT
Oppdragsnr,
Kommune,
LEFT(Kommunenr, CHARINDEX(';', Kommunenr + ';') - 1),
STUFF(Kommunenr, 1, CHARINDEX(';', Kommunenr + ';'), '')
FROM
tmp
WHERE
Kommunenr > ''
)
SELECT
Oppdragsnr,
Kommune,
DataItem
FROM
tmp
ORDER BY
Oppdragsnr
I would like the output to be a new table with new rows for every project that appears in multiple districts.
You should probably CAST to INT the DataItem column in the base and recursive part of the query, the following query should work for you
;WITH tmp(Oppdragsnr, Kommune, DataItem, Kommunenr) AS
(
SELECT
Oppdragsnr,
Kommune,
CAST(LEFT(Kommunenr, CHARINDEX(';', Kommunenr + ';') - 1) AS INT),
STUFF(Kommunenr, 1, CHARINDEX(';', Kommunenr + ';'), '')
FROM
oppdragene
UNION all
SELECT
Oppdragsnr,
Kommune,
CAST(LEFT(Kommunenr, CHARINDEX(';', Kommunenr + ';') - 1) AS INT),
STUFF(Kommunenr, 1, CHARINDEX(';', Kommunenr + ';'), '')
FROM
tmp
WHERE
Kommunenr > ''
)
SELECT
Oppdragsnr,
Kommune,
DataItem
FROM
tmp
ORDER BY
Oppdragsnr
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
Is there any ways to SELECT the first and last 2 characters and REPLACE all the other characters with * as shown below?
WA***RT
EX*** ***IL
CH***ON
BE******* *****AY
AP*LE
Thanks in advance!
Spaces skipped:
SELECT
name,
[hidden] = CASE WHEN LEN(name) <= 4 THEN name
ELSE CONCAT(LEFT(name, 2),REPLICATE('*', LEN(name)- 2),RIGHT(name,2))
END
FROM #tab;
If you need spaces and there is only one you can use:
SELECT name,
[hidden] = CASE
WHEN LEN(name) <= 4 THEN name
WHEN CHARINDEX(' ', name) > 0
THEN STUFF(CONCAT(LEFT(name, 2),REPLICATE('*', LEN(name) - 2) ,
RIGHT(name,2)), CHARINDEX(' ', name),1, ' ')
ELSE CONCAT(LEFT(name, 2),REPLICATE('*', LEN(name) - 2) ,RIGHT(name,2))
END
FROM #tab;
LiveDemo
If you use version lower than SQL Server 2012 concatenate string with +.
select left(col, 2) + REPLICATE('*',len(col)-4) + right(col,2) from table;
You can use STUFF.
SELECT STUFF(col, 3, LEN(col)-4, REPLICATE('*',LEN(col)-4))
This doesn't preserve spaces.
To preserve N spaces, you can simply "add back spaces" with STUFF again:
SELECT col,
masked =
coalesce(
-- handle 2 spaces
STUFF(STUFF(
STUFF(col, 3, LEN(col)-4, REPLICATE('*',LEN(col)-4)),
CHARINDEX(' ', col), 1, ' '),
CHARINDEX(' ', col, CHARINDEX(' ', col)+1), 1, ' '),
-- handle 1 spaces
STUFF(
STUFF(col, 3, LEN(col)-4, REPLICATE('*',LEN(col)-4)),
CHARINDEX(' ', col), 1, ' '),
-- normal masking
STUFF(col, 3, LEN(col)-4, REPLICATE('*',LEN(col)-4)),
-- too short to mask
col
)
Demo
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?
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.