Showing posts with label datetime. Show all posts
Showing posts with label datetime. Show all posts

Friday, March 30, 2012

out-of-range datetime value

Hi,

I have a page working with not problem since 3 years, and now that we've moved our database from a SQL Server 2000 to SQL Server 7 and i receive this error everywhere a date is used.

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.


The error append on this line "While myReader.Read()" .. what can i do to solve this problem ?

Thank you

This may be caused by date format that differs from the SQL2000 instance to SQL7.0 instance. Try to change the DATEFORMAT option like this (can be set to mdy,ydm,and ymd) and test again:

SET DATEFORMAT mdy
GO

You can take a look at this link about converting to datetime type:

http://msdn.microsoft.com/library/en-us/acdata/ac_8_con_03_27c5.asp?frame=true

sql

Monday, March 26, 2012

Outer join query

Hi!
I have a problem with a query:
Two tables:
CREATE TABLE Emp (empno INT, depno INT)
CREATE TABLE Work (empno INT, depno INT, date DATETIME)

I want a list of all employees that belongs to a department (from Emp
table), together with ("union") all employeees WORKING on that department a
spescial day (An employee can have been borrowed from another department
which he does not belong)

Sample data
INSERT INTO Emp (empno, depno) VALUES (1,10)
INSERT INTO Emp (empno, depno) VALUES (2,10)
INSERT INTO Emp (empno, depno) VALUES (3,20)

INSERT INTO Work (empno, depno, date) VALUES (1,10,'2003-10-17')
INSERT INTO Work (empno, depno, date) VALUES (3,10,'2003-10-17')
INSERT INTO Work (empno, depno, date) VALUES (3,10,'2003-10-18')

Note that Employee 3 works on a department to which he does not belong (he
is borrowed to another department)

The following query
SELECT empno, depno, date FROM work WHERE depno = 10 AND date = '2003-10-17'
gives me this result set:

empno depno date
1 10 2003-10-17 00:00:00.000
3 10 2003-10-17 00:00:00.000

But I want employee 2 to appear in the result set as well, because he
belongs to department 10 (eaven thoug he is not working this particular day)

The result set should look like this
empno depno date
1 10 2003-10-01 00:00:00.000
2 10 NULL
3 10 2003-10-01 00:00:00.000

I have tried different approaches, but none of them is good.
Could someone please help me?
Thanks in advance

Regards,
Gunnar Vyenli
EDB-konsulent as
NORWAYSELECT empno, depno,
CASE [date] WHEN '20031017' THEN [date] END AS [date]
FROM Work
WHERE depno = 10

Date is a reserved word and shouldn't be used as a column name (it's a
pretty meaningless name for a column anyway - Date of what?)

--
David Portas
----
Please reply only to the newsgroup
--|||Thanks for your reply, but am afraid this will not do.

I need data from BOTH the tables, not only from Work.
With your query, employee 2 will not be included in the result set, because
he belongs to the Employee table.

In other words: I want a list of all employees who belongs to depno 10,
TOGETHER with all employees which does not belong to depno 10, but work on
depno 10 this particular day.

We are talking about two categories of employees:
1) All the employees who belong to depno 10 (whether they work this day or
not)
2) Those employees who does NOT belong to depno 10, BUT is working at depno
10 this date.

A new suggestion would be apprechiated.

-Gunnar

"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:yrednQ5UUuzGQxKiRVn-vQ@.giganews.com...
> SELECT empno, depno,
> CASE [date] WHEN '20031017' THEN [date] END AS [date]
> FROM Work
> WHERE depno = 10
> Date is a reserved word and shouldn't be used as a column name (it's a
> pretty meaningless name for a column anyway - Date of what?)
> --
> David Portas
> ----
> Please reply only to the newsgroup
> --|||OK. Your DDL was missing any keys. Assuming the PK in Emp is empno and in
Work is (empno, date) and that there is an FK constraint on Work (empno NOT
NULL REFERENCES Emp (empno)):

SELECT COALESCE(E.empno,W.empno) AS empno, 10 AS depno, W.[date]
FROM Emp AS E
LEFT JOIN Work AS W
ON W.empno = E.empno AND W.[date] = '20031017'
WHERE E.depno = 10 OR W.depno = 10

--
David Portas
----
Please reply only to the newsgroup
--|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:cNednSW9WZJGaBKiRVn-vQ@.giganews.com...
> OK. Your DDL was missing any keys. Assuming the PK in Emp is empno and in
> Work is (empno, date) and that there is an FK constraint on Work (empno NOT
> NULL REFERENCES Emp (empno)):
> SELECT COALESCE(E.empno,W.empno) AS empno, 10 AS depno, W.[date]
> FROM Emp AS E
> LEFT JOIN Work AS W
> ON W.empno = E.empno AND W.[date] = '20031017'
> WHERE E.depno = 10 OR W.depno = 10

Hi David, minor aside but the call to COALESCE is unnecessary as E.empno
will do. Nice solution.

So as to not be taking up bandwidth with total triviality, here's another take.

SELECT depno, empno, MAX(work_date) AS work_date
FROM (SELECT empno, depno, "date" AS work_date
FROM Work
UNION ALL
SELECT empno, depno, NULL AS work_date
FROM Emp) AS W
WHERE depno = 10 AND
(work_date = '20031017' OR work_date IS NULL)
GROUP BY depno, empno

Regards,
jag

> --
> David Portas
> ----
> Please reply only to the newsgroup
> --

Friday, March 23, 2012

Out of range datetime value error when inserting using select...union

Hi all,
I am getting this error when insert values from one table to another in
the first table the values are varchar (10). In the second they are
datetime. The format of the data is mm/dd/yyyy to be easily converted
to dates. The conversion in this case is implicit as indicated in SQL
Server documentation. Here is my query:

INSERT INTO Campaign (CampaignID, Name, DateStart, DateEnd, ParentID,
ListID)
SELECT mysqlfactiva.dbo.campaigns.campaign_id AS CampaignID,
mysqlfactiva.dbo.campaigns.campaign_name AS Name,
MIN(mysqlfactiva.dbo.programs.start_date) AS DateStart,

MIN(mysqlfactiva.dbo.programs.end_date) AS DateEnd,
NULL AS ParentID,
NULL AS ListID
FROM mysqlfactiva.dbo.campaigns, mysqlfactiva.dbo.programs
WHERE mysqlfactiva.dbo.campaigns.campaign_id =
mysqlfactiva.dbo.programs.campaign_id
GROUP BY mysqlfactiva.dbo.campaigns.campaign_id,
mysqlfactiva.dbo.campaigns.campaign_name,
mysqlfactiva.dbo.campaigns.description
UNION
SELECT program_id + 100000, program_name, start_date, end_date,
campaign_id AS ParentID, NULL AS ListID
FROM mysqlfactiva.dbo.programs
UNION
SELECT execution_id + 200000, execution_name, start_date,
end_date, program_id + 100000 AS ParentID, NULL AS ListID
FROM mysqlfactiva.dbo.executions
UNION
SELECT wave_id + 300000, wave_name, start_date, end_date,
mysqlfactiva.dbo.waves.execution_id + 200000 AS ParentID, NULL AS
ListID
FROM mysqlfactiva.dbo.waves, mysqlfactiva.dbo.executions
WHERE mysqlfactiva.dbo.waves.execution_id =
mysqlfactiva.dbo.executions.execution_id

I am referencing programs table two times. If I just select this all I
get all data I need. When doing insert I get a message:

Server: Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value. The statement has been terminated.

If I execute just first part of the query before first union, I insert
data fine:

INSERT INTO Campaign (CampaignID, Name, DateStart, DateEnd, ParentID,
ListID)
SELECT mysqlfactiva.dbo.campaigns.campaign_id AS CampaignID,
mysqlfactiva.dbo.campaigns.campaign_name AS Name,
MIN(mysqlfactiva.dbo.programs.start_date) AS DateStart,

MIN(mysqlfactiva.dbo.programs.end_date) AS DateEnd,
NULL AS ParentID,
NULL AS ListID
FROM mysqlfactiva.dbo.campaigns, mysqlfactiva.dbo.programs
WHERE mysqlfactiva.dbo.campaigns.campaign_id =
mysqlfactiva.dbo.programs.campaign_id
GROUP BY mysqlfactiva.dbo.campaigns.campaign_id,
mysqlfactiva.dbo.campaigns.campaign_name,
mysqlfactiva.dbo.campaigns.description

As soon as I use union I get the above error. This is very strange
since even when I execute the query using first union where the dates
come from the same table 'programs' I get the error. Why I can insert
from programs first time and can's second time?

Any help will be appreciated.

Thanks,
Stan(suslikovich@.yahoo.com) writes:
> I am getting this error when insert values from one table to another in
> the first table the values are varchar (10). In the second they are
> datetime. The format of the data is mm/dd/yyyy to be easily converted
> to dates. The conversion in this case is implicit as indicated in SQL
> Server documentation. Here is my query:

Note that the interpretation is dependent on the settings for DATEFORMAT
and LANGUAGE.

> INSERT INTO Campaign (CampaignID, Name, DateStart, DateEnd, ParentID,
> ListID)
> SELECT mysqlfactiva.dbo.campaigns.campaign_id AS CampaignID,
> mysqlfactiva.dbo.campaigns.campaign_name AS Name,
> MIN(mysqlfactiva.dbo.programs.start_date) AS DateStart,
> MIN(mysqlfactiva.dbo.programs.end_date) AS DateEnd,
> NULL AS ParentID,
> NULL AS ListID
> FROM mysqlfactiva.dbo.campaigns, mysqlfactiva.dbo.programs

So the data type of mysqlfactiva.dbo.programs.start_date is varchar(10)?

Excuse me, if I am nosy, but a MIN on that columns appears somewhat
funny to me. If there is a date in early January you will use that
date, no matter which year it is in?

Better would be

MIN (CASE WHEN isdate(start_date) = 1 THEN start_date END

> As soon as I use union I get the above error. This is very strange
> since even when I execute the query using first union where the dates
> come from the same table 'programs' I get the error. Why I can insert
> from programs first time and can's second time?

In the first query, you are only taking the first value, in the
second query you are taking all the values. This query should give
you the bad data:

SELECT * FROM mysqlfactiva.dbo.programs.start_date
WHERE isdate(start_date) = 0 OR isdate(end_date) = 0

By the way, I seem recall that MySQL has a very liberal view on what
is a good date...

--
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

Tuesday, March 20, 2012

Other characters other than datetime in EM

When I clicked on tables and upon viewing all the tables on the right pane
in EM, the createdate column has some other characters appended to the
datetime... Has anyone seen that before ? Seems kinda buggy...Using SQL 2000If you have installed SQL Server 2005 on the same machine, that is the
cause...
--
http://www.aspfaq.com/
(Reverse address to reply.)
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:OL2obVChEHA.636@.TK2MSFTNGP12.phx.gbl...
> When I clicked on tables and upon viewing all the tables on the right pane
> in EM, the createdate column has some other characters appended to the
> datetime... Has anyone seen that before ? Seems kinda buggy...Using SQL
2000
>|||Gotcha... I had a feeling it could have been but wasnt too sure.. How come ?
Kinda strange..
"Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:%23s2L9HFhEHA.712@.TK2MSFTNGP09.phx.gbl...
> If you have installed SQL Server 2005 on the same machine, that is the
> cause...
> --
> http://www.aspfaq.com/
> (Reverse address to reply.)
>
>
> "Hassan" <fatima_ja@.hotmail.com> wrote in message
> news:OL2obVChEHA.636@.TK2MSFTNGP12.phx.gbl...
> > When I clicked on tables and upon viewing all the tables on the right
pane
> > in EM, the createdate column has some other characters appended to the
> > datetime... Has anyone seen that before ? Seems kinda buggy...Using SQL
> 2000
> >
> >
>|||Don't know WHY, other than it seems to be a side-by-side tools issue. You
and I are not the only ones to notice the problem.
The easy solution, of course, is to use Query Analyzer to view lists of
tables and stored procedures. ;-)
--
http://www.aspfaq.com/
(Reverse address to reply.)
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:es9rwFIhEHA.1724@.tk2msftngp13.phx.gbl...
> Gotcha... I had a feeling it could have been but wasnt too sure.. How come
?
> Kinda strange..
> "Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
> news:%23s2L9HFhEHA.712@.TK2MSFTNGP09.phx.gbl...
> > If you have installed SQL Server 2005 on the same machine, that is the
> > cause...
> >
> > --
> > http://www.aspfaq.com/
> > (Reverse address to reply.)
> >
> >
> >
> >
> > "Hassan" <fatima_ja@.hotmail.com> wrote in message
> > news:OL2obVChEHA.636@.TK2MSFTNGP12.phx.gbl...
> > > When I clicked on tables and upon viewing all the tables on the right
> pane
> > > in EM, the createdate column has some other characters appended to the
> > > datetime... Has anyone seen that before ? Seems kinda buggy...Using
SQL
> > 2000
> > >
> > >
> >
> >
>

Other characters other than datetime in EM

When I clicked on tables and upon viewing all the tables on the right pane
in EM, the createdate column has some other characters appended to the
datetime... Has anyone seen that before ? Seems kinda buggy...Using SQL 2000
If you have installed SQL Server 2005 on the same machine, that is the
cause...
http://www.aspfaq.com/
(Reverse address to reply.)
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:OL2obVChEHA.636@.TK2MSFTNGP12.phx.gbl...
> When I clicked on tables and upon viewing all the tables on the right pane
> in EM, the createdate column has some other characters appended to the
> datetime... Has anyone seen that before ? Seems kinda buggy...Using SQL
2000
>
|||Gotcha... I had a feeling it could have been but wasnt too sure.. How come ?
Kinda strange..
"Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:%23s2L9HFhEHA.712@.TK2MSFTNGP09.phx.gbl...[vbcol=seagreen]
> If you have installed SQL Server 2005 on the same machine, that is the
> cause...
> --
> http://www.aspfaq.com/
> (Reverse address to reply.)
>
>
> "Hassan" <fatima_ja@.hotmail.com> wrote in message
> news:OL2obVChEHA.636@.TK2MSFTNGP12.phx.gbl...
pane
> 2000
>
|||Don't know WHY, other than it seems to be a side-by-side tools issue. You
and I are not the only ones to notice the problem.
The easy solution, of course, is to use Query Analyzer to view lists of
tables and stored procedures. ;-)
http://www.aspfaq.com/
(Reverse address to reply.)
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:es9rwFIhEHA.1724@.tk2msftngp13.phx.gbl...
> Gotcha... I had a feeling it could have been but wasnt too sure.. How come
?[vbcol=seagreen]
> Kinda strange..
> "Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
> news:%23s2L9HFhEHA.712@.TK2MSFTNGP09.phx.gbl...
> pane
SQL
>

Other characters other than datetime in EM

When I clicked on tables and upon viewing all the tables on the right pane
in EM, the createdate column has some other characters appended to the
datetime... Has anyone seen that before ? Seems kinda buggy...Using SQL 2000If you have installed SQL Server 2005 on the same machine, that is the
cause...
http://www.aspfaq.com/
(Reverse address to reply.)
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:OL2obVChEHA.636@.TK2MSFTNGP12.phx.gbl...
> When I clicked on tables and upon viewing all the tables on the right pane
> in EM, the createdate column has some other characters appended to the
> datetime... Has anyone seen that before ? Seems kinda buggy...Using SQL
2000
>|||Gotcha... I had a feeling it could have been but wasnt too sure.. How come ?
Kinda strange..
"Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:%23s2L9HFhEHA.712@.TK2MSFTNGP09.phx.gbl...
> If you have installed SQL Server 2005 on the same machine, that is the
> cause...
> --
> http://www.aspfaq.com/
> (Reverse address to reply.)
>
>
> "Hassan" <fatima_ja@.hotmail.com> wrote in message
> news:OL2obVChEHA.636@.TK2MSFTNGP12.phx.gbl...
pane[vbcol=seagreen]
> 2000
>|||Don't know WHY, other than it seems to be a side-by-side tools issue. You
and I are not the only ones to notice the problem.
The easy solution, of course, is to use Query Analyzer to view lists of
tables and stored procedures. ;-)
http://www.aspfaq.com/
(Reverse address to reply.)
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:es9rwFIhEHA.1724@.tk2msftngp13.phx.gbl...
> Gotcha... I had a feeling it could have been but wasnt too sure.. How come
?
> Kinda strange..
> "Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
> news:%23s2L9HFhEHA.712@.TK2MSFTNGP09.phx.gbl...
> pane
SQL[vbcol=seagreen]
>