I'm using this code:
WITH cte AS(SELECT panelname FROM inventorypanelcaptions UNION ALL SELECT CAST(0 AS VARCHAR(50)) FROM INVENTORYPANELCAPTIONS) SELECT CASE WHEN cast(min(right(panelname, 2) + 1) as varchar(50)) < 10 THEN 'o0' ELSE 'o' END + cast(min(right(panelname, 2) + 1) as varchar(50)) FROM cte WHERE NOT EXISTS ( SELECT panelname FROM inventorypanelcaptions WHERE right(inventorypanelcaptions.panelname, 2)= right(cte.panelname, 2)+1) ", con);
I'm getting incorrect syntax near 'With'
To make it work in SQL 2000, you can move it to a subquery
SELECT CASE WHEN cast(min(right(panelname, 2) + 1) as varchar(50)) < 10 THEN 'o0' ELSE 'o' END + cast(min(right(panelname, 2) + 1) as varchar(50))
FROM (
SELECT panelname FROM inventorypanelcaptions UNION ALL SELECT CAST(0 AS VARCHAR(50))
FROM INVENTORYPANELCAPTIONS
) cte
WHERE NOT EXISTS ( SELECT panelname
FROM inventorypanelcaptions
WHERE right(inventorypanelcaptions.panelname, 2)= right(cte.panelname, 2)+1
) ", con);
Related
I have these rows
14-L-100-10008-G03S-N
1-DR-100-20057-G48-N
2-CL-100-12133-B12-P
2-FG-100-12202-B01-N
2-G-100-15520-B48-N
2-GM-100-10024-B01-N
2-HC-100-10022-G03S-N
2-HC-100-10023-G03S-N
2-HC-100-20023-G03S-N
32-G-100-15518-F03P2-N
32-G-100-15518-F03P2-N
I just need these parts .how can i get these parts using sql server code :
result:G03S,G48,B12,B01,B48,B12 and ...
You can as the below:
;WITH CTE
AS
(
select '14-L-100-10008-G03S-N' Val UNION ALL
select '1-DR-100-20057-G48-N' Val UNION ALL
select '2-CL-100-12133-B12-P' Val UNION ALL
select '2-FG-100-12202-B01-N' Val UNION ALL
select '2-G-100-15520-B48-N' Val UNION ALL
select '2-GM-100-10024-B01-N' Val UNION ALL
select '2-HC-100-10022-G03S-N' Val UNION ALL
select '2-HC-100-10023-G03S-N' Val UNION ALL
select '2-HC-100-20023-G03S-N' Val UNION ALL
select '32-G-100-15518-F03P2-N' Val UNION ALL
select '32-G-100-15518-F03P2-N'
)
SELECT REVERSE(SUBSTRING(REVERSE(Val), CHARINDEX('-', REVERSE(Val), 0) + 1, CHARINDEX('-', REVERSE(Val), CHARINDEX('-', REVERSE(Val), 0) + 1) - CHARINDEX('-', REVERSE(Val)) - 1))
FROM
CTE
Edit:
SELECT
(
SELECT REVERSE(SUBSTRING(REVERSE(Val), CHARINDEX('-', REVERSE(Val), 0) + 1, CHARINDEX('-', REVERSE(Val), CHARINDEX('-', REVERSE(Val), 0) + 1) - CHARINDEX('-', REVERSE(Val)) - 1)) + ' ,'
FROM
CTE
FOR XML PATH ('')
) Result
Result: G03S ,G48 ,B12 ,B01 ,B48 ,B01 ,G03S ,G03S ,G03S ,F03P2 ,F03P2 ,
SELECT LEFT(A.Result, LEN(A.Result) -1) FROM
(SELECT (SELECT REVERSE(SUBSTRING(REVERSE(Val), CHARINDEX('-', REVERSE(Val), 0) + 1, CHARINDEX('-', REVERSE(Val), CHARINDEX('-', REVERSE(Val), 0) + 1) - CHARINDEX('-', REVERSE(Val)) - 1)) + ' ,' FROM CTE FOR XML PATH ('') ) Result) A
Result: G03S ,G48 ,B12 ,B01 ,B48 ,B01 ,G03S ,G03S ,G03S ,F03P2 ,F03P2
Using one of the splitstring functions from here..
create table #t
(
id varchar(max)
)
insert into #t
select '14-L-100-10008-G03S-N'
union all
select '1-DR-100-20057-G48-N'
select item
from #t t1
cross apply
(select * ,row_number() over (partition by t1.id order by t1.id) as rn from [dbo].[SplitStrings_Numbers](t1.id,'-')
) b
where rn=5
I would like to split a string by commas (,) or pipe (|) to each character in SQL SERVER. Example 'APPLE'. Expected result: 'A|P|P|L|E'. Preferably without creating function.
You can do it with CTE:
DECLARE #s NVARCHAR(MAX) = 'APPLE'
DECLARE #result NVARCHAR(MAX)
;WITH cte(N, S) AS
(
SELECT 1 AS N, SUBSTRING(#s, 1, 1)
UNION ALL
SELECT N + 1, SUBSTRING(#s, N + 1, 1)
FROM cte
WHERE N < LEN(#s)
)
SELECT #result = COALESCE(#result + '|', '') + S FROM cte
SELECT #result
Output:
A|P|P|L|E
Or even shorter version:
DECLARE #s NVARCHAR(MAX) = 'APPLE'
;WITH cte(N, S, D) AS
(
SELECT 1 AS N, SUBSTRING(#s, 1, 1), D = SUBSTRING(#s, 1, 1)
UNION ALL
SELECT N + 1, SUBSTRING(#s, N + 1, 1), D = D + '|' + SUBSTRING(#s, N + 1, 1)
FROM cte
WHERE N < LEN(#s)
)
SELECT TOP 1 D FROM cte
ORDER BY N DESC
You could use a concept like the "Tally Table String Splitter" to achieve what you want.
http://www.sqlservercentral.com/articles/Tally+Table/72993/
DECLARE #txt varchar(50) ='APPLE'
;WITH cte(x) as
(
SELECT top (len(#txt)) ';'
+ substring(#txt, row_number() over (order by (select 1)), 1)
FROM master..spt_values x1
cross join
master..spt_values x2
FOR XML PATH('')
)
SELECT stuff(x, 1, 1, '')
FROM CTE
Result
A;P;P;L;E
Ok i have 2 questions 1) I've googled that error(in title) but I cant seem to get it to help me, my proc:
CREATE PROCEDURE [dbo].[p_Target]
AS
BEGIN
SET NOCOUNT ON
CREATE TABLE #tmp1
(
AUD_ID BIGINT,
RowCounter BIGINT,
DistinctCounter BIGINT,
NACounter BIGINT,
Total BIGINT,
[Status] VARCHAR(MAX)
)
INSERT INTO #tmp1 EXEC [p_GetCompleteIncompleteNaOverviewSCORE]
DECLARE #Total AS INT
SELECT #Total = COUNT(*)
FROM (
SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101)) < CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,GETDATE()), 101))
THEN 'Over Due: '
ELSE 'On Time: ' END AS [Target Status]
FROM #tmp1 t1 INNER JOIN dbo.Audit t2
ON t1.AUD_ID = t2.AUD_ID
WHERE t1.[Status] = 'Open') DER
SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255)) + ' of ' + CAST(#Total AS NVARCHAR(255))) AS TargetStatus, (SELECT COUNT(*)) * 100 / (SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255))) AS [Count]
FROM (
SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101)) < CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,GETDATE()), 101))
THEN 'Over Due: '
ELSE 'On Time: ' END AS [Target Status]
FROM #tmp1 t1 INNER JOIN dbo.Audit t2
ON t1.AUD_ID = t2.AUD_ID
WHERE t1.[Status] = 'Open') DER
GROUP BY [Target Status]
END
GO
I've tied adding ) to NVARCHAR(255))) AS [Count] but still errors,
2)I need to get AS [Count] as a percentag, if anyone can help? That is my try code maybe i am on the right track.?
You're missing a close paren on your final subselect. Also, you're missing the 'S' in SET NOCOUNT. Updates below.
CREATE PROCEDURE [dbo].[p_Target]
AS
BEGIN
SET NOCOUNT ON
CREATE TABLE #tmp1
(
AUD_ID BIGINT,
RowCounter BIGINT,
DistinctCounter BIGINT,
NACounter BIGINT,
Total BIGINT,
[Status] VARCHAR(MAX)
)
INSERT INTO #tmp1 EXEC [p_GetCompleteIncompleteNaOverviewSCORE]
DECLARE #Total AS INT
SELECT #Total = COUNT(*)
FROM (
SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101)) < CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,GETDATE()), 101))
THEN 'Over Due: '
ELSE 'On Time: ' END AS [Target Status]
FROM #tmp1 t1 INNER JOIN dbo.Audit t2
ON t1.AUD_ID = t2.AUD_ID
WHERE t1.[Status] = 'Open') DER
SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255)) + ' of ' + CAST(#Total AS NVARCHAR(255))) AS TargetStatus, (SELECT COUNT(*)) * 100 / (SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255))) AS [Count]
FROM (
SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101)) < CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,GETDATE()), 101))
THEN 'Over Due: '
ELSE 'On Time: ' END AS [Target Status]
FROM #tmp1 t1 INNER JOIN dbo.Audit t2
ON t1.AUD_ID = t2.AUD_ID
WHERE t1.[Status] = 'Open') DER
GROUP BY [Target Status]
)
END
GO
Is it the missing 'S' on SET NOCOUNT ON after the initial begin perhaps?
It looks like your SELECT statement above your last last FROM statement has an un-closed parentheses. That would make the parser throw an error when it hits the END statement, since it still thinks it's working on the SELECT.
SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255)) + ' of ' + CAST(#Total AS NVARCHAR(255))) AS TargetStatus, (SELECT COUNT(*)) * 100 / (SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255))) AS [Count]
^
Whenever you get an incorrect Syntax error it's best to carefully go through your code and check that all statements are well formed and complete. Usually google can't help you, since it's very specific to your code. A good approach to finding the fault is to locate the point of the error (END in this case) and work backwards. The cause of the error may be quite far above the actual point that the error is thrown, but it will be somewhere above the error. And reading backwards can help you catch simple mistakes.
Create Table TblAccount (UserName nvarchar(20) Primary Key,UserPwd varchar(20) not null)
Declare #i int
Set #i=1
While #i<=10
I have one question
BEGIN
Insert INTO TblAccount Values('Seng Vitou'+CAST(#i AS VARCHar(2)),REVERSE('pwd'+CAST(#i as varchar(2))
SET #i=#i+1
End
Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'SET'.
create PROC [dbo].[Sample]
#fromDate datetime, #toDate datetime, #office varchar(30)
AS
declare #char varchar(200)
DECLARE #Temp TABLE (ID int, Name varchar(50), Countt int, Reason varchar(20))
INSERT INTo #Temp (ID, Name, Countt, Reason)
SELECT DD.ID, O.Name, Count(DD.Reason) Countt, convert(varchar,DD.Reason) Reason FROM samp1 AS DD
INNER JOIN samp3 AS O ON O.ID = DD.ID
select #char = coalesce(#char + ',', '') + reason from #Temp
select *
FROM
(
select distinct ID, Name, Reason, sum(Countt) as Countt from #Temp group by Name, Reason, ID
)P
PIVOT
(
SUM(Countt)
FOR Reason
IN (select #char)
) AS pvt
Error: Msg 156, Level 15, State 1, Procedure Sample, Line 45
Incorrect syntax near the keyword 'select'.
can anybody help me
SELECT #char = COALESCE(#char + ',[' + reason + ']', '[' + reason + ']')
FROM #Temp
DECLARE #pvtQuery VARCHAR(2500)
SET #pvtQuery =
'
SELECT ID, Name, ' + #char + '
FROM
(
SELECT DISTINCT ID, Name, Reason, SUM(Countt) AS Countt
FROM #Temp
GROUP BY Name, Reason, ID
) P
PIVOT
(
SUM(Countt)
FOR Reason
IN (' + #char + ')
) AS pvt
'
EXEC (#pvtQuery)
Could anyone help me understand why the following query works fine in SQL 2000 and not in SQL 2005
In SQL 2005 it errors out "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value"
DECLARE #Table table(date varchar(6),code char(1))
INSERT INTO #Table select '010209','N'
INSERT INTO #Table select '032809','N'
INSERT INTO #Table select space(6),'N'
select * FROM #Table
WHERE
(
(
date <> ''
AND
GETDATE() < CAST(LEFT(date, 2) + '/' + SUBSTRING(date, 3, 2) + '/' + RIGHT(date,2) AS SMALLDATETIME)
)
OR code = 'Y')
Yes, leppie could have been a bit clearer, but <> '' does not match your space(6).
This will work (using ltrim/rtrim)
DECLARE #Table table(date varchar(6),code char(1))
INSERT INTO #Table select '010209','N'
INSERT INTO #Table select '032809','N'
INSERT INTO #Table select space(6),'N'
select * FROM #Table
WHERE
(
(
ltrim(rtrim(date)) <> ''
AND
GETDATE() < CAST(LEFT(date, 2) + '/' + SUBSTRING(date, 3, 2) + '/' + RIGHT(date,2) AS SMALLDATETIME)
)
OR code = 'Y')
The error is exactly what it says.
No matter how hard you try, ' / / ' will never ever be any date.
Use
date.RTRIM() <> ''
instead of
date <> ''