Showing posts with label sql2k. Show all posts
Showing posts with label sql2k. Show all posts

Monday, March 19, 2012

Convert to Unicode

Hi all
I have an Sql2k database with SQL Server Sort Order 185 on Code Page 1252
for non-Unicode Data
Now customers from latvia needs to use this database concurrently , is it
possible to convert the tables to Unicode when it is already running live?
Best regards
Henrik Hasselblad
SYSteamThis is a multi-part message in MIME format
--=_NextPart_000_61137148
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
You will have to change all varchars to nvarchars. You will need to
check that no string is bigger than 4000 chars and that you don't
violate the 8060 byte max rowsize.
Changing the values via alter column may cause problems too.
You can chnage the collation via alter database.
Nigel Rivett
www.nigelrivett.net
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
--=_NextPart_000_61137148
Content-Type: text/html; name="_alt.0"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename="_alt.0"
=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4=2E0= Transitional//EN">
&

Careful=2E Is transdate your= partitioning
column or is month? Make sure to include the partitioning= column in the
WHERE clause=2E
-- Tom
----=--Thomas
A=2E Moreau, BSc, PhD, MCSE, MCDBASQL Server MVPColumnist,= SQL Server
ProfessionalToronto, ON Canadahref=3D"www=2Epinnaclepublishi=">http://www=2Epinnaclepublishing=2Ecom/sql">www=2Epinnaclepublishi=ng=2Ecom/sql
"George" wrote in
message ne=ws:E5C82E40-6AE6-444D-B939-FC55D20B19C4@.microsoft=2Ecom=2E=2E=2EThanks
a lot for Tom and Dan=2E I have changed the partition tables= divided by month, and
created the table with CHECK constraint=2E My script only= references V_use once
per query, and, even as simple as 'select * from v_use where= transdate between
@.d1 and @.d2' will call every base table, although most of them= only takes 1%~2%
total cost=2E Thanks again=2E
--=_NextPart_000_61137148--

Wednesday, March 7, 2012

Convert Rows to Columns

Hi All,

I need to help with converting rows to columns in SQL2k.

Input:
Id Name Role

58Ron Doe Associate
58Mark BonasDoctor
59Mike JohnsonDoctor
59John SmithAssociate
102Chris CarterAssociate
102Ron Doe Associate
102James JonesAssociate

Output should look like:

IdDoctorAssoc1Assoc2Assoc3

58Mark BonasRon Doe NULLNULL
59Mike JohnsonJohn SmithNULLNULL
102NULLChris CarterRon Doe James Jones

There could be more than 3 associates in the input but I only need 3
above columns for associates.

I used following query:
SELECT Q.sales_id,
doctor2= (SELECT Q2.name FROM view1 Q2 where Q2.role = 'doctor'
and Q2.sales_id = Q.sales_id),
assoc1= (SELECT Q2.name FROM view1 Q2 where Q2.role =
'associate' and Q2.sales_id = Q.sales_id),
assoc2= (SELECT Q2.name FROM view1 Q2 where Q2.role =
'associate' and Q2.sales_id = Q.sales_id),
assoc3= (SELECT Q2.name FROM view1 Q2 where Q2.role =
'associate' and Q2.sales_id = Q.sales_id)
FROM view1 Q
GROUP BY sales_id

and I get this error "Subquery returned more than 1 value" since there
are multiple associate for Id 102.

Thenks<ambersaria420@.yahoo.com> wrote in message
news:1106185678.994241.297820@.c13g2000cwb.googlegr oups.com...
> Hi All,
> I need to help with converting rows to columns in SQL2k.
> Input:
> Id Name Role
> 58 Ron Doe Associate
> 58 Mark Bonas Doctor
> 59 Mike Johnson Doctor
> 59 John Smith Associate
> 102 Chris Carter Associate
> 102 Ron Doe Associate
> 102 James Jones Associate
>
> Output should look like:
> Id Doctor Assoc1 Assoc2 Assoc3
> 58 Mark Bonas Ron Doe NULL NULL
> 59 Mike Johnson John Smith NULL NULL
> 102 NULL Chris Carter Ron Doe James Jones
>
> There could be more than 3 associates in the input but I only need 3
> above columns for associates.
> I used following query:
> SELECT Q.sales_id,
> doctor2= (SELECT Q2.name FROM view1 Q2 where Q2.role = 'doctor'
> and Q2.sales_id = Q.sales_id),
> assoc1= (SELECT Q2.name FROM view1 Q2 where Q2.role =
> 'associate' and Q2.sales_id = Q.sales_id),
> assoc2= (SELECT Q2.name FROM view1 Q2 where Q2.role =
> 'associate' and Q2.sales_id = Q.sales_id),
> assoc3= (SELECT Q2.name FROM view1 Q2 where Q2.role =
> 'associate' and Q2.sales_id = Q.sales_id)
> FROM view1 Q
> GROUP BY sales_id
> and I get this error "Subquery returned more than 1 value" since there
> are multiple associate for Id 102.
> Thenks

This is very awkward to write in TSQL, especially since the number of
associates may vary - you would need a cursor (maybe even nested cursors) to
loop through the table for each ID. A better solution is to do this in the
front end or using a reporting tool.

Simon|||I changed the query to following:

select distinct v1.sales_id, v1.name as doctor2 , v2.name as assoc1,
v3.name as assoc2, v4.name as assoc3

from

(select sales_id, max(case when role = 'Doctor' then name else NULL
end) as name from view1

group by sales_id) v1

left join (select sales_id , name from view1 where role = 'Associate'
) v2 on v1.sales_id = v2.sales_id

left join (select sales_id , name from view1 where role = 'Associate'
) v3 on v1.sales_id = v3.sales_id and (v3.name is null or v3.name >
v2.name)

left join (select sales_id , name from view1 where role = 'Associate'
) v4 on v1.sales_id = v4.sales_id and (v4.name is null or( v4.name >
v2.name and v4.name > v3.name))

However now it return extra row if there is more than one associate.

New Output:

58Mark BonasRon Doe NULL NULL
59Mike Johnsonjohn2 smithNULL NULL
102NULL Chris CarterJames JonesRon Doe
102NULL Chris CarterRon Doe NULL
102NULL James JonesRon Doe NULL
102NULL Ron Doe NULL
NULL

Thanks Again.|||It's ugly to write this in SQL. If you really want to do this, you can
do it using temp tables.
-------------------------------------

create table #T
(i int, name varchar(50), role varchar(50))

insert #T
values('58','Ron Doe','Associate')
insert #T
values('58','Mark Bonas','Doctor')
insert #T
values('59','Mike Johnson','Doctor')
insert #T
values('59','John Smith','Associate')
insert #T
values('102','Chris Carter','Associate')
insert #T
values('102','Ron Doe','Associate')
insert #T
values('102','James Jones','Associate')

-- doctor and first associate
select
i,
min(case when role='doctor' then name else 'zzz' end) as doctor,
min(case when role='associate' then name else 'zzz' end) as
associate
into #T1
from #T
group by i

-- second associate
select
#T.i,
min(case when role='associate' then name else 'zzz' end) as
associate
into #T2
from #T
left join #T1 on #T.i=#T1.i and #T.name=#T1.associate
where #T1.associate is null
group by #T.i

-- third associate
select
#T.i,
min(case when role='associate' then name else 'zzz' end) as
associate
into #T3
from #T
left join #T1 on #T.i=#T1.i and #T.name=#T1.associate
left join #T2 on #T.i=#T2.i and #T.name=#T2.associate
where #T1.associate is null and #T2.associate is null
group by #T.i

-- output
select #T1.i, #T1.doctor, #T1.associate as assoc1, #T2.associate as
assoc2, #T3.associate as assoc3
from #T1
join #T2 on #T1.i=#T2.i
join #T3 on #T1.i=#T3.i

Friday, February 10, 2012

Convert Date with T-SQL

Hi,

problem with transact-SQL (SQL2k). I'm gathering informations from2 different tables and the column that interest me (the invoice date) have same format but not filled in the same. One is an old table and the format is "datetime" (but written like dd/mm/yyyy) and the new one "datetime" as well but "dd/mm/yyyy hh:mm:ss" (and it's not small datetime)
I'd like to convert the date format wich appears with the "hh:mm:ss" to a smaller format (just dd/mm/yyyy).
I'v tried with the conversion into text (the CONVERT function limits) wich works but I need to deal with a date format when I get the result. If I re convert to smalldatetime, I get the hh:mm:ss again.

Hope this clear.

ThanksDDL and some sample data would be a BIG help...|||SQL Server always stores the date and time. There isn't any way to change that.

You can change how your client displays what SQL Server returns. I think that is what you need to address.

-PatP|||Select ShortInvoiceDt = CONVERT(VARCHAR(10), NewerInvoiceDate, 101)
FROM NewerTable

Use the client app to format accordingly. For example, are you using Access? Crystal? VB?|||I'm testing differents client apps, like Business Object and Harry soft as the data are re stored in a datawarehouse DB.

And yes, seems I don't have the choice, deal with the client App.

Brett, Do you need more details like example?

Thanks for your posts, I'll spare time now searching on the wright place.

Cheers|||In addition to the other reply's,
maybe this will also help some people who read this topic...
------------------
declare
@.invoice_date datetime,
@.new_date varchar(10)

set @.invoice_date = getdate()
-- 'Cut' the time part
select @.new_date = convert(varchar,@.invoice_date,103)
-- Display date
select @.new_date as new_date
-- Work with the date in date format with the time set to zero and convert it later to character for displaying
-- only the date without time part
select convert(datetime,@.new_date,103) as working_date