Showing posts with label columns. Show all posts
Showing posts with label columns. Show all posts

Friday, March 30, 2012

How to remove unused columns and eliminate warnings

I have hundreds of these I think mainly due to the way the union all task works. Any ideas on ways to eliminate these?Select the connections in the editor and hit the delete button.
K|||

I guess I wasn't clear. Those columns are used earlier on in the data flow so I can't delete them from the connection. However I get incessant warnings about them hanging around later in the data flow.

|||Chris,

could you give us more details? How did those columns get generated?

That might give us a better idea if you can get rid of them.

Thanks,|||Just an idea... As far as I understand SSIS columns are "recreated" behind an asynchronous transform...

I.e. when you have a source with 2 fields (A and B), a derieved column task which calulates C by A+B, a sort by C passing A and B through it and a destination where you write A and C in a file you should get a warning that B is not used and can be removed... That's because B is passed through an asynchronous transform so it's a new column. And in fact, it's not used behind the sort...

At least that's how I understand SSIS...sql

How to remove rows where only part of the row is duplicated

Hi,

I've got a db table containing 5 columns(excluding id) consisting of
1.) First Half of a UK postcode
2.) Town name to which postcode belongs
3.) Latitude of Postcode
4.) Longitude of Postcode
5.) Second Part of the Postcode

I want to select columns 1,2,3 and 4, but once only. There are often
several entries where 1 and 2 are the same but 3 and 4 are different
i.e.
WA1Bewsey and Whitecross53.386492-2.596847
WA1Bewsey and Whitecross53.388203-2.590961
WA1Bewsey and Whitecross53.388875-2.598504
WA1Fairfield and Howley53.388455-2.581701
WA1Fairfield and Howley53.396117-2.571789

My current query is
SELECT DISTINCT Postcode, Town, latitude, longitude
FROM Postcode
WHERE Postcode.Postcode = 'wa1'
ORDER BY Postcode, Town

However as latitude and longitude differ on each line DISTINCT does
not do what I'm looking for.
Can anybody suggest a way changing the query to just give the first
instance of each Postcode/Town combo?
I.E.
WA1Bewsey and Whitecross53.386492-2.596847
WA1Fairfield and Howley53.388455-2.581701

Many thanks!
DrewThere isn't really any 'first' instance unless you
define your own ordering. However, assuming you have a unique ID
column
this should work

SELECT a.Postcode, a.Town, a.latitude, a.longitude
FROM Postcode a
WHERE NOT EXISTS (SELECT * FROM Postcode b
WHERE b.Postcode=a.Postcode
AND b.Town=a.Town
AND b.ID>a.ID)
ORDER BY a.Postcode, a.Town|||(andylole@.gmail.com) writes:

Quote:

Originally Posted by

I've got a db table containing 5 columns(excluding id) consisting of
1.) First Half of a UK postcode
2.) Town name to which postcode belongs
3.) Latitude of Postcode
4.) Longitude of Postcode
5.) Second Part of the Postcode
>
I want to select columns 1,2,3 and 4, but once only. There are often
several entries where 1 and 2 are the same but 3 and 4 are different
i.e.
WA1 Bewsey and Whitecross 53.386492 -2.596847
WA1 Bewsey and Whitecross 53.388203 -2.590961
WA1 Bewsey and Whitecross 53.388875 -2.598504
WA1 Fairfield and Howley 53.388455 -2.581701
WA1 Fairfield and Howley 53.396117 -2.571789
>
My current query is
SELECT DISTINCT Postcode, Town, latitude, longitude
FROM Postcode
WHERE Postcode.Postcode = 'wa1'
ORDER BY Postcode, Town
>
However as latitude and longitude differ on each line DISTINCT does
not do what I'm looking for.
Can anybody suggest a way changing the query to just give the first
instance of each Postcode/Town combo?


A simple way out would be:

SELECT Postcode, Town, AVG(latitude), AVG(longitude)
FROM Postcode
GROUP BY Postcode, Town

Of course, this yield data that is not in the table at all, but it's a
reasonable assumption that the different lat/long values are in the same
proximity. And if they are not, you have a much bigger problem anyway.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland,

Your solution is perfect!
Many thanks to all for helping.
Cheers,
Andy.

Wednesday, March 28, 2012

How to remove Hyperlink from column while exporting to excel

Hi,

I have one report with few of columns as hyperlinks leading to subreports. When report exported to Excel, columns which are hyperlinks exported and these hyperlinks still present in Excel and is not causing some problem as relative links are used in hyperlinks in main report. So, is there any way to remove hyperlinks from report while exporting to Excel?

I found a sort of work around for this.

Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"

Dim sw As StringWriter = New StringWriter()
Dim htmlTextWriter As HtmlTextWriter = New HtmlTextWriter(sw)

ph.RenderControl(htmlTextWriter)
Dim sTemp As String = sw.ToString

sTemp = sTemp.Replace("href=", "")
Response.Write(sTemp)
Response.End()

Basically whatever you are giving the response.write to output, modify the string by taking out "href=". Even though there is still part of the <a> tag still there, excel seems to ignore it and just display the text.

Hope this works for you!!

And if it does work for, please mark this post as answer!!

Rob

sql

Friday, March 23, 2012

How to reference a previous field Alias in TSQL as in Jet SQL?

My question is simple, I'd like to do something I do in Jet ANSI-89 SQL. Mind you I'm just adding numbers here - they are not actual columns in 'SomeTable'

SELECT 1 AS A, 2 AS B, A+B AS C
FROM SomeTable

The Jet engine evaluates and does arithmetic on the Aliased column names - handy when they contain their own functions. The resultset would show:

A B C
1 2 3

However from what I can tell SQL Server 2005 is not picking this up. Is their an equivalent?

The Jet syntax is non-standard SQL syntax so it will not work in SQL Server. You will have to use a derived table or CTE (in SQL Server 2005) or use a view or computed column in the table (if the expression involves columns from a single table).

select A, B, A+B as C

from (select 1 as A, 2 as B

from sometable

) as t

with t as (

select 1 as A, 2 as B

from sometable

)

select A, B, A+B as C

from t

How to refer to subselect columns in select statement

Hi everybody,

I have been looking around for an answer to this one, but haven't had any luck so far. Hope some of you clever minds out there can help me out on this :-)

I have an sql statement that does updates into another table like this:

insert into result_table

select value1 as 'value 1',

(select number1 from some_other_table where (... linked)) as 'my sub1',

(select number2 from some_third_table where (... again linked)) as 'my sub2'

select 0 as 'zero'

from my_table where ("some clause")

Now, I want to replace the 'zero' column with something useful like the sum of subselect 1 and 2. How can I do that? The only solution I can make work is something like:

insert into result_table

select value1 as 'value 1',

(select number1 from some_other_table where (... linked)) as 'my sub1',

(select number2 from some_third_table where (... again linked)) as 'my sub2',

(

(select number1 from some_other_table where (... linked)) +

(select number2 from some_third_table where (... again linked))

) as 'sub1_plus_sub2'

from my_table where ("some clause")

It works, but in my current project I would end up with tons of duplicate lines. This way I would end up having to maintain select code several places.

Is there a way to refer to a column in the same select statement that you're executing? In the ORDER BY clause you can use things like ORDER BY 1,2 to refer to columns without having to use the entire statement again, but I haven't been able to find anything that seems useful on this subject. I guess what I miss is a way like

field(2).value + field(3).value

Best regards, Jan

In T-SQL you can't do as you guess..

But it is possible to reuse the values. For achive your requirement you have to use the derived table..

Code Snippet

insert into result_table

Select

[Value 1]

,[My Sub1]

,[My Sub2]

,[My Sub1] + [My Sub1] as [Sub1_Pluse_Sub2]

From

(

select

value1 as [Value 1]

,(select number1 from some_other_table where (... linked)) as [My Sub1]

,(select number2 from some_third_table where (... again linked)) as [My Sub2]

from

my_table

where

("some clause")

) as Data

|||

try this instead


INSERT
INTO result_table
SELECT value1 as 'value 1'
, some_other_table.number1 AS 'my sub1'
, some_third_table.number2 AS 'my sub2',
, some_other_table.number1 + some_third_table.number2 as 'sub1_plus_sub2'
FROM my_table INNER JOIN
some_other_table ON (.. linked) INNER JOIN
some_third_table ON (... again linked)
WHERE ("some clause")


Monday, March 12, 2012

How to reclaim space in columns changed from nvarchar to varchar

Hi,

This is probably an easy question for someone so any help would be
appreciated.

I have changed the columns in a table that where nvarchar to the same
size of type varchar so halve the space needed for them.

I have done this a) becuase this is never going to be an international
application, b) we are running out of space and c) there are 100
million rows.

I have done this with the alter table statement which seems to work but
the space used in the database hasn't altered.

I'm presuming that the way the records are structured within the table
there is just now more space free inbetween each page?

Is there a way or re-shrinking just an individual table and free up
some of the space in there or am i missing the point somewhere?

Thanks in advance,

IanYou can reorg the table by rebuilding the table's clustered index.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"wriggs" <ian.w@.btinternet.com> wrote in message
news:1116844882.254733.254670@.g47g2000cwa.googlegr oups.com...
> Hi,
> This is probably an easy question for someone so any help would be
> appreciated.
> I have changed the columns in a table that where nvarchar to the same
> size of type varchar so halve the space needed for them.
> I have done this a) becuase this is never going to be an international
> application, b) we are running out of space and c) there are 100
> million rows.
> I have done this with the alter table statement which seems to work but
> the space used in the database hasn't altered.
> I'm presuming that the way the records are structured within the table
> there is just now more space free inbetween each page?
> Is there a way or re-shrinking just an individual table and free up
> some of the space in there or am i missing the point somewhere?
> Thanks in advance,
> Ian|||The table doesn't have any clustered index. Any other ideas?

Dan Guzman wrote:
> You can reorg the table by rebuilding the table's clustered index.
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "wriggs" <ian.w@.btinternet.com> wrote in message
> news:1116844882.254733.254670@.g47g2000cwa.googlegr oups.com...
> > Hi,
> > This is probably an easy question for someone so any help would be
> > appreciated.
> > I have changed the columns in a table that where nvarchar to the
same
> > size of type varchar so halve the space needed for them.
> > I have done this a) becuase this is never going to be an
international
> > application, b) we are running out of space and c) there are 100
> > million rows.
> > I have done this with the alter table statement which seems to work
but
> > the space used in the database hasn't altered.
> > I'm presuming that the way the records are structured within the
table
> > there is just now more space free inbetween each page?
> > Is there a way or re-shrinking just an individual table and free up
> > some of the space in there or am i missing the point somewhere?
> > Thanks in advance,
> > Ian|||wriggs (ian.w@.btinternet.com) writes:
> The table doesn't have any clustered index. Any other ideas?

Is that a conscious decision?

Having a clustered index on a table is usually good practice. Not the
least, because you then can run DBCC DBREINDEX to handle fragmentation.

If nothing else, you could create a clustered index on the table, and
then drop it.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thanks for the advice Eland,

Not my decision, I've only just taken over the day to day running of
the box on a contract basis.

I agree totally with what you said, so I may have to try and at least
add a clustered index to the table and try what you said.

Ian,

Sunday, February 19, 2012

How to Query for NULL in a database

In my database, in a few tables there are NULL values in some of the columns. This is okay, but I need to know how to query for nulls. For example I tried the following query but it did not work:

select*from Employeewhere DateOfBirth=NULL

This did not work so I also tried the following:

select*from Employeewhere DateOfBirth='NULL'

Neither of these worked. Can someone help me out?

NULL means unknown. So 2 NULLs are not equal. Read up books online about NULLs. Its an important topic. For your query, you would need to write it as:

SELECT * FROM Employee WHERE DateOfBirthIS NULL

How to query data type of XML?

I want to query XML datas.

I have one table T.this T has two columns,id int , datasXML xml.

the datasXML has some XML datas, there into one data is:

<root>
<item val="true"></item>
<others ... />
</root>

Now,I want to query this table's datasXML column,and root/item value is true.

How to design this SQL string?

SELECT * FROM T WHERE datasXML = "<root>.." -- or other?

Please give me some samples.thanks!

Ray Lynn

In SQL2005, XQuery is great for this. There are several good articles on MSDN, for example:

http://msdn.microsoft.com/library/en-us/dnsql90/html/sql2k5_xqueryintro.asp?frame=true

|||Thank you very much!!!!!!!!!!!!!!

How to query bitmap columns

I need to find out all columns that are identity. How do I query syscolumn to find out this information.

Thanks.

I need to find all the identity columns in my database. How do I query syscolumns to get this information>

Thanks.

|||Try this: select object_name(object_id) + '.' + name as Table_Column from sys.columns where is_identity = 1 You may want to filter further to eliminate system table identity columns. Please don't post the same question more than once, either. Steve Kass Drew University http://www.stevekass.com MJM2@.discussions.microsoft.com wrote: > I need to find out all columns that are identity. How do I query > syscolumn to find out this information. > > Thanks. > >|||Create the below stored procedure in ur SQL server 2005 database

and execute .It will return all identity column information

Code Snippet

CREATE PROC dbo.CheckIdentities
AS
BEGIN
SET NOCOUNT ON

SELECT QUOTENAME(SCHEMA_NAME(t.schema_id)) + '.' + QUOTENAME(t.name) AS TableName,
c.name AS ColumnName,
CASE c.system_type_id
WHEN 127 THEN 'bigint'
WHEN 56 THEN 'int'
WHEN 52 THEN 'smallint'
WHEN 48 THEN 'tinyint'
END AS 'DataType',
IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + '.' + t.name) AS CurrentIdentityValue,
CASE c.system_type_id
WHEN 127 THEN (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + '.' + t.name) * 100.) / 9223372036854775807
WHEN 56 THEN (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + '.' + t.name) * 100.) / 2147483647
WHEN 52 THEN (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + '.' + t.name) * 100.) / 32767
WHEN 48 THEN (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + '.' + t.name) * 100.) / 255
END AS 'PercentageUsed'
FROM sys.columns AS c
INNER JOIN
sys.tables AS t
ON t.[object_id] = c.[object_id]
WHERE c.is_identity = 1
ORDER BY PercentageUsed DESC
END


|||This is what I need. Thanks (and sorry for the double post - am new at this).

|||This works as well. Appreciate the responses. Thanks!