Showing posts with label create. Show all posts
Showing posts with label create. Show all posts

Friday, March 30, 2012

nvarchar & varchar

Hi,

I am new to MS SQL. When I create a column in a table, when shall I
use nvarchar or varchar? Please help.

Thanks,
MikeThe nvarchar data type provides support for Unicode characters. This is
needed if you are building an international system that must store different
languages. However, if you have no need to store Unicode characters then you
are better using varchar. The nvarchar data type occupies twice the space of
varchar as it uses 2 bytes to encode each character.

HTH,

Plamen Ratchev
http://www.SQLStudio.com|||On 26 Feb, 15:13, "Plamen Ratchev" <Pla...@.SQLStudio.comwrote:

Quote:

Originally Posted by

The nvarchar data type provides support for Unicode characters. This is
needed if you are building an international system that must store different
languages. However, if you have no need to store Unicode characters then you
are better using varchar. The nvarchar data type occupies twice the space of
varchar as it uses 2 bytes to encode each character.
>
HTH,
>
Plamen Ratchevhttp://www.SQLStudio.com


varchar will support a lot of characters from different languages
though (depending on the collation codepage) so no need to rush into
doubling your storage if you dont "really" need to|||On Feb 26, 10:26 am, "oliver" <oraus...@.hotmail.comwrote:

Quote:

Originally Posted by

On 26 Feb, 15:13, "Plamen Ratchev" <Pla...@.SQLStudio.comwrote:
>

Quote:

Originally Posted by

The nvarchar data type provides support for Unicode characters. This is
needed if you are building an international system that must store different
languages. However, if you have no need to store Unicode characters then you
are better using varchar. The nvarchar data type occupies twice the space of
varchar as it uses 2 bytes to encode each character.


>

Quote:

Originally Posted by

HTH,


>

Quote:

Originally Posted by

Plamen Ratchevhttp://www.SQLStudio.com


>
varchar will support a lot of characters from different languages
though (depending on the collation codepage) so no need to rush into
doubling your storage if you dont "really" need to


Plamen, Oliver Thanks a lot!

Mike|||On Feb 26, 9:47 am, haid...@.gmail.com wrote:

Quote:

Originally Posted by

Hi,
>
I am new to MS SQL. When I create a column in a table, when shall I
use nvarchar or varchar? Please help.
>
Thanks,
Mike


Mike,

Clearly you need to go back to reading the manual or get a Dummy's
book if you don't know the difference between unicode and ascii
strings.

HTH,

Carl Tegeder
Master MS-SQL Administrator|||Carl Tegeder wrote:

Quote:

Originally Posted by

Quote:

Originally Posted by

>I am new to MS SQL. When I create a column in a table, when shall I
>use nvarchar or varchar? Please help.


Quote:

Originally Posted by

Clearly you need to go back to reading the manual or get a Dummy's
book if you don't know the difference between unicode and ascii
strings.


Not the most tactful of responses, but hey.

To the original poster: Google and Wikipedia are your friends.

Wednesday, March 28, 2012

numbering rows of unordered table?

create table t (colA int, colB char(1), colC int)
insert into t(colB, colC) Values('C', 3)
insert into t(colB, colC) Values('C', 1)
insert into t(colB, colC) Values('C', 4)
insert into t(colB, colC) Values('C', 2)
insert into t(colB, colC) Values('A', 4)
insert into t(colB, colC) Values('A', 1)
insert into t(colB, colC) Values('A', 3)
insert into t(colB, colC) Values('A', 2)
insert into t(colB, colC) Values('B', 2)
insert into t(colB, colC) Values('B', 3)
insert into t(colB, colC) Values('B', 4)
insert into t(colB, colC) Values('B', 1)
so colA of table t contains all nulls right now.
Select * from t
NULL C 3
NULL C 1
NULL C 4
NULL C 2
NULL A 4
NULL A 1
NULL A 3
NULL A 2
NULL B 2
NULL B 3
NULL B 4
NULL B 1
I need to number each row of table t so it looks like this
Select * from t Order By colB, colC
1 A 1
2 A 2
3 A 3
4 A 4
5 B 1
6 B 2
7 B 3
8 B 4
9 C 1
10 C 2
11 C 3
12 C 4
In my actual app Table t already exists with colA = null
and colB and colC as above and thousands of rows. Thus,
to say
Update t set colA = 1 Where colB = 'A' And colC = '1'
Update t set colA = 2 Where colB = 'A' And colC = '2'
Update t set colA = 3 Where colB = 'A' And colC = '3'
...
Update t set colA = 9 Where colB = 'C' And colC = '1'
Update t set colA = 10 Where colB = 'C' And colC = '2'
...
is clearly is not the way to go. I humbly request if
someone could show me how to number the rows with T-sql
the correct way. My problem is that I don't know how to
increment the seed number and how to apply it to the
desired order. I am thinking a while loop, but what flag
to use to stop the loop? How to order the rows?
Thanks,
RonTry,
select
count(*) as colA,
a.colB,
a.colC
from
t as a
inner join
t as b
on a.colB + ltrim(a.colC) >= b.colB + ltrim(b.colC)
group by
a.colB,
a.colC
order by
1
go
How to dynamically number rows in a SELECT Statement
http://support.microsoft.com/defaul...kb;en-us;186133
AMB
"Ron" wrote:

> create table t (colA int, colB char(1), colC int)
> insert into t(colB, colC) Values('C', 3)
> insert into t(colB, colC) Values('C', 1)
> insert into t(colB, colC) Values('C', 4)
> insert into t(colB, colC) Values('C', 2)
> insert into t(colB, colC) Values('A', 4)
> insert into t(colB, colC) Values('A', 1)
> insert into t(colB, colC) Values('A', 3)
> insert into t(colB, colC) Values('A', 2)
> insert into t(colB, colC) Values('B', 2)
> insert into t(colB, colC) Values('B', 3)
> insert into t(colB, colC) Values('B', 4)
> insert into t(colB, colC) Values('B', 1)
> so colA of table t contains all nulls right now.
> Select * from t
> NULL C 3
> NULL C 1
> NULL C 4
> NULL C 2
> NULL A 4
> NULL A 1
> NULL A 3
> NULL A 2
> NULL B 2
> NULL B 3
> NULL B 4
> NULL B 1
> I need to number each row of table t so it looks like this
> Select * from t Order By colB, colC
> 1 A 1
> 2 A 2
> 3 A 3
> 4 A 4
> 5 B 1
> 6 B 2
> 7 B 3
> 8 B 4
> 9 C 1
> 10 C 2
> 11 C 3
> 12 C 4
> In my actual app Table t already exists with colA = null
> and colB and colC as above and thousands of rows. Thus,
> to say
> Update t set colA = 1 Where colB = 'A' And colC = '1'
> Update t set colA = 2 Where colB = 'A' And colC = '2'
> Update t set colA = 3 Where colB = 'A' And colC = '3'
> ...
> Update t set colA = 9 Where colB = 'C' And colC = '1'
> Update t set colA = 10 Where colB = 'C' And colC = '2'
> ...
> is clearly is not the way to go. I humbly request if
> someone could show me how to number the rows with T-sql
> the correct way. My problem is that I don't know how to
> increment the seed number and how to apply it to the
> desired order. I am thinking a while loop, but what flag
> to use to stop the loop? How to order the rows?
> Thanks,
> Ron
>|||here is the update.
update
t
set
colA = (select count(*) from t as a where t.colB + ltrim(t.colC) >= a.colB
+ ltrim(a.colC))
go
AMB
"Alejandro Mesa" wrote:
> Try,
> select
> count(*) as colA,
> a.colB,
> a.colC
> from
> t as a
> inner join
> t as b
> on a.colB + ltrim(a.colC) >= b.colB + ltrim(b.colC)
> group by
> a.colB,
> a.colC
> order by
> 1
> go
> How to dynamically number rows in a SELECT Statement
> http://support.microsoft.com/defaul...kb;en-us;186133
>
> AMB
>
> "Ron" wrote:
>|||Thanks very much for your reply. I guess the trick was in
the self join. I took this one step further and performed
an update (as I need to hardcode these numbers):
update t set t.colA = t2.colA
From t Join
(select count(*) as colA, a.colB, a.colC from t as a inner
join t as b on a.colB + ltrim(a.colC) >= b.colB + ltrim
(b.colC) group by a.colB, a.colC) t2
on t.colB = t2.colB and t.colC = t2.colC
Question: someone advised me that using joins in an
update statement is not correct. But this Update
statement accomplished what I needed. Any comments
appreciated.
Thanks again for your help.
Ron

>--Original Message--
>Try,
>select
> count(*) as colA,
> a.colB,
> a.colC
>from
> t as a
> inner join
> t as b
> on a.colB + ltrim(a.colC) >= b.colB + ltrim(b.colC)
>group by
> a.colB,
> a.colC
>order by
> 1
>go
>How to dynamically number rows in a SELECT Statement
>http://support.microsoft.com/default.aspx?scid=kb;en-
us;186133
>
>AMB|||Read my last post.
AMB
"Ron" wrote:

> Thanks very much for your reply. I guess the trick was in
> the self join. I took this one step further and performed
> an update (as I need to hardcode these numbers):
> update t set t.colA = t2.colA
> From t Join
> (select count(*) as colA, a.colB, a.colC from t as a inner
> join t as b on a.colB + ltrim(a.colC) >= b.colB + ltrim
> (b.colC) group by a.colB, a.colC) t2
> on t.colB = t2.colB and t.colC = t2.colC
> Question: someone advised me that using joins in an
> update statement is not correct. But this Update
> statement accomplished what I needed. Any comments
> appreciated.
> Thanks again for your help.
> Ron
>
> us;186133
>|||Thanks again for this correction.

>--Original Message--
>here is the update.
>update
> t
>set
> colA = (select count(*) from t as a where t.colB +
ltrim(t.colC) >= a.colB
>+ ltrim(a.colC))
>go
>
>AMB
>"Alejandro Mesa" wrote:
>
us;186133
this
null
Thus,
sql
to
flag
>.
>

Numbering groups of rows?

create table t (colA int, colB char(1), colC int)
insert into t(colB) Values('C')
insert into t(colB) Values('C')
insert into t(colB) Values('C')
insert into t(colB) Values('C')
insert into t(colB) Values('A')
insert into t(colB) Values('A')
insert into t(colB) Values('A')
insert into t(colB) Values('A')
insert into t(colB) Values('B')
insert into t(colB) Values('B')
insert into t(colB) Values('B')
insert into t(colB) Values('B')
update t set colc =
(select count(*) from t as a where t.colb >= a.colb)
yields
NULL C 12
NULL C 12
NULL C 12
NULL C 12
NULL A 4
NULL A 4
NULL A 4
NULL A 4
NULL B 8
NULL B 8
NULL B 8
NULL B 8
how can I make it yield
NULL C 3
NULL C 3
NULL C 3
NULL C 3
NULL A 1
NULL A 1
NULL A 1
NULL A 1
NULL B 2
NULL B 2
NULL B 2
NULL B 2
Thanks,
Ronupdate t set colc =
(select count(DISTINCT a.colb) from t as a where t.colb >= a.colb)
Jacco Schalkwijk
SQL Server MVP
"Ron" <anonymous@.discussions.microsoft.com> wrote in message
news:01b401c50fb9$5a806ee0$a501280a@.phx.gbl...
> create table t (colA int, colB char(1), colC int)
> insert into t(colB) Values('C')
> insert into t(colB) Values('C')
> insert into t(colB) Values('C')
> insert into t(colB) Values('C')
> insert into t(colB) Values('A')
> insert into t(colB) Values('A')
> insert into t(colB) Values('A')
> insert into t(colB) Values('A')
> insert into t(colB) Values('B')
> insert into t(colB) Values('B')
> insert into t(colB) Values('B')
> insert into t(colB) Values('B')
> update t set colc =
> (select count(*) from t as a where t.colb >= a.colb)
> yields
> NULL C 12
> NULL C 12
> NULL C 12
> NULL C 12
> NULL A 4
> NULL A 4
> NULL A 4
> NULL A 4
> NULL B 8
> NULL B 8
> NULL B 8
> NULL B 8
> how can I make it yield
> NULL C 3
> NULL C 3
> NULL C 3
> NULL C 3
> NULL A 1
> NULL A 1
> NULL A 1
> NULL A 1
> NULL B 2
> NULL B 2
> NULL B 2
> NULL B 2
> Thanks,
> Ron
>|||On Thu, 10 Feb 2005 13:42:01 -0800, Ron wrote:

>update t set colc =
>(select count(*) from t as a where t.colb >= a.colb)
>yields
(snip)
>how can I make it yield
(snip)
Hi Ron,
Better not to store this information at all - you'll find yourself
constantly fighting to keep the rankingf column current after each
modification to the underlying data. It's better to drop the column from
the table and create a view to calculate it.
If you MUST do it in an update, try
update t set colc =
(select count(distinct a.colb) from t as a where t.colb >= a.colb)
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Thanks very much. That worked well. But I also tried
using this in a select statement as follows for ranking
the values A, B, C
select a.colB, count(distinct a.colb) as colC
from t as a inner join t as b
on a.colB >= b.colB
group by a.colB
yielded this ranking
A 1
B 1
C 1
without the Distinct keyword I get this ranking
A 16
B 32
C 48
But I would like to get a ranking as follows
A 1
B 2
C 3
I ask this because I am trying to understand the sql logic
to achieve these results. Hopefully, after I do enough of
these kinds of queries I will get the idea how they work.
May I ask how I could achieve the ranking from result3?
Thanks again,
Ron

>--Original Message--
> update t set colc =
>(select count(DISTINCT a.colb) from t as a where t.colb
>= a.colb)
>
>--
>Jacco Schalkwijk
>SQL Server MVP
>
>"Ron" <anonymous@.discussions.microsoft.com> wrote in
message
>news:01b401c50fb9$5a806ee0$a501280a@.phx.gbl...
>
>.
>|||Ron,
Try count(distinct b.colb) instead of count(distinct a.colb). I suspect
that's what you had in mind.
Steve Kass
Drew University
Ron wrote:
>Thanks very much. That worked well. But I also tried
>using this in a select statement as follows for ranking
>the values A, B, C
>select a.colB, count(distinct a.colb) as colC
>from t as a inner join t as b
>on a.colB >= b.colB
>group by a.colB
>yielded this ranking
>A 1
>B 1
>C 1
>without the Distinct keyword I get this ranking
>A 16
>B 32
>C 48
>But I would like to get a ranking as follows
>A 1
>B 2
>C 3
>I ask this because I am trying to understand the sql logic
>to achieve these results. Hopefully, after I do enough of
>these kinds of queries I will get the idea how they work.
>May I ask how I could achieve the ranking from result3?
>Thanks again,
>Ron
>
>
>message
>|||On Thu, 10 Feb 2005 14:21:58 -0800, Ron wrote:

>Thanks very much. That worked well. But I also tried
>using this in a select statement as follows for ranking
>the values A, B, C
>select a.colB, count(distinct a.colb) as colC
>from t as a inner join t as b
>on a.colB >= b.colB
>group by a.colB
Hi Ron,
Try this one instead:
select a.colB, count(distinct b.colb) as colC
from t as a inner join t as b
on a.colB >= b.colB
group by a.colB
(Note: only one letter weas changed!!)
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Thanks very much. I am also trying to figure out how to
rank A, B, C
select a.colB, count(distinct a.colb) as colC
from t as a inner join t as b
on a.colB >= b.colB
group by a.colB
yielded this ranking
A 1
B 1
C 1
without the Distinct keyword I get this ranking
A 16
B 32
C 48
But I would like to get a ranking as follows
A 1
B 2
C 3
May I ask how I could achieve the ranking from result3?
This way, as you say, I don't really store the ranks, just
retrieve them dynamically.
Thanks again,
Ron

>--Original Message--
>On Thu, 10 Feb 2005 13:42:01 -0800, Ron wrote:
>
>(snip)
>(snip)
>Hi Ron,
>Better not to store this information at all - you'll find
yourself
>constantly fighting to keep the rankingf column current
after each
>modification to the underlying data. It's better to drop
the column from
>the table and create a view to calculate it.
>If you MUST do it in an update, try
>update t set colc =
>(select count(distinct a.colb) from t as a where t.colb
>= a.colb)
>Best, Hugo
>--
>(Remove _NO_ and _SPAM_ to get my e-mail address)
>.
>|||Thanks all. I am sort of starting to get the idea. But I
wonder if I could belabor this thing one more notch:
Instead of using distinct is it possible to plant a group
by query in there? Pseudocode here:
select a.colB, count(select a.colb from a group by a.colb)
as colC from t as a inner join t as b
on a.colB >= b.colB group by a.colB
Again, I just ask because I don't really know all the
rules for t-sql, let alone the tricks. I am guessing that
t-sql does not allow Selects inside of Count(..)
Thanks again,
Ron

>--Original Message--
>Thanks very much. That worked well. But I also tried
>using this in a select statement as follows for ranking
>the values A, B, C
>select a.colB, count(distinct a.colb) as colC
>from t as a inner join t as b
>on a.colB >= b.colB
>group by a.colB
>yielded this ranking
>A 1
>B 1
>C 1
>without the Distinct keyword I get this ranking
>A 16
>B 32
>C 48
>But I would like to get a ranking as follows
>A 1
>B 2
>C 3
>I ask this because I am trying to understand the sql
logic
>to achieve these results. Hopefully, after I do enough
of
>these kinds of queries I will get the idea how they
work.
>May I ask how I could achieve the ranking from result3?
>Thanks again,
>Ron
>
>message
>.
>|||On Thu, 10 Feb 2005 14:45:11 -0800, Ron wrote:

>Thanks all. I am sort of starting to get the idea. But I
>wonder if I could belabor this thing one more notch:
>Instead of using distinct is it possible to plant a group
>by query in there? Pseudocode here:
>select a.colB, count(select a.colb from a group by a.colb)
>as colC from t as a inner join t as b
>on a.colB >= b.colB group by a.colB
Hi Ron,
This code won't work. I'm sure there is some way to do this with a group
by in the subquery, but it's not trivial and it'll be more complex than
the version with DISTINCT that I suggested.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||>> Instead of using distinct is it possible to plant a group by query
in there? <<
This will use a GROUP BY and get you a bit more information in the VIEW
aggregate functions. I am not sure that there is any advantage. .
CREATE TABLE Foobar (letter CHAR(1) NOT NULL);
INSERT INTO Foobar (letter) VALUES('C');
INSERT INTO Foobar (letter) VALUES('C');
INSERT INTO Foobar (letter) VALUES('C');
INSERT INTO Foobar (letter) VALUES('C');
INSERT INTO Foobar (letter) VALUES('A');
INSERT INTO Foobar (letter) VALUES('A');
INSERT INTO Foobar (letter) VALUES('A');
INSERT INTO Foobar (letter) VALUES('A');
INSERT INTO Foobar (letter) VALUES('B');
INSERT INTO Foobar (letter) VALUES('B');
INSERT INTO Foobar (letter) VALUES('B');
INSERT INTO Foobar (letter) VALUES('B');
CREATE VIEW FoobarReport (letter, occurs, place)
AS
SELECT F1.letter, COUNT(*),
(SELECT COUNT (DISTINCT F2.letter)
FROM Foobar AS F2
WHERE F2.letter <= F1.letter)
FROM Foobar AS F1
GROUP BY F1.letter;

Monday, March 26, 2012

Number to Date.

create table testDate(DateNumber int)
go
insert into testDate
Select 20040101
Union
Select 20040201
Union
Select 20040301
Union
Select 20040401
go
Select DateNumber
from testDate
where convert(datetime,convert(varchar,DateNumber)) <
DateAdd(day,-45,getdate())
"Dylan" <anonymous@.discussions.microsoft.com> wrote in message
news:1848e01c41b1e$9d82cb60$a301280a@.phx.gbl...
> Hi,
> The accounts package that we use uses MSSQL for its
> database. One of the tables is for recording sales
> transactions. The due date field in this table is stored
> as INT size 4 in the YYYYMMDD format; i.e 20040405
> (today's date). I have been asked to create a script
> that will report all transactions that are x numbers past
> their due date from a specified date. So they might want
> to find out all transactions that will be 45 days overdue
> (past due date) as of the end of the month, for example.
> My problem is finding a way to convert this due_date
> filed to date format so that the date they enter can be
> deleted from it. Can anyone help?
> To recap:
> Convert 20040405 (INT) to 20040405 (YYYYMMDD - date)
> TIA,
> Dylan
Try following:
declare @.p_custom_date datetime
select *
from (select accnt_code
, convert(char(8),due_date) due_date, amount
from salfldgtel
WHERE ( ( ( ACCNT_CODE LIKE '51%')
AND ( ( ALLOCATION <> 'A' ) ) ) )
AND accnt_code <> '511534'
AND accnt_code <> '512088'
AND accnt_code <> '512165'
AND accnt_code <> '515124'
AND accnt_code <> '512199'
AND accnt_code <> '518020'
AND accnt_code <> '511538'
--if due_Date is integer
AND convert(datetime, cast(due_date as char(8))) between '19000101' and '20050101'
--and trunc(mod(due_date,1E4)*1E-2) between 1 and 12 CAN BE CHANGED TO
and round(((due_date % 10000) * .01),0,1) between 1 and 12
-- and mod(due_date,1E2) between 1 and 31)x CAN BE CHANGED TO
and (due_date % 100 ) between 1 and 31)x
--to_date(&p_custom_date,'DDMMYYYY') - x.due_date > 60
--above clause can be changed as follows, assuming you are looking for difference in two dates
in terms of number of days.
where datediff(dd,@.p_custom_date , x.due_date) > 60
order by accnt_code
dylan,
as you must have realized that,you can not straight away put the query that is working in
oracle into sql server. simply because the functions like trunc/mod and other expressions like
1E4/1E2 can not be accepted by sql server as it is. I've tried giving you closest possible
answer. Try referring to following url which will be more helpful to you.
Migrating Oracle Databases to SQL Server 2000
http://microsoft.com/sql/techinfo/de...rateOracle.asp
Vishal Parkar
vgparkar@.yahoo.co.in
|||Dear Vishal,
Thank you for your help. Not being a whiz at building
queries for DBs it took me a long time to build the
original query for Oracle. Trying to find the
corresponding statements for MS SQL was taking me a very
long time. You have helped me plenty.
Thanks again,
Dylan.
>--Original Message--
>Try following:
>declare @.p_custom_date datetime
>select *
>from (select accnt_code
>, convert(char(8),due_date) due_date, amount
>from salfldgtel
>WHERE ( ( ( ACCNT_CODE LIKE '51%')
>AND ( ( ALLOCATION <> 'A' ) ) ) )
>AND accnt_code <> '511534'
>AND accnt_code <> '512088'
>AND accnt_code <> '512165'
>AND accnt_code <> '515124'
>AND accnt_code <> '512199'
>AND accnt_code <> '518020'
>AND accnt_code <> '511538'
>--if due_Date is integer
>AND convert(datetime, cast(due_date as char(8)))
between '19000101' and '20050101'
>--and trunc(mod(due_date,1E4)*1E-2) between 1 and 12 CAN
BE CHANGED TO
>and round(((due_date % 10000) * .01),0,1) between 1 and
12
>-- and mod(due_date,1E2) between 1 and 31)x CAN BE
CHANGED TO
>and (due_date % 100 ) between 1 and 31)x
>--to_date(&p_custom_date,'DDMMYYYY') - x.due_date > 60
>--above clause can be changed as follows, assuming you
are looking for difference in two dates
>in terms of number of days.
>where datediff(dd,@.p_custom_date , x.due_date) > 60
>order by accnt_code
>
>dylan,
>as you must have realized that,you can not straight away
put the query that is working in
>oracle into sql server. simply because the functions
like trunc/mod and other expressions like
>1E4/1E2 can not be accepted by sql server as it is. I've
tried giving you closest possible
>answer. Try referring to following url which will be
more helpful to you.
>Migrating Oracle Databases to SQL Server 2000
>http://microsoft.com/sql/techinfo/de...t/2000/Migrate
Oracle.asp
>
>--
>Vishal Parkar
>vgparkar@.yahoo.co.in
>
>.
>
|||Hi vishal,
declare @.p_custom_date is supposed to be a prompt for
users to enter a date. Is this how you pull this off in
SQL? When I run it in Query Analyzer it doesn't prompt
me and so it returns no rows. I manually enter a date in
the place of @.p_custom_date for the datediff clause it
seems to work.
Any ideads?
Thanks,
Dylan

>--Original Message--
>Try following:
>declare @.p_custom_date datetime
>select *
>from (select accnt_code
>, convert(char(8),due_date) due_date, amount
>from salfldgtel
>WHERE ( ( ( ACCNT_CODE LIKE '51%')
>AND ( ( ALLOCATION <> 'A' ) ) ) )
>AND accnt_code <> '511534'
>AND accnt_code <> '512088'
>AND accnt_code <> '512165'
>AND accnt_code <> '515124'
>AND accnt_code <> '512199'
>AND accnt_code <> '518020'
>AND accnt_code <> '511538'
>--if due_Date is integer
>AND convert(datetime, cast(due_date as char(8)))
between '19000101' and '20050101'
>--and trunc(mod(due_date,1E4)*1E-2) between 1 and 12 CAN
BE CHANGED TO
>and round(((due_date % 10000) * .01),0,1) between 1 and
12
>-- and mod(due_date,1E2) between 1 and 31)x CAN BE
CHANGED TO
>and (due_date % 100 ) between 1 and 31)x
>--to_date(&p_custom_date,'DDMMYYYY') - x.due_date > 60
>--above clause can be changed as follows, assuming you
are looking for difference in two dates
>in terms of number of days.
>where datediff(dd,@.p_custom_date , x.due_date) > 60
>order by accnt_code
>
>dylan,
>as you must have realized that,you can not straight away
put the query that is working in
>oracle into sql server. simply because the functions
like trunc/mod and other expressions like
>1E4/1E2 can not be accepted by sql server as it is. I've
tried giving you closest possible
>answer. Try referring to following url which will be
more helpful to you.
>Migrating Oracle Databases to SQL Server 2000
>http://microsoft.com/sql/techinfo/de...t/2000/Migrate
Oracle.asp
>
>--
>Vishal Parkar
>vgparkar@.yahoo.co.in
>
>.
>
|||hi dylan,
Set value for this variable using SET .
Ex:
declare @.p_custom_date datetime
set @.p_custom_date = '20040101' --settting date value for variable @.p_custom_date
Vishal Parkar
vgparkar@.yahoo.co.in

Number of User Databases

Hello,

I am being asked to support a third part y app that will create a new database per session with upwards of 500 databases being created per month, now I know that this is not good but my bosses are asking for the technical reasons why this is not good, can someone help please? There are the obvious #of databases to maintain ie backup etc but what else can I add?

Thanks

Julian

I can think of a couple. First is memory consumption. Each open database will have in memory structures that consume virtual address space. See this article for one of the issues:http://support.microsoft.com/kb/316749/en-us

There is also some disk space implications. Each new database will need it's own system tables, allocation pages, Index roots, etc. Say this adds up to even 2mb per database that's 1000 MB/month just for metadata. If everything were in a single database you could safely assume only a few MB total over the life of the database for metadata and allocation pages.

Next is sheer capacity of the server. SQL Server currently supports 32767 databases, at over 500 databases/month that will get you by for about 65 months. Do you really want to go with a solution that will leave you high and dry after a few years.

not to mention the overhead of trying to replicate schema or index changes to thousands of databases.

HTH

-Jerome

Friday, March 9, 2012

nulls

create table t1(c1 int, c2 varchar(10))
insert t1 values(1,'Hello')
insert t1 values(2,'')
insert t1 values(3,NULL)

select *
from t1

c1c2
1Hello
2
3NULL

select *
from t1
where c2 = ' '

c1c2
2

select *
from t1
where ltrim(rtrim(c2)) is null

c1c2
3NULL

The last query should have result as following. However sql server
2000 does no list row c1 = 2.
c1c2
2
3NULLOn 20 Mar, 06:28, othell...@.yahoo.com wrote:

Quote:

Originally Posted by

create table t1(c1 int, c2 varchar(10))
insert t1 values(1,'Hello')
insert t1 values(2,'')
insert t1 values(3,NULL)
>
select *
from t1
>
c1 c2
1 Hello
2
3 NULL
>
select *
from t1
where c2 = ' '
>
c1 c2
2
>
select *
from t1
where ltrim(rtrim(c2)) is null
>
c1 c2
3 NULL
>
The last query should have result as following. However sql server
2000 does no list row c1 = 2.
c1 c2
2
3 NULL


Why would you think that the result of ltrim(rtrim(c2)) would be NULL
when c2 is a non-null string? In fact the result is an empty string
(not the same as NULL) so the answer you got is correct. The row where
c1=2 should NOT be included.

In SQL, NULL is not the same as an empty string. The only common
exception that I know of is Oracle, which treats empty strings as
NULLs.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx
--|||On Mar 20, 3:52 pm, "David Portas"
<REMOVE_BEFORE_REPLYING_dpor...@.acm.orgwrote:

Quote:

Originally Posted by

On 20 Mar, 06:28, othell...@.yahoo.com wrote:
>
>
>
>
>

Quote:

Originally Posted by

create table t1(c1 int, c2 varchar(10))
insert t1 values(1,'Hello')
insert t1 values(2,'')
insert t1 values(3,NULL)


>

Quote:

Originally Posted by

select *
from t1


>

Quote:

Originally Posted by

c1 c2
1 Hello
2
3 NULL


>

Quote:

Originally Posted by

select *
from t1
where c2 = ' '


>

Quote:

Originally Posted by

c1 c2
2


>

Quote:

Originally Posted by

select *
from t1
where ltrim(rtrim(c2)) is null


>

Quote:

Originally Posted by

c1 c2
3 NULL


>

Quote:

Originally Posted by

The last query should have result as following. However sql server
2000 does no list row c1 = 2.
c1 c2
2
3 NULL


>
Why would you think that the result of ltrim(rtrim(c2)) would be NULL
when c2 is a non-null string? In fact the result is an empty string
(not the same as NULL) so the answer you got is correct. The row where
c1=2 should NOT be included.
>
In SQL, NULL is not the same as an empty string. The only common
exception that I know of is Oracle, which treats empty strings as
NULLs.
>
--
David Portas, SQL Server MVP
>
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
>
SQL Server Books Online:http://msdn2.microsoft.com/library/...US,SQL.90).aspx
-- Hide quoted text -
>
- Show quoted text -


If it is not null then it is definitely not 'any number of spaces' and
match.

select *
from t1
where c2 = ' '|||Actually, ltrim(rtrim(c2)) is 'any number of spaces', it's zero
spaces, or empty string, not NULL. NULL is not an empty string, it is
NULL. End of story.

Cheers,
Jason Lepack

On Mar 20, 6:04 am, othell...@.yahoo.com wrote:

Quote:

Originally Posted by

On Mar 20, 3:52 pm, "David Portas"
>
>
>
<REMOVE_BEFORE_REPLYING_dpor...@.acm.orgwrote:

Quote:

Originally Posted by

On 20 Mar, 06:28, othell...@.yahoo.com wrote:


>

Quote:

Originally Posted by

Quote:

Originally Posted by

create table t1(c1 int, c2 varchar(10))
insert t1 values(1,'Hello')
insert t1 values(2,'')
insert t1 values(3,NULL)


>

Quote:

Originally Posted by

Quote:

Originally Posted by

select *
from t1


>

Quote:

Originally Posted by

Quote:

Originally Posted by

c1 c2
1 Hello
2
3 NULL


>

Quote:

Originally Posted by

Quote:

Originally Posted by

select *
from t1
where c2 = ' '


>

Quote:

Originally Posted by

Quote:

Originally Posted by

c1 c2
2


>

Quote:

Originally Posted by

Quote:

Originally Posted by

select *
from t1
where ltrim(rtrim(c2)) is null


>

Quote:

Originally Posted by

Quote:

Originally Posted by

c1 c2
3 NULL


>

Quote:

Originally Posted by

Quote:

Originally Posted by

The last query should have result as following. However sql server
2000 does no list row c1 = 2.
c1 c2
2
3 NULL


>

Quote:

Originally Posted by

Why would you think that the result of ltrim(rtrim(c2)) would be NULL
when c2 is a non-null string? In fact the result is an empty string
(not the same as NULL) so the answer you got is correct. The row where
c1=2 should NOT be included.


>

Quote:

Originally Posted by

In SQL, NULL is not the same as an empty string. The only common
exception that I know of is Oracle, which treats empty strings as
NULLs.


>

Quote:

Originally Posted by

--
David Portas, SQL Server MVP


>

Quote:

Originally Posted by

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.


>

Quote:

Originally Posted by

SQL Server Books Online:http://msdn2.microsoft.com/library/...US,SQL.90).aspx
-- Hide quoted text -


>

Quote:

Originally Posted by

- Show quoted text -


>
If it is not null then it is definitely not 'any number of spaces' and
match.
>
select *
from t1
where c2 = ' '

|||On 20 Mar 2007 03:04:36 -0700, othellomy@.yahoo.com wrote:

(snip)

Quote:

Originally Posted by

>If it is not null then it is definitely not 'any number of spaces' and
>match.
>
>select *
>from t1
>where c2 = ' '


Hi othellomy,

I'm not sure if I understand you correctly, but I assume that you are
asking why a string of zero length ('') is considered equal to a string
of spaces (' ').

The reason is how ANSI has ruled that string comparisons in SQL should
be carried out: the shorter string has to be padded with spaces to match
the length of the longer string; after that, the strings are compared
position by position.

I know that this is not always the behaviour people expect and require.
The expectation can be managed by understanding the rules for string
comparisons. And the required behaviour of string comparisons can be
gotten by using one of the followinmg two workarounds:

DECLARE @.a varchar(10), @.b varchar(10);
SET @.a = 'abc';
SET @.b = 'abc ';

-- Workaround 1
IF @.a = @.b AND DATALENGTH(@.a) = DATALENGTH(@.b)
PRINT 'They are equal!';
ELSE
PRINT 'They are different!';

-- Workaround 2
IF @.a + 'X' = @.b + 'X'
PRINT 'They are equal!';
ELSE
PRINT 'They are different!';

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis|||On Mar 21, 12:12 am, Hugo Kornelis
<h...@.perFact.REMOVETHIS.info.INVALIDwrote:

Quote:

Originally Posted by

On 20 Mar 2007 03:04:36 -0700, othell...@.yahoo.com wrote:
>
(snip)
>

Quote:

Originally Posted by

If it is not null then it is definitely not 'any number of spaces' and
match.


>

Quote:

Originally Posted by

select *
from t1
where c2 = ' '


>
Hi othellomy,
>
I'm not sure if I understand you correctly, but I assume that you are
asking why a string of zero length ('') is considered equal to a string
of spaces (' ').
>
The reason is how ANSI has ruled that string comparisons in SQL should
be carried out: the shorter string has to be padded with spaces to match
the length of the longer string; after that, the strings are compared
position by position.
>
I know that this is not always the behaviour people expect and require.
The expectation can be managed by understanding the rules for string
comparisons. And the required behaviour of string comparisons can be
gotten by using one of the followinmg two workarounds:
>
DECLARE @.a varchar(10), @.b varchar(10);
SET @.a = 'abc';
SET @.b = 'abc ';
>
-- Workaround 1
IF @.a = @.b AND DATALENGTH(@.a) = DATALENGTH(@.b)
PRINT 'They are equal!';
ELSE
PRINT 'They are different!';
>
-- Workaround 2
IF @.a + 'X' = @.b + 'X'
PRINT 'They are equal!';
ELSE
PRINT 'They are different!';
>
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog:http://sqlblog.com/blogs/hugo_kornelis


SET @.a = ''
SET @.b = ' '
if nullif(@.a,'') is null and nullif(@.b,'') is null
PRINT 'They are equal!';
ELSE
PRINT 'They are different!';|||On 21 Mar 2007 00:32:20 -0700, othellomy@.yahoo.com wrote:

(snip)

Quote:

Originally Posted by

>SET @.a = ''
>SET @.b = ' '
>if nullif(@.a,'') is null and nullif(@.b,'') is null
PRINT 'They are equal!';
>ELSE
PRINT 'They are different!';


Hi othellomy,

I'm not sure what you're trying to say here. This code will return "They
are equal!" if both @.a and @.b are either NULL or a string consisting of
zero or more space characters, regardless of whether they are equal:

DECLARE @.a varchar(10), @.b varchar(10);
SET @.a = ' ';
SET @.b = NULL;

if nullif(@.a,'') is null and nullif(@.b,'') is null
PRINT 'They are equal!';

But it will return nothing if @.a and @.b are both non-NULL and not empty,
even if they ARE equal:

DECLARE @.a varchar(10), @.b varchar(10);
SET @.a = 'X';
SET @.b = @.a;

if nullif(@.a,'') is null and nullif(@.b,'') is null
PRINT 'They are equal!';

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Null View Definition?!?

In QA,
SELECT Table_Name, View_Definition
FROM Information_Schema.Views
Table_Name View_Definition
ThisView CREATE VIEW...
ThatView CREATE VIEW...
OtherView NULL <-- HUH?!?
So I flip over to EM and double-click 'OtherView' and I can see the code in
the property page that appears. What does that mean?
Peace & happy computing,
Mike Labosh, MCSD
"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"Mike,
No idea, but try refreshing all views before selecting from
information_schema.views
Example:
use northwind
go
declare @.ts sysname
declare @.tn sysname
declare @.sql nvarchar(4000)
declare views_cursor cursor local fast_forward
for
select
table_schema,
table_name
from
information_schema.tables
where
table_type = 'view'
and objectproperty(object_id(quotename(table
_schema) + N'.' +
quotename(table_name)), 'IsMSShipped') = 0
open views_cursor
while 1 = 1
begin
fetch next from views_cursor into @.ts, @.tn
if @.@.error != 0 or @.@.fetch_status != 0 break
set @.sql = N'exec sp_refreshview ''' + quotename(@.ts) + N'.' +
quotename(@.tn) + ''''
exec sp_executesql @.sql
end
close views_cursor
deallocate views_cursor
go
AMB
"Mike Labosh" wrote:

> In QA,
> SELECT Table_Name, View_Definition
> FROM Information_Schema.Views
> Table_Name View_Definition
> ThisView CREATE VIEW...
> ThatView CREATE VIEW...
> OtherView NULL <-- HUH?!?
> So I flip over to EM and double-click 'OtherView' and I can see the code i
n
> the property page that appears. What does that mean?
> --
> Peace & happy computing,
> Mike Labosh, MCSD
> "Mr. McKittrick, after very careful consideration, I have
> come to the conclusion that this new system SUCKS."
> -- General Barringer, "War Games"
>
>|||> SELECT Table_Name, View_Definition
> FROM Information_Schema.Views
> Table_Name View_Definition
> ThisView CREATE VIEW...
> ThatView CREATE VIEW...
> OtherView NULL <-- HUH?!?
I GOT IT!
Inside Information_Schema.Views, it does this:
-- Displays views accessable to current user
create view INFORMATION_SCHEMA.VIEWS
as
select
db_name() as TABLE_CATALOG
,user_name(obj.uid) as TABLE_SCHEMA
,obj.name as TABLE_NAME
,case
when exists (select *
from syscomments com3
where com3.id = obj.id
and com3.colid > 1) then convert(nvarchar(4000), NULL)
else com.text
end as VIEW_DEFINITION
If the text of the view is greater than 4000 characters, the convert
function returns null.
Rats.
So where does EM get the code from, to display in the View's property page?
Peace & happy computing,
Mike Labosh, MCSD
"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"|||What about quering the tables direct without the information_views...
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Mike Labosh" <mlabosh@.hotmail.com> schrieb im Newsbeitrag
news:etaHXDvZFHA.2788@.TK2MSFTNGP12.phx.gbl...
> I GOT IT!
> Inside Information_Schema.Views, it does this:
> -- Displays views accessable to current user
> create view INFORMATION_SCHEMA.VIEWS
> as
> select
> db_name() as TABLE_CATALOG
> ,user_name(obj.uid) as TABLE_SCHEMA
> ,obj.name as TABLE_NAME
> ,case
> when exists (select *
> from syscomments com3
> where com3.id = obj.id
> and com3.colid > 1) then convert(nvarchar(4000), NULL)
> else com.text
> end as VIEW_DEFINITION
>
> If the text of the view is greater than 4000 characters, the convert
> function returns null.
> Rats.
> So where does EM get the code from, to display in the View's property
> page?
> --
> Peace & happy computing,
> Mike Labosh, MCSD
> "Mr. McKittrick, after very careful consideration, I have
> come to the conclusion that this new system SUCKS."
> -- General Barringer, "War Games"
>

Monday, February 20, 2012

NULL login name as dbo in database

Hi all!
Recently, every database i create - regardless the user -
comes with the dbo user with NULL login name.
My older databases were created correctly, with "sa" as "dbo".
I am running SQL Server 2000 SP3 on PIII 800Mhz 512MB
Tanks in advance!
DiegoYou can change database ownership to the correct owner using
sp_changedbowner:
USE MyDatabase
EXEC sp_changedbowner 'sa'
I'm not sure why new databases would get created with a NULL dbo. What
does sp_helpdb and sp_helpuser report?
--
Hope this helps.
Dan Guzman
SQL Server MVP
--
SQL FAQ links (courtesy Neil Pike):
http://www.ntfaq.com/Articles/Index.cfm?DepartmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
--
"Diego Dias" <diego.dias@.terra.com.br> wrote in message
news:053601c38d11$14ed91e0$a301280a@.phx.gbl...
> Hi all!
> Recently, every database i create - regardless the user -
> comes with the dbo user with NULL login name.
> My older databases were created correctly, with "sa" as "dbo".
> I am running SQL Server 2000 SP3 on PIII 800Mhz 512MB
> Tanks in advance!
> Diego|||This occurs when you use EM and are connected with NT-authentication.
EM then shows you NT-account and when you select the ussers-tab, dbo has no
accout associatted.
(personaly, I always reroute dbo to sa, even if my developers don't like it
because they have to create tables dbo.** and have to code the 'dbo.'-part )
Jobi
"Dan Guzman" <danguzman@.nospam-earthlink.net> wrote in message
news:uRT5rfUjDHA.1284@.TK2MSFTNGP09.phx.gbl...
> You can change database ownership to the correct owner using
> sp_changedbowner:
> USE MyDatabase
> EXEC sp_changedbowner 'sa'
> I'm not sure why new databases would get created with a NULL dbo. What
> does sp_helpdb and sp_helpuser report?
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> --
> SQL FAQ links (courtesy Neil Pike):
> http://www.ntfaq.com/Articles/Index.cfm?DepartmentID=800
> http://www.sqlserverfaq.com
> http://www.mssqlserver.com/faq
> --
> "Diego Dias" <diego.dias@.terra.com.br> wrote in message
> news:053601c38d11$14ed91e0$a301280a@.phx.gbl...
> > Hi all!
> >
> > Recently, every database i create - regardless the user -
> > comes with the dbo user with NULL login name.
> >
> > My older databases were created correctly, with "sa" as "dbo".
> >
> > I am running SQL Server 2000 SP3 on PIII 800Mhz 512MB
> >
> > Tanks in advance!
> >
> > Diego
>|||> This occurs when you use EM and are connected with NT-authentication.
> EM then shows you NT-account and when you select the ussers-tab, dbo
has no
> accout associatted.
This can be corrected with sp_changedbowner. This may occur when a
database is restored or attached but I haven't seen the problem with
CREATE DATABASE.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"jobi" <jobi@.reply2.group> wrote in message
news:bm0b8s$6q4$1@.reader08.wxs.nl...
> This occurs when you use EM and are connected with NT-authentication.
> EM then shows you NT-account and when you select the ussers-tab, dbo
has no
> accout associatted.
> (personaly, I always reroute dbo to sa, even if my developers don't
like it
> because they have to create tables dbo.** and have to code the
'dbo.'-part )
> Jobi
>
> "Dan Guzman" <danguzman@.nospam-earthlink.net> wrote in message
> news:uRT5rfUjDHA.1284@.TK2MSFTNGP09.phx.gbl...
> > You can change database ownership to the correct owner using
> > sp_changedbowner:
> >
> > USE MyDatabase
> > EXEC sp_changedbowner 'sa'
> >
> > I'm not sure why new databases would get created with a NULL dbo.
What
> > does sp_helpdb and sp_helpuser report?
> >
> > --
> > Hope this helps.
> >
> > Dan Guzman
> > SQL Server MVP
> >
> > --
> > SQL FAQ links (courtesy Neil Pike):
> >
> > http://www.ntfaq.com/Articles/Index.cfm?DepartmentID=800
> > http://www.sqlserverfaq.com
> > http://www.mssqlserver.com/faq
> > --
> >
> > "Diego Dias" <diego.dias@.terra.com.br> wrote in message
> > news:053601c38d11$14ed91e0$a301280a@.phx.gbl...
> > > Hi all!
> > >
> > > Recently, every database i create - regardless the user -
> > > comes with the dbo user with NULL login name.
> > >
> > > My older databases were created correctly, with "sa" as "dbo".
> > >
> > > I am running SQL Server 2000 SP3 on PIII 800Mhz 512MB
> > >
> > > Tanks in advance!
> > >
> > > Diego
> >
> >
>|||<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
Dan and everybody that ansewered,<br>
<br>
You're all right, i was connected in EM as NT Admin account. <br>
<br>
I ran the sp_changedbowner and solved my problem.<br>
<br>
Thanks!<br>
<br>
Diego Dias<br>
Brazil<br>
<br>
Dan Guzman wrote:<br>
<blockquote type="cite"
cite="mid%23BrzYOZjDHA.4008@.TK2MSFTNGP11.phx.gbl">
<blockquote type="cite">
<pre wrap="">This occurs when you use EM and are connected with NT-authentication.
EM then shows you NT-account and when you select the ussers-tab, dbo
</pre>
</blockquote>
<pre wrap=""><!-->has no
</pre>
<blockquote type="cite">
<pre wrap="">accout associatted.
</pre>
</blockquote>
<pre wrap=""><!-->
This can be corrected with sp_changedbowner. This may occur when a
database is restored or attached but I haven't seen the problem with
CREATE DATABASE.
</pre>
</blockquote>
<br>
</body>
</html>