|
you are welcome
|
|
|
|
|
Hi,
I have one column in one table in sql server like
SUK
-----
abc 111 bbb 1212
pqr 222 ccc 2323
i have to split this in to 4 columns
how to do this,,,please help
DOnt update the table ,only for viewing. in a storedprocedure i want this
Rakesh
|
|
|
|
|
SUK is a good name for that column - as in it SUKs having an awful structure. If you needed those values as discrete columns they should have been discrete to start with!
Anyway, a partial solution for you; if you can guarantee the startindex and length of each part it's not too bad:
;WITH SukTable ([Suk]) AS
(
SELECT 'abc 111 bbb 1212'
UNION SELECT 'pqr 222 ccc 2323'
)
SELECT
SUBSTRING(SUK,1,3) AS Field1,
SUBSTRING(SUK,5,3) AS Field2,
SUBSTRING(SUK,9,3) AS Field3,
SUBSTRING(SUK,12,5) AS Field4
FROM SukTable
Otherwise, assuming the parts are separated by a single space, its possible but becomes a dog very quickly - here's a solution for the first 2 parts, already becoming unweildly:
;WITH SukTable ([Suk]) AS
(
SELECT 'abc 111 bbb 1212'
UNION SELECT 'pqr 222 ccc 2323'
)
SELECT
LEFT(SUK,CHARINDEX(' ',Suk,0)-1) AS Field1,
SUBSTRING(SUK,CHARINDEX(' ',Suk,0),CHARINDEX(' ',Suk,CHARINDEX(' ',Suk,0))-CHARINDEX(' ',Suk,0)) AS Field2
FROM SukTable
Edit: Another option is something like this: http://stackoverflow.com/questions/697519/split-function-equivalent-in-tsql[^]
Fix your data model, before it's too late.
|
|
|
|
|
IT IS AUTOMATICALLY EXPANDING TABLE,VALUES MAY CHANGE,ONLY COMMON THING IS ONLY THE SPACE BETWEEN THEM.SO ON THAT WAY .......
|
|
|
|
|
1) DONT SHOUT!!!!
2) I have no idea what you just told me
|
|
|
|
|
Do it after you fetch the data into your code.
|
|
|
|
|
Here i am giving a scalar function to active this..,
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[SplitGETPosFunc](@String varchar(8000),
@Delimiter char(1),
@POSITION INT)
RETURNS varchar(1000)
WITH EXECUTE AS CALLER
AS
begin
declare @idx int;
DECLARE @CHARIDX INT;
DECLARE @INPUTSTR VARCHAR(8000);
declare @OUTPUT varchar(1000);
SET @INPUTSTR=@String;
SET @CHARIDX =0;
SET @idx=0;
if (substring(@String,1,1)='S')
begin
while @idx < @POSITION
begin
SET @CHARIDX= CHARINDEX(@Delimiter, @INPUTSTR);
SET @OUTPUT=SUBSTRING(@INPUTSTR, 1, @CHARIDX - 1)
SET @INPUTSTR = SUBSTRING(@INPUTSTR,@CHARIDX +1,LEN(@INPUTSTR));
SET @idx=@idx+1;
end
end
else
set @OUTPUT=@String;
return @OUTPUT;
end
@String = original string
@Delimiter = delemeter (in your case space)
@POSITION INT = position of the word(starts from 1)
function will return the word in the specified location
Thanks & Regards
Rajesh B
Rajesh B --> A Poor Workman Blames His Tools <--
|
|
|
|
|
while executing ssis package to execute an access macro and end up by getting error "The script threw an exception: Retrieving the COM class factory for component with CLSID {73A4C9C1-D68D-11D0-98BF-00A0C90DC8D9} failed due to the following error: 80080005."
i googled a lot and all i have found is to edit the access application properties in componant services.
any ideas?
|
|
|
|
|
This looks like a Permission exception. Try to give your aspnet user permission and the "Administrator" user of the machine you are running the package.
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
I have a recursive problem.
I have a query that select some columns but one of these colums should be a value that is recursive.
I have a documentname and based on this name I can retrieve the the folderstructure, but this folderstructure is recursive. How can i create 1 query that returns the documentName and the full folderstructure (which is found if the foldername is '').
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
you need to expand your question with code sample and data sample.
as my first questions would be is the data heirachical? or is it based on columns through out the row?
As barmey as a sack of badgers
Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
|
|
|
|
|
Do you mean ,with a given root node to get a whole tree ?
|
|
|
|
|
No its an endnode that should retrieve the way back to rootnode
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
To give more details.
1 table called documents holds documentnames and have a link to
1 table called documentfolders and this table has a link to documentid and folderid
1 table called folders and holders foldernames, folderid and parentfolderid.
structure to retrieve is
folder1/folder2/folder3/foldern for the complete path in which the document can be found.
If I only ask documentname and foldername it would be simple. but once I have to foldername I should retrieve the previous level of foldername recusrively until no foldername is found. Basically it knows 'foldern' but should find the way back to root ('folder1' in example)
So my query should do something like
SELECT DocumentName, GetVirtualPath(documentID, FOLDERID) AS virtualPath
FROM Documents as d
Join Documentfolders as df on df.documentID = d.documentID
Join Folders as f on f.folderID = df.folderID
GetVirtualPath(documentID, FOLDERID) should be the recursive call.
In Word you can only store 2 bytes. That is why I use Writer.
modified on Thursday, October 14, 2010 8:49 AM
|
|
|
|
|
You could try writing a function that calls itself and returns the parent folder name for a given parent folder id. Eventually you should end up at root, where the parent folder id should be null and that would end the recursive call.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
|
|
|
|
|
Since this system is build and delivered by an external party I am not allowed to build a function in this database (warranty story....)
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
if your using SQL 2005 or later. Have a look into recursive CTE (common table expressions)
As barmey as a sack of badgers
Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
|
|
|
|
|
It is an Interbase database but hat can do CTE as well.
But the select results is multiple rows and not 1 varchar with the result like desired.
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
Since you cannot make changes to the database and you don't want the information in it's natural state, rows from the CTE then you need to do the manipulation in your client app. Take the results of the CTE and massage it to meet your needs.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
 It was a bit of a headache but my colleague came up with materialized path design pattern:
WITH
ParentChildRels
(ParentID, ChildID, KeyField, [Path], HierarchyLevel, Doc)
AS
(
SELECT
ParentID, ChildID, KeyField, CAST('root/' + Folder AS nvarchar(255)) AS Path, 1 AS HierarchyLevel, Doc
FROM
(
SELECT
dbo.Folder.ParentID, dbo.Folder.ID AS ChildID, dbo.Folder.Folder, dbo.[Document].DocumentID AS KeyField, dbo.[Document].[Document] AS Doc
FROM
dbo.[Document]
RIGHT OUTER JOIN
dbo.Folder
ON
dbo.[Document].FolderID = dbo.Folder.ID
) AS x
WHERE
(ParentID = 0)
UNION ALL
SELECT
r.ParentID, r.ChildID, r.KeyField, CAST(RTRIM(pr.Path) + '/' + r.Folder AS nvarchar(255)) AS Path, pr.HierarchyLevel + 1 AS HierarchyLevel, r.Doc
FROM
(
SELECT
Folder_1.ParentID, Folder_1.ID AS ChildID, Folder_1.Folder, Document_1.DocumentID AS KeyField, Document_1.[Document] AS Doc
FROM
dbo.[Document] AS Document_1
INNER JOIN
dbo.[Folder] AS Folder_1
ON
Document_1.FolderID = Folder_1.ID
) AS r
INNER JOIN
ParentChildRels AS pr
ON r.ParentID = pr.ChildID
)
SELECT DISTINCT TOP (100) PERCENT
ParentID, ChildID, KeyField, RTRIM(RTRIM([Path]) + '/' + Doc)
FROM
ParentChildRels AS ParentChildRels_1
WHERE Keyfield is not null
ORDER BY
RTRIM(RTRIM([Path]) + '/' + Doc), ParentID, ChildID, KeyField
I thought to share it with you all if you ever come into the same situation
In Word you can only store 2 bytes. That is why I use Writer.
modified on Monday, October 18, 2010 4:59 AM
|
|
|
|
|
mates,
need help. i'm stuck on sql query.
SELECT * FROM TABLE1
WHERE MYFLG =0
AND MYFLG =(SELECT CASE WHEN COLUMN1 = 1 AND COLUMN2 = 0 THEN 1 ELSE 0 END AS MYFLG FROM TABLE1)
got an error: Invalid column name 'MYFLG'.
I know the error because it is an alias name in case statement which is not totally exist in table1 but how will i correct this one.
My aim is only select those MYFLG = 0 in my case statement.
C# コードMicrosoft End User
2000-2008
「「「「「「「「「「「「「「「「「「「「「「「「「「「「
The best things in life are free
」」」」」」」」」」」」」」」」」」」」」」」」」」」」
|
|
|
|
|
I have not tried running my suggestions -
1) Try w/ brackets as below
SELECT * FROM TABLE1
WHERE MYFLG =0
AND MYFLG =(SELECT (CASE WHEN COLUMN1 = 1 AND COLUMN2 = 0 THEN 1 ELSE 0 END) AS MYFLG FROM TABLE1)
2) What are you trying to accomplish w/ this query? Seeing your query, I guess you could simplify it as
SELECT * FROM TABLE1 WHERE (MYFLG = 0 OR (COLUMN1 = 1 AND COLUMN2 = 0))
MYFLG = 0 AND MYFLG = ... doesn't make sense. How could MYFLG could be both 0 and 1? It should be an "or" I guess
I may be wrong, in that case, try to explain your question in detail...
Cheers,
Karthik
|
|
|
|
|
I guess "MYFLG" is not a column of table1.
what result he wants to get is that rows "
not in where COLUMN1 = 1 AND COLUMN2 = 0 "
If so,Why don't use this
Select * from TABLE1 where PK not in ( Select PK From TABLE1 Where COLUMN1 = 1 AND COLUMN2 =0)
modified on Wednesday, October 13, 2010 10:37 PM
|
|
|
|
|
I don't think so, if you see the posters' query, it has
WHERE MYFLG =0
Cheers,
Karthik
|
|
|
|
|
but he alse got this Error infomation "got an error: Invalid column name 'MYFLG'".
|
|
|
|
|