Showing posts with label containing. Show all posts
Showing posts with label containing. Show all posts

Thursday, March 22, 2012

Converting a parent-child table into a genrational table / text file

I need to convert a parent - child table into another table or a text file containing in a generational format.

eg.

child / parent / grand parent / great grand parent / ....

Does anyone have a stored procedure of code to do this?

I'm working with a dimension having 250,000 + members, writing code is fast enough for much smaller hierarchies but with a dimension this size we need something fast.

Thank you

Hi Rod,

Not sure if this is the same problem as you posted in TSQL under "Adjacency List' - as suggested there, recursive CTE would be one approach in SQL Server 2005:

>>

with GenTable(LeafKey, LeafName, GenNum, AncestorKey, AncestorName) as

(select do.OrganizationKey as LeafKey, do.OrganizationName as LeafName,

1 as GenNum, do1.OrganizationKey as AncestorKey,

do1.OrganizationName as AncestorName

from dbo.DimOrganization do

join dbo.DimOrganization do1

on do.ParentOrganizationKey = do1.OrganizationKey

where not exists(select *

from dbo.DimOrganization do2

where do2.ParentOrganizationKey = do.OrganizationKey)

union all

select gt.LeafKey, gt.LeafName,

gt.GenNum + 1 as GenNum, do.ParentOrganizationKey as AncestorKey,

do1.OrganizationName as AncestorName

from GenTable gt

join dbo.DimOrganization do

on gt.AncestorKey = do.OrganizationKey

join dbo.DimOrganization do1

on do.ParentOrganizationKey = do1.OrganizationKey)

select LeafKey, [1] as Gen1Key, [2] as Gen2Key, [3] as Gen3Key

from (select LeafKey, GenNum, AncestorKey

from GenTable) gt

Pivot (Max(AncestorKey)

for GenNum in ([1], [2], [3])) as pt

order by LeafKey

--

3 14 2 1
4 14 2 1
5 14 2 1
6 14 2 1
7 14 2 1
8 2 1 NULL
11 9 1 NULL
12 9 1 NULL
13 10 1 NULL

>>

|||Hi Deepak,

Thank you very much, it's been as long day and I'll look closer in the morning. It looks vey helpful. I should of mentioned that I have to do the with 2000 and 2005 but this likes I'm half way there and on the right track

Thanks again,

Rod

Monday, March 19, 2012

Convert to smalldatetime

I have a varchar field containing dates that I am trying to convert to a
smalldatetime but I keep getting the following error message:
Syntax error converting datetime from character string.
Here is the line of code I am using:
select convert(smalldatetime, DateReported, 101)
from tblName
I checked to make sure there were only valid dates in the field using IsDate
so I am not sure why I can't convert the data to a smalldatetime datatype.
Thanks!any sample data? can you locate the record that causes problem? or all of
them are problematic? maybe the 101 format is wrong?
peter|||It would help if you post a sample of the data, but it appears there is at
least one row that has some extra special character which is the one that
causing the error at the time of the conversion.
You can try executing
select top 1 convert(smalldatetime, DateReported, 101)
from tblName
to verify that not all the values have an issue.
Let me know
"Anonymous" wrote:

> I have a varchar field containing dates that I am trying to convert to a
> smalldatetime but I keep getting the following error message:
> Syntax error converting datetime from character string.
> Here is the line of code I am using:
> select convert(smalldatetime, DateReported, 101)
> from tblName
> I checked to make sure there were only valid dates in the field using IsDa
te
> so I am not sure why I can't convert the data to a smalldatetime datatype.
> Thanks!
>
>|||You guys are right. The data imported with an extra character.
I changed my insert statement to:
Insert tblNew
Select LTrim(RTrim([DateReported]))
From tblOld
However, the extra space is still being inserted into the field. How do I
insert the data with no trailing spaces?
"Edgardo Valdez, MCSD, MCDBA" wrote:
> It would help if you post a sample of the data, but it appears there is at
> least one row that has some extra special character which is the one that
> causing the error at the time of the conversion.
> You can try executing
> select top 1 convert(smalldatetime, DateReported, 101)
> from tblName
> to verify that not all the values have an issue.
> Let me know
> "Anonymous" wrote:
>|||You can use the T-SQL function RTRIM and or LTRIM
Let me know if that helps
"Anonymous" wrote:
> You guys are right. The data imported with an extra character.
> I changed my insert statement to:
> Insert tblNew
> Select LTrim(RTrim([DateReported]))
> From tblOld
> However, the extra space is still being inserted into the field. How do I
> insert the data with no trailing spaces?
> "Edgardo Valdez, MCSD, MCDBA" wrote:
>|||I tried that but it is still inserting the extra space. This is the code I
used to insert the data into the new table:
Insert tblNew
Select LTrim(RTrim([DateReported]))
From tblOld
"Edgardo Valdez, MCSD, MCDBA" wrote:
> You can use the T-SQL function RTRIM and or LTRIM
> Let me know if that helps
> "Anonymous" wrote:
>|||You can try the REPLACE function
Insert tblNew
select replace(DateReported, ' ', '')
From tblOld
"Anonymous" wrote:
> I tried that but it is still inserting the extra space. This is the code
I
> used to insert the data into the new table:
> Insert tblNew
> Select LTrim(RTrim([DateReported]))
> From tblOld
>
> "Edgardo Valdez, MCSD, MCDBA" wrote:
>|||Nope, that doesn't work either. Even tried a LTrim RTrim and replace update
on the data once it was in the table.
Is there a line of code to see what the ascii character is that is being
used. Maybe it isn't a space.
"Edgardo Valdez, MCSD, MCDBA" wrote:
> You can try the REPLACE function
> Insert tblNew
> select replace(DateReported, ' ', '')
> From tblOld
> "Anonymous" wrote:
>|||Well you can use ASCII to inspect a character at a time:
eg
SELECT ASCII('a') -- Returns 97
Write a loop to look at your string. I myself had big problems when
importing some Null Characters ( ascii code 0 )
SELECT ASCII(CHAR(0))
You can't use REPLACE on these and I had to use STUFF! Whoever uses STUFF
Kev!? You could use a low-level text editor to look at these. But why worr
y
about that? Just use SUBSTRING to extract the bit of string you want. ie i
f
you know your funny character is at the start of your string use SUBSTRING (
your_date, 2, 10 ) for example
Let me know how you get on.
Damien
"Anonymous" wrote:
> Nope, that doesn't work either. Even tried a LTrim RTrim and replace upda
te
> on the data once it was in the table.
> Is there a line of code to see what the ascii character is that is being
> used. Maybe it isn't a space.
> "Edgardo Valdez, MCSD, MCDBA" wrote:
>|||Can you show use the definition of tblNew as well as tblOld?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Anonymous" <Anonymous@.discussions.microsoft.com> wrote in message
news:2157F90B-2B55-4AB6-AD14-0F64D506F792@.microsoft.com...
>I tried that but it is still inserting the extra space. This is the code I
> used to insert the data into the new table:
> Insert tblNew
> Select LTrim(RTrim([DateReported]))
> From tblOld
>
> "Edgardo Valdez, MCSD, MCDBA" wrote:
>

Tuesday, February 14, 2012

Convert from unicodestring to uniqueidentifier

Hello,
I have a XML data source containing strings representing GUIDs (ie <modelAsset id="e04ba23c-0401-43a9-b40c-b4c3d580caa9" />) and I need to fill a SQL table with uniqueidentifier column.

I added Data Conversion component setting the data type as DT_GUID.

Running the package I get the following error message:

Error: 0xC02020C5 at Import MyALM settings, Data Conversion [1301]: Data conversion failed while converting column "id" (4645) to column "GUID" (5912). The conversion returned status value 2 and status text "The value could not be converted because of a potential loss of data.".

Error: 0xC0209029 at Import MyALM settings, Data Conversion [1301]: The "output column "GUID" (5912)" failed because error code 0xC020907F occurred, and the error row disposition on "output column "GUID" (5912)" specifies failure on error. An error occurred on the specified object of the specified component.

How can I solve the problem ?

Thanks,
PierreWhile the error message could use some improvement the problem is that your strings that represent guids are not exactly correct. A string representation of a GUID has opening and closing curly braces ({}) and yours do not. If you wrap the column with curly braces (you could use a derived column for this) then your conversion should succeed.

HTH,
Matt|||

Thanks - I had the same problem.

It would be useful if the documentation for uniqueidentifer was updated to reflect the need for { and }.

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/b026035b-f3d2-4d70-989d-3884b4ca0233.htm

Currently it states:

"converting from a string constant in the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, in which each x is a hexadecimal digit in the range 0-9 or a-f. For example, 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid uniqueidentifier value."

|||

So did you use the Send Feedback link then?

-Jamie

Convert from unicodestring to uniqueidentifier

Hello,
I have a XML data source containing strings representing GUIDs (ie <modelAsset id="e04ba23c-0401-43a9-b40c-b4c3d580caa9" />) and I need to fill a SQL table with uniqueidentifier column.

I added Data Conversion component setting the data type as DT_GUID.

Running the package I get the following error message:

Error: 0xC02020C5 at Import MyALM settings, Data Conversion [1301]: Data conversion failed while converting column "id" (4645) to column "GUID" (5912). The conversion returned status value 2 and status text "The value could not be converted because of a potential loss of data.".

Error: 0xC0209029 at Import MyALM settings, Data Conversion [1301]: The "output column "GUID" (5912)" failed because error code 0xC020907F occurred, and the error row disposition on "output column "GUID" (5912)" specifies failure on error. An error occurred on the specified object of the specified component.

How can I solve the problem ?

Thanks,
PierreWhile the error message could use some improvement the problem is that your strings that represent guids are not exactly correct. A string representation of a GUID has opening and closing curly braces ({}) and yours do not. If you wrap the column with curly braces (you could use a derived column for this) then your conversion should succeed.

HTH,
Matt|||

Thanks - I had the same problem.

It would be useful if the documentation for uniqueidentifer was updated to reflect the need for { and }.

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/b026035b-f3d2-4d70-989d-3884b4ca0233.htm

Currently it states:

"converting from a string constant in the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, in which each x is a hexadecimal digit in the range 0-9 or a-f. For example, 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid uniqueidentifier value."

|||

So did you use the Send Feedback link then?

-Jamie