Showing posts with label field. Show all posts
Showing posts with label field. Show all posts

Sunday, March 25, 2012

Day of year

How would I get the day of year from a datetime field in a query?
Such as Feb. 28 would be day 59.
Thank youSELECT DATEPART(DAYOFYEAR,'20050228') ;
David Portas
SQL Server MVP
--
"Lyners" <Lyners@.discussions.microsoft.com> wrote in message
news:78DFFDDD-9227-4A95-896E-F50FDCBB4E17@.microsoft.com...
> How would I get the day of year from a datetime field in a query?
> Such as Feb. 28 would be day 59.
> Thank you

day name of a date

Hello,
One of my group field is a date, and I need to return day name, like Monday,
Tuesday, .. how can I do this?
Thanks,Imagine you have OrderDate; then the exression could be
=WeekDayName(WeekDay(Fields!OrderDate.Value))
--
Dejan Sarka, SQL Server MVP
Associate Mentor
www.SolidQualityLearning.com
"JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
news:9F7027F2-7A04-4D0A-9176-63514D0599AA@.microsoft.com...
> Hello,
> One of my group field is a date, and I need to return day name, like
Monday,
> Tuesday, .. how can I do this?
> Thanks,
>sql

Thursday, March 22, 2012

DateTime without the time

Hi,

Im moving data from a OLE DB Source to a Flat File Destination.


I have a DateTime field in my database.

My current query returns:
2007-05-21 00:00:00

How can I make it return:
2007-05-21

Thank you!! Smile

Use a derived column to cast the field to DT_DBDATE...

(DT_DBDATE)[YourDateTimeField]|||

I′ve modified the query so it returns only the date.

However, the Flat File Destination always changes it back to a DateTime.

|||

MrHat wrote:

I′ve modified the query so it returns only the date.

However, the Flat File Destination always changes it back to a DateTime.

Yes, you need to define the data type of that column to DT_DBDATE in the flat file connection manager.|||My SQL server destination changes back to DT_DBtimestamp.....in my sql table it has datatype of datetime....but I do not want to display the Time.....just the date....any ideas?|||

JStutz wrote:

My SQL server destination changes back to DT_DBtimestamp.....in my sql table it has datatype of datetime....but I do not want to display the Time.....just the date....any ideas?

Displaying just the time is a simple transact-sql statement using the CONVERT function.

|||

Can you use a SQL Command in your source?

If so, use CONVERT(varchar, <dateField>, 112) in your select list

|||

SQL-PRO wrote:

Can you use a SQL Command in your source?

If so, use CONVERT(varchar, <dateField>, 112) in your select list

Still if that's in your source query, you can't store it that way -- not in SQL Server anyway. (Unless you're storing it in a varchar field.)

DateTime without the time

Hi,

Im moving data from a OLE DB Source to a Flat File Destination.


I have a DateTime field in my database.

My current query returns:
2007-05-21 00:00:00

How can I make it return:
2007-05-21

Thank you!! Smile

Use a derived column to cast the field to DT_DBDATE...

(DT_DBDATE)[YourDateTimeField]|||

I′ve modified the query so it returns only the date.

However, the Flat File Destination always changes it back to a DateTime.

|||

MrHat wrote:

I′ve modified the query so it returns only the date.

However, the Flat File Destination always changes it back to a DateTime.

Yes, you need to define the data type of that column to DT_DBDATE in the flat file connection manager.|||My SQL server destination changes back to DT_DBtimestamp.....in my sql table it has datatype of datetime....but I do not want to display the Time.....just the date....any ideas?|||

JStutz wrote:

My SQL server destination changes back to DT_DBtimestamp.....in my sql table it has datatype of datetime....but I do not want to display the Time.....just the date....any ideas?

Displaying just the time is a simple transact-sql statement using the CONVERT function.

|||

Can you use a SQL Command in your source?

If so, use CONVERT(varchar, <dateField>, 112) in your select list

|||

SQL-PRO wrote:

Can you use a SQL Command in your source?

If so, use CONVERT(varchar, <dateField>, 112) in your select list

Still if that's in your source query, you can't store it that way -- not in SQL Server anyway. (Unless you're storing it in a varchar field.)

Date-time unsupported in subquery?

I have a query that returns 2 fields, a date-time field and a Count(*)
field. Basically it tells me how many people exist for a specific date. I
want to use this as a subquery and test for the earliest date that the
Count(*) field (members) is below a certain number. However, once I use it
in a subquery I get an error on the date-time field that it is an
unsupported data type... any clues?
SELECT TermEndDate, Members
FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS Members
FROM Person_mm_Board AS P
WHERE (BoardID = @.BoardID)
GROUP BY TermEndDate
ORDER BY TermEndDate) AS SAre you making this too difficult? Perhaps this will do the same without err
or.
SELECT
TermEndDate
, count( Members )
FROM Person_mm_Board
WHERE BoardID = @.BoardID
GROUP BY TermEndDate
ORDER BY TermEndDate
--
Arnie Rowland*
"To be successful, your heart must accompany your knowledge."
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.
gbl...
>I have a query that returns 2 fields, a date-time field and a Count(*)
> field. Basically it tells me how many people exist for a specific date.
I
> want to use this as a subquery and test for the earliest date that the
> Count(*) field (members) is below a certain number. However, once I use i
t
> in a subquery I get an error on the date-time field that it is an
> unsupported data type... any clues?
>
> SELECT TermEndDate, Members
> FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS Member
s
> FROM Person_mm_Board AS P
> WHERE (BoardID = @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S
>
>|||Yes this will do the same thing. I'm not finished with the main query yet.
The end query will look something like below. Just trying to simplify thin
gs to find the source of the problem. The question remains - Why is the dat
etime field not showing up when used as a subquery? Thanks.
SELECT TOP(1) TermEndDate, Members
FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS Members,
BoardID
FROM Person_mm_Board AS P
WHERE (BoardID = @.BoardID)
GROUP BY TermEndDate
ORDER BY TermEndDate) AS S
INNER JOIN Board AS B
ON B.BoardID = S.BoardID
WHERE S.Members < B.Members
"Arnie Rowland" <arnie@.1568.com> wrote in message news:eVSMvOcpGHA.2400@.TK2M
SFTNGP03.phx.gbl...
Are you making this too difficult? Perhaps this will do the same without err
or.
SELECT
TermEndDate
, count( Members )
FROM Person_mm_Board
WHERE BoardID = @.BoardID
GROUP BY TermEndDate
ORDER BY TermEndDate
--
Arnie Rowland*
"To be successful, your heart must accompany your knowledge."
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.
gbl...
>I have a query that returns 2 fields, a date-time field and a Count(*)
> field. Basically it tells me how many people exist for a specific date.
I
> want to use this as a subquery and test for the earliest date that the
> Count(*) field (members) is below a certain number. However, once I use i
t
> in a subquery I get an error on the date-time field that it is an
> unsupported data type... any clues?
>
> SELECT TermEndDate, Members
> FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS Member
s
> FROM Person_mm_Board AS P
> WHERE (BoardID = @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S
>
>|||OK I cast the datetime field to varchar and then back to datetime in the mai
n query and it seems to work. Here's my final query.
SELECT TOP (1) S.Members, CAST(S.Expr1 AS datetime) AS TermEnd
FROM (SELECT TOP (100) PERCENT CAST(TermEndDate AS Varchar) AS E
xpr1, COUNT(*) AS Members, BoardID
FROM Person_mm_Board AS P
WHERE (BoardID = @.BoardID)
GROUP BY TermEndDate, BoardID
ORDER BY TermEndDate) AS S INNER JOIN
Board AS B ON S.BoardID = B.BoardID AND S.Members < B.NumberMembers
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message news:OJ3MOdcpGHA.4548@.TK2
MSFTNGP03.phx.gbl...
Yes this will do the same thing. I'm not finished with the main query yet.
The end query will look something like below. Just trying to simplify thin
gs to find the source of the problem. The question remains - Why is the dat
etime field not showing up when used as a subquery? Thanks.
SELECT TOP(1) TermEndDate, Members
FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS Members,
BoardID
FROM Person_mm_Board AS P
WHERE (BoardID = @.BoardID)
GROUP BY TermEndDate
ORDER BY TermEndDate) AS S
INNER JOIN Board AS B
ON B.BoardID = S.BoardID
WHERE S.Members < B.Members
"Arnie Rowland" <arnie@.1568.com> wrote in message news:eVSMvOcpGHA.2400@.TK2M
SFTNGP03.phx.gbl...
Are you making this too difficult? Perhaps this will do the same without err
or.
SELECT
TermEndDate
, count( Members )
FROM Person_mm_Board
WHERE BoardID = @.BoardID
GROUP BY TermEndDate
ORDER BY TermEndDate
--
Arnie Rowland*
"To be successful, your heart must accompany your knowledge."
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.
gbl...
>I have a query that returns 2 fields, a date-time field and a Count(*)
> field. Basically it tells me how many people exist for a specific date.
I
> want to use this as a subquery and test for the earliest date that the
> Count(*) field (members) is below a certain number. However, once I use i
t
> in a subquery I get an error on the date-time field that it is an
> unsupported data type... any clues?
>
> SELECT TermEndDate, Members
> FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS Member
s
> FROM Person_mm_Board AS P
> WHERE (BoardID = @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S
>
>|||Just to clarify:
if you remove the parenthesis around the number 100 so that the code reads
... (SELECT TOP 100 PERCENT ...
your code is good to run.
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message
news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.gbl...
>I have a query that returns 2 fields, a date-time field and a Count(*)
>field. Basically it tells me how many people exist for a specific date. I
>want to use this as a subquery and test for the earliest date that the
>Count(*) field (members) is below a certain number. However, once I use it
>in a subquery I get an error on the date-time field that it is an
>unsupported data type... any clues?
> SELECT TermEndDate, Members
> FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS
> Members
> FROM Person_mm_Board AS P
> WHERE (BoardID = @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S
>|||On Wed, 12 Jul 2006 09:48:39 -0500, Ryan wrote:

>I have a query that returns 2 fields, a date-time field and a Count(*)
>field. Basically it tells me how many people exist for a specific date. I
>want to use this as a subquery and test for the earliest date that the
>Count(*) field (members) is below a certain number. However, once I use it
>in a subquery I get an error on the date-time field that it is an
>unsupported data type... any clues?
Hi Ryan,
None at all. Datetime columns are allowed in subqueries. It might help
if could post the actual error message (use copy and paste to prevent
transcription errors). I'd also like to see the structure of the table
(posted as a CREATE TABLE statement).

>SELECT TermEndDate, Members
>FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS Members
> FROM Person_mm_Board AS P
> WHERE (BoardID = @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S
The ORDER BY and the TOP (100) PERCENT in the subquery are completely
useless. Get rid of them.
If you need the results to be ordered, put an ORDER BY clause on the
outer query:
SELECT TermEndDate, Members
FROM (SELECT TermEndDate, COUNT(*) AS Members
FROM Person_mm_Board AS P
WHERE BoardID = @.BoardID)
GROUP BY TermEndDate) AS S
ORDER BY TermEndDate
Hugo Kornelis, SQL Server MVP|||It still looks like you are making this too difficult.
Datetime fields work just fine in sub-queries.
Are you attempting to locate the next Board Member with term expiring?
Having a bit more detail about what you are working with (table DDL, sample
data, complete problem story) sure would make it easier to assist you. Witho
ut that, we are just playing twenty questions with you.
--
Arnie Rowland*
"To be successful, your heart must accompany your knowledge."
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message news:OE5bfhcpGHA.756@.TK2M
SFTNGP05.phx.gbl...
OK I cast the datetime field to varchar and then back to datetime in the mai
n query and it seems to work. Here's my final query.
SELECT TOP (1) S.Members, CAST(S.Expr1 AS datetime) AS TermEnd
FROM (SELECT TOP (100) PERCENT CAST(TermEndDate AS Varchar) AS E
xpr1, COUNT(*) AS Members, BoardID
FROM Person_mm_Board AS P
WHERE (BoardID = @.BoardID)
GROUP BY TermEndDate, BoardID
ORDER BY TermEndDate) AS S INNER JOIN
Board AS B ON S.BoardID = B.BoardID AND S.Members < B.NumberMembers
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message news:OJ3MOdcpGHA.4548@.TK2
MSFTNGP03.phx.gbl...
Yes this will do the same thing. I'm not finished with the main query yet.
The end query will look something like below. Just trying to simplify thin
gs to find the source of the problem. The question remains - Why is the dat
etime field not showing up when used as a subquery? Thanks.
SELECT TOP(1) TermEndDate, Members
FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS Members,
BoardID
FROM Person_mm_Board AS P
WHERE (BoardID = @.BoardID)
GROUP BY TermEndDate
ORDER BY TermEndDate) AS S
INNER JOIN Board AS B
ON B.BoardID = S.BoardID
WHERE S.Members < B.Members
"Arnie Rowland" <arnie@.1568.com> wrote in message news:eVSMvOcpGHA.2400@.TK2M
SFTNGP03.phx.gbl...
Are you making this too difficult? Perhaps this will do the same without err
or.
SELECT
TermEndDate
, count( Members )
FROM Person_mm_Board
WHERE BoardID = @.BoardID
GROUP BY TermEndDate
ORDER BY TermEndDate
--
Arnie Rowland*
"To be successful, your heart must accompany your knowledge."
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.
gbl...
>I have a query that returns 2 fields, a date-time field and a Count(*)
> field. Basically it tells me how many people exist for a specific date.
I
> want to use this as a subquery and test for the earliest date that the
> Count(*) field (members) is below a certain number. However, once I use i
t
> in a subquery I get an error on the date-time field that it is an
> unsupported data type... any clues?
>
> SELECT TermEndDate, Members
> FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS Member
s
> FROM Person_mm_Board AS P
> WHERE (BoardID = @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S
>
>

Date-time unsupported in subquery?

I have a query that returns 2 fields, a date-time field and a Count(*)
field. Basically it tells me how many people exist for a specific date. I
want to use this as a subquery and test for the earliest date that the
Count(*) field (members) is below a certain number. However, once I use it
in a subquery I get an error on the date-time field that it is an
unsupported data type... any clues?
SELECT TermEndDate, Members
FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS Members
FROM Person_mm_Board AS P
WHERE (BoardID = @.BoardID)
GROUP BY TermEndDate
ORDER BY TermEndDate) AS SThis is a multi-part message in MIME format.
--=_NextPart_000_0BC1_01C6A589.01BE9E50
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Are you making this too difficult? Perhaps this will do the same without =error.
SELECT
TermEndDate
, count( Members )
FROM Person_mm_Board
WHERE BoardID =3D @.BoardID
GROUP BY TermEndDate
ORDER BY TermEndDate
-- Arnie Rowland* "To be successful, your heart must accompany your knowledge."
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message =news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.gbl...
>I have a query that returns 2 fields, a date-time field and a Count(*) > field. Basically it tells me how many people exist for a specific =date. I > want to use this as a subquery and test for the earliest date that the =
> Count(*) field (members) is below a certain number. However, once I =use it > in a subquery I get an error on the date-time field that it is an > unsupported data type... any clues?
> > SELECT TermEndDate, Members
> FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS =Members
> FROM Person_mm_Board AS P
> WHERE (BoardID =3D @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S > >
--=_NextPart_000_0BC1_01C6A589.01BE9E50
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

Are you making this too difficult? =Perhaps this will do the same without error.
SELECT TermEndDate , count( Members )FROM =Person_mm_BoardWHERE BoardID =3D @.BoardIDGROUP BY TermEndDateORDER BY =TermEndDate
-- Arnie Rowland* "To be =successful, your heart must accompany your knowledge."
"Ryan" wrote in message news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.gbl...>I =have a query that returns 2 fields, a date-time field and a Count(*) > =field. Basically it tells me how many people exist for a specific date. I => want to use this as a subquery and test for the earliest date =that the > Count(*) field (members) is below a certain number. =However, once I use it > in a subquery I get an error on the date-time field =that it is an > unsupported data type... any clues?> > SELECT TermEndDate, Members> FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) =AS Members> &nbs=p;  =; FROM =Person_mm_Board AS P> &nbs=p; WHERE (BoardID =3D @.BoardID)> &n=bsp; &nb=sp; GROUP BY TermEndDate> = &=nbsp; ORDER BY TermEndDate) AS S > >

--=_NextPart_000_0BC1_01C6A589.01BE9E50--|||This is a multi-part message in MIME format.
--=_NextPart_000_0033_01C6A59D.202C71F0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Yes this will do the same thing. I'm not finished with the main query =yet. The end query will look something like below. Just trying to =simplify things to find the source of the problem. The question remains =- Why is the datetime field not showing up when used as a subquery? =Thanks.
SELECT TOP(1) TermEndDate, Members
FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS =Members, BoardID
FROM Person_mm_Board AS P
WHERE (BoardID =3D @.BoardID)
GROUP BY TermEndDate
ORDER BY TermEndDate) AS S INNER JOIN Board AS B ON B.BoardID =3D S.BoardID
WHERE S.Members < B.Members "Arnie Rowland" <arnie@.1568.com> wrote in message =news:eVSMvOcpGHA.2400@.TK2MSFTNGP03.phx.gbl...
Are you making this too difficult? Perhaps this will do the same =without error.
SELECT
TermEndDate
, count( Members )
FROM Person_mm_Board
WHERE BoardID =3D @.BoardID
GROUP BY TermEndDate
ORDER BY TermEndDate
-- Arnie Rowland* "To be successful, your heart must accompany your knowledge."
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message =news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.gbl...
>I have a query that returns 2 fields, a date-time field and a =Count(*) > field. Basically it tells me how many people exist for a specific =date. I > want to use this as a subquery and test for the earliest date that =the > Count(*) field (members) is below a certain number. However, once I =use it > in a subquery I get an error on the date-time field that it is an > unsupported data type... any clues?
> > SELECT TermEndDate, Members
> FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS =Members
> FROM Person_mm_Board AS P
> WHERE (BoardID =3D @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S > >
--=_NextPart_000_0033_01C6A59D.202C71F0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

Yes this will do the same thing. =I'm not finished with the main query yet. The end query will look =something like below. Just trying to simplify things to find the source of the problem. The question remains - Why is the datetime field not =showing up when used as a subquery? Thanks.
SELECT TOP(1) TermEndDate, MembersFROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) =AS Members, BoardID &n=bsp; FROM =Person_mm_Board AS P &n=bsp; WHERE (BoardID =3D @.BoardID) = =GROUP BY TermEndDate &nbs=p;  =; ORDER BY TermEndDate) AS S
INNER JOIN Board AS B
ON B.BoardID =3D S.BoardID
WHERE S.Members < B.Members
"Arnie Rowland" wrote in message =news:eVSMvOcpGHA.2400=@.TK2MSFTNGP03.phx.gbl...
Are you making this too difficult? =Perhaps this will do the same without error.

SELECT TermEndDate , count( Members )FROM Person_mm_BoardWHERE BoardID =3D @.BoardIDGROUP BY =TermEndDateORDER BY TermEndDate

-- Arnie Rowland* "To be =successful, your heart must accompany your knowledge."


"Ryan" wrote in message news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.gbl...>I have a query that returns 2 fields, a date-time field and a Count(*) > =field. Basically it tells me how many people exist for a specific date. =I > want to use this as a subquery and test for the earliest date =that the > Count(*) field (members) is below a certain number. = However, once I use it > in a subquery I get an error on the =date-time field that it is an > unsupported data type... any =clues?> > SELECT TermEndDate, Members> FROM (SELECT TOP (100) PERCENT TermEndDate, =COUNT(*) AS =Members> &nbs=p;  =; FROM =Person_mm_Board AS =P> &nbs=p; WHERE (BoardID =3D =@.BoardID)> &n=bsp; &nb=sp; GROUP BY =TermEndDate> = &=nbsp; ORDER BY TermEndDate) AS S > > =

--=_NextPart_000_0033_01C6A59D.202C71F0--|||This is a multi-part message in MIME format.
--=_NextPart_000_004C_01C6A59E.30B9DA20
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
OK I cast the datetime field to varchar and then back to datetime in the =main query and it seems to work. Here's my final query.
SELECT TOP (1) S.Members, CAST(S.Expr1 AS datetime) AS TermEnd
FROM (SELECT TOP (100) PERCENT CAST(TermEndDate AS Varchar) =AS Expr1, COUNT(*) AS Members, BoardID
FROM Person_mm_Board AS P
WHERE (BoardID =3D @.BoardID)
GROUP BY TermEndDate, BoardID
ORDER BY TermEndDate) AS S INNER JOIN
Board AS B ON S.BoardID =3D B.BoardID AND =S.Members < B.NumberMembers
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message =news:OJ3MOdcpGHA.4548@.TK2MSFTNGP03.phx.gbl...
Yes this will do the same thing. I'm not finished with the main query =yet. The end query will look something like below. Just trying to =simplify things to find the source of the problem. The question remains =- Why is the datetime field not showing up when used as a subquery? =Thanks.
SELECT TOP(1) TermEndDate, Members
FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS =Members, BoardID
FROM Person_mm_Board AS P
WHERE (BoardID =3D @.BoardID)
GROUP BY TermEndDate
ORDER BY TermEndDate) AS S INNER JOIN Board AS B ON B.BoardID =3D S.BoardID
WHERE S.Members < B.Members "Arnie Rowland" <arnie@.1568.com> wrote in message =news:eVSMvOcpGHA.2400@.TK2MSFTNGP03.phx.gbl...
Are you making this too difficult? Perhaps this will do the same =without error.
SELECT
TermEndDate
, count( Members )
FROM Person_mm_Board
WHERE BoardID =3D @.BoardID
GROUP BY TermEndDate
ORDER BY TermEndDate
-- Arnie Rowland* "To be successful, your heart must accompany your knowledge."
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message =news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.gbl...
>I have a query that returns 2 fields, a date-time field and a =Count(*) > field. Basically it tells me how many people exist for a specific =date. I > want to use this as a subquery and test for the earliest date that =the > Count(*) field (members) is below a certain number. However, once =I use it > in a subquery I get an error on the date-time field that it is an > unsupported data type... any clues?
> > SELECT TermEndDate, Members
> FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) =AS Members
> FROM Person_mm_Board AS P
> WHERE (BoardID =3D @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S > >
--=_NextPart_000_004C_01C6A59E.30B9DA20
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

OK I cast the datetime field to varchar =and then back to datetime in the main query and it seems to work. Here's my =final query.
SELECT TOP (1) =S.Members, CAST(S.Expr1 AS datetime) AS TermEndFROM (SELECT TOP (100) PERCENT CAST(TermEndDate AS =Varchar) AS Expr1, COUNT(*) AS Members, BoardID &n=bsp; FROM =Person_mm_Board AS P &n=bsp; WHERE (BoardID =3D @.BoardID) = =GROUP BY TermEndDate, BoardID &n=bsp; ORDER BY TermEndDate) AS S INNER JOIN  =; Board AS B ON S.BoardID =3D B.BoardID AND S.Members < B.NumberMembers
"Ryan" = wrote in message news:OJ3MOdcpGHA.4548=@.TK2MSFTNGP03.phx.gbl...
Yes this will do the same =thing. I'm not finished with the main query yet. The end query will look =something like below. Just trying to simplify things to find the source of the problem. The question remains - Why is the datetime field not =showing up when used as a subquery? Thanks.

SELECT TOP(1) TermEndDate, MembersFROM (SELECT TOP (100) PERCENT TermEndDate, =COUNT(*) AS Members, =BoardID &n=bsp; FROM =Person_mm_Board AS =P &n=bsp; WHERE (BoardID =3D =@.BoardID) = = GROUP BY =TermEndDate &nbs=p;  =; ORDER BY TermEndDate) AS S
INNER JOIN Board AS B
ON B.BoardID =3D S.BoardID
WHERE S.Members < B.Members
"Arnie Rowland" wrote in =message news:eVSMvOcpGHA.2400=@.TK2MSFTNGP03.phx.gbl...
Are you making this too difficult? =Perhaps this will do the same without error.

SELECT TermEndDate , count( Members )FROM Person_mm_BoardWHERE BoardID =3D @.BoardIDGROUP BY =TermEndDateORDER BY TermEndDate

-- Arnie Rowland* "To =be successful, your heart must accompany your knowledge."


"Ryan" wrote in message news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.gbl...>I have a query that returns 2 fields, a date-time field and a Count(*) > field. Basically it tells me how many people exist for a =specific date. I > want to use this as a subquery and test for =the earliest date that the > Count(*) field (members) is below a =certain number. However, once I use it > in a subquery I get an =error on the date-time field that it is an > unsupported data =type... any clues?> > SELECT =TermEndDate, Members> FROM = (SELECT TOP (100) PERCENT TermEndDate, =COUNT(*) AS =Members> &nbs=p;  =; FROM =Person_mm_Board AS =P> &nbs=p; WHERE (BoardID =3D =@.BoardID)> &n=bsp; &nb=sp; GROUP BY =TermEndDate> = &=nbsp; ORDER BY TermEndDate) AS S > >

--=_NextPart_000_004C_01C6A59E.30B9DA20--|||Just to clarify:
if you remove the parenthesis around the number 100 so that the code reads
... (SELECT TOP 100 PERCENT ...
your code is good to run.
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message
news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.gbl...
>I have a query that returns 2 fields, a date-time field and a Count(*)
>field. Basically it tells me how many people exist for a specific date. I
>want to use this as a subquery and test for the earliest date that the
>Count(*) field (members) is below a certain number. However, once I use it
>in a subquery I get an error on the date-time field that it is an
>unsupported data type... any clues?
> SELECT TermEndDate, Members
> FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS
> Members
> FROM Person_mm_Board AS P
> WHERE (BoardID = @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S
>|||On Wed, 12 Jul 2006 09:48:39 -0500, Ryan wrote:
>I have a query that returns 2 fields, a date-time field and a Count(*)
>field. Basically it tells me how many people exist for a specific date. I
>want to use this as a subquery and test for the earliest date that the
>Count(*) field (members) is below a certain number. However, once I use it
>in a subquery I get an error on the date-time field that it is an
>unsupported data type... any clues?
Hi Ryan,
None at all. Datetime columns are allowed in subqueries. It might help
if could post the actual error message (use copy and paste to prevent
transcription errors). I'd also like to see the structure of the table
(posted as a CREATE TABLE statement).
>SELECT TermEndDate, Members
>FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS Members
> FROM Person_mm_Board AS P
> WHERE (BoardID = @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S
The ORDER BY and the TOP (100) PERCENT in the subquery are completely
useless. Get rid of them.
If you need the results to be ordered, put an ORDER BY clause on the
outer query:
SELECT TermEndDate, Members
FROM (SELECT TermEndDate, COUNT(*) AS Members
FROM Person_mm_Board AS P
WHERE BoardID = @.BoardID)
GROUP BY TermEndDate) AS S
ORDER BY TermEndDate
Hugo Kornelis, SQL Server MVP|||This is a multi-part message in MIME format.
--=_NextPart_000_0C84_01C6A5D2.9A8AF130
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
It still looks like you are making this too difficult.
Datetime fields work just fine in sub-queries.
Are you attempting to locate the next Board Member with term expiring?
Having a bit more detail about what you are working with (table DDL, =sample data, complete problem story) sure would make it easier to assist =you. Without that, we are just playing twenty questions with you.
-- Arnie Rowland* "To be successful, your heart must accompany your knowledge."
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message =news:OE5bfhcpGHA.756@.TK2MSFTNGP05.phx.gbl...
OK I cast the datetime field to varchar and then back to datetime in =the main query and it seems to work. Here's my final query.
SELECT TOP (1) S.Members, CAST(S.Expr1 AS datetime) AS TermEnd
FROM (SELECT TOP (100) PERCENT CAST(TermEndDate AS =Varchar) AS Expr1, COUNT(*) AS Members, BoardID
FROM Person_mm_Board AS P
WHERE (BoardID =3D @.BoardID)
GROUP BY TermEndDate, BoardID
ORDER BY TermEndDate) AS S INNER JOIN
Board AS B ON S.BoardID =3D B.BoardID AND =S.Members < B.NumberMembers
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message =news:OJ3MOdcpGHA.4548@.TK2MSFTNGP03.phx.gbl...
Yes this will do the same thing. I'm not finished with the main =query yet. The end query will look something like below. Just trying =to simplify things to find the source of the problem. The question =remains - Why is the datetime field not showing up when used as a =subquery? Thanks.
SELECT TOP(1) TermEndDate, Members
FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) AS =Members, BoardID
FROM Person_mm_Board AS P
WHERE (BoardID =3D @.BoardID)
GROUP BY TermEndDate
ORDER BY TermEndDate) AS S INNER JOIN Board AS B ON B.BoardID =3D S.BoardID
WHERE S.Members < B.Members "Arnie Rowland" <arnie@.1568.com> wrote in message =news:eVSMvOcpGHA.2400@.TK2MSFTNGP03.phx.gbl...
Are you making this too difficult? Perhaps this will do the same =without error.
SELECT
TermEndDate
, count( Members )
FROM Person_mm_Board
WHERE BoardID =3D @.BoardID
GROUP BY TermEndDate
ORDER BY TermEndDate
-- Arnie Rowland* "To be successful, your heart must accompany your knowledge."
"Ryan" <Tyveil@.newsgroups.nospam> wrote in message =news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.gbl...
>I have a query that returns 2 fields, a date-time field and a =Count(*) > field. Basically it tells me how many people exist for a =specific date. I > want to use this as a subquery and test for the earliest date =that the > Count(*) field (members) is below a certain number. However, =once I use it > in a subquery I get an error on the date-time field that it is =an > unsupported data type... any clues?
> > SELECT TermEndDate, Members
> FROM (SELECT TOP (100) PERCENT TermEndDate, COUNT(*) =AS Members
> FROM Person_mm_Board AS P
> WHERE (BoardID =3D @.BoardID)
> GROUP BY TermEndDate
> ORDER BY TermEndDate) AS S > >
--=_NextPart_000_0C84_01C6A5D2.9A8AF130
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

It still looks like you are making this =too difficult.
Datetime fields work just fine in sub-queries.
Are you attempting to locate the next =Board Member with term expiring?
Having a bit more detail about what you =are working with (table DDL, sample data, complete problem story) sure would make it =easier to assist you. Without that, we are just playing twenty questions with =you.
-- Arnie Rowland* "To be successful, your heart must =accompany your knowledge."
"Ryan" = wrote in message news:OE5bfhcpGHA.756@.T=K2MSFTNGP05.phx.gbl...
OK I cast the datetime field to =varchar and then back to datetime in the main query and it seems to work. Here's =my final query.

SELECT TOP =(1) S.Members, CAST(S.Expr1 AS datetime) AS TermEndFROM (SELECT TOP (100) PERCENT CAST(TermEndDate AS =Varchar) AS Expr1, COUNT(*) AS Members, =BoardID &n=bsp; FROM =Person_mm_Board AS =P &n=bsp; WHERE (BoardID =3D =@.BoardID) = = GROUP BY TermEndDate, =BoardID &n=bsp; ORDER BY TermEndDate) AS S INNER =JOIN  =; Board AS B ON S.BoardID =3D B.BoardID AND S.Members < B.NumberMembers
"Ryan" = wrote in message news:OJ3MOdcpGHA.4548=@.TK2MSFTNGP03.phx.gbl...
Yes this will do the same =thing. I'm not finished with the main query yet. The end query will look =something like below. Just trying to simplify things to find the source =of the problem. The question remains - Why is the datetime field not =showing up when used as a subquery? Thanks.

SELECT TOP(1) TermEndDate, MembersFROM (SELECT TOP (100) PERCENT TermEndDate, =COUNT(*) AS Members, =BoardID &n=bsp; FROM =Person_mm_Board AS =P &n=bsp; WHERE (BoardID =3D =@.BoardID) = = GROUP BY =TermEndDate &nbs=p;  =; ORDER BY TermEndDate) AS S
INNER JOIN Board AS B
ON B.BoardID =3D S.BoardID
WHERE S.Members < B.Members
"Arnie Rowland" wrote in =message news:eVSMvOcpGHA.2400=@.TK2MSFTNGP03.phx.gbl...
Are you making this too =difficult? Perhaps this will do the same without error.

SELECT TermEndDate , count( Members )FROM Person_mm_BoardWHERE BoardID =3D @.BoardIDGROUP BY TermEndDateORDER BY TermEndDate

-- Arnie Rowland* "To =be successful, your heart must accompany your =knowledge."


"Ryan" wrote in message news:%237V2DKcpGHA.4760@.TK2MSFTNGP05.phx.gbl...>I have a query that returns 2 fields, a date-time field and a Count(*) => field. Basically it tells me how many people exist for a =specific date. I > want to use this as a subquery and test for =the earliest date that the > Count(*) field (members) is below =a certain number. However, once I use it > in a =subquery I get an error on the date-time field that it is an > unsupported =data type... any clues?> > =SELECT TermEndDate, Members> FROM (SELECT TOP (100) PERCENT TermEndDate, =COUNT(*) AS =Members> &nbs=p;  =; FROM =Person_mm_Board AS =P> &nbs=p; WHERE (BoardID =3D =@.BoardID)> &n=bsp; &nb=sp; GROUP BY =TermEndDate> = &=nbsp; ORDER BY TermEndDate) AS S > >

--=_NextPart_000_0C84_01C6A5D2.9A8AF130--

DateTime unable to save in datetime field of SQL database

Hi all, having a little problem with saving dates to sql database

I've got the CreatedOn field in the table set to datetime type, but every time i try and run it i get an error kicked up

Error "

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

I've tried researching it but not been able to find something similar.

Heres the code:

DateTime createOn = DateTime.Now;

string sSQLStatement = "INSERT INTO Index (Name, Description, Creator,CreatedOn) values ('" + name + "','" + description + "','" + userName + "','" + createOn + "')";

Any help would be much appreciated

If you are using SQL Server, change the statement to

INSERT INTO Index (Name, Description, Creator,CreatedOn) values ('" +name + "','" + description + "','" + userName + "',GetDate())

If you are using Access then use this:

INSERT INTO Index (Name, Description, Creator,CreatedOn) values ('" +name + "','" + description + "','" + userName + "',Date())

|||

Sorry, my fault i should have said, i'm coding in c sharp, heres the expanded function

void AddToQuizIndex(String userName,String quizName,String description,String question_xml)

{

DateTime createOn = DateTime.Now;

string sSQLStatement ="INSERT INTO QuizIndex (Name, Description,Creator,CreatedOn,Data) values ('" + quizName +"','" + description +"','" + userName +"','" +createOn+"','" + question_xml +"')";this.ActionSQLStatement(sSQLStatement);

}

|||

C# makes no difference. GetDate() in SQL Server will automatically apply the equivalent of C# datetime.now. But your database won't complain. Try it.

string sSQLStatement ="INSERT INTO QuizIndex (Name, Description,Creator,CreatedOn,Data) values ('" + quizName +"','" + description +"','" + userName +"',GetDate(),'" + question_xml +"')";

Really, you should be using parameters rather than compiling dynamic SQL statements, but that's another topic.

|||

nice one, first time i tried it i didn't put ' ' round the GetDate()

Thanks very much for the replyMikesdotnetting, you really helped me out.

Datetime to Time Only

Is it possible in SRS 2K to use a String.Format to change a field of
MM/DD/YYYY HH:MM:SS to display as HH:MM AM/PM only?
ThanksIt is possible. Just go to the properties of the field/ Textbox where you
want to be in time format and go to Format tab. and select format code and
click on "..." you can see time format just select it or it is basically "t"
format.
Amarnath
"lucotc" wrote:
> Is it possible in SRS 2K to use a String.Format to change a field of
> MM/DD/YYYY HH:MM:SS to display as HH:MM AM/PM only?
>
> Thanks

datetime to smalldatetime

I need to convert a datetime field to smalldatetime.

This particular field we only care about the time portion (an example would be '1899-12-30 13:15:00.000')

For now I created another field say 'newTime' that is smalldatetime, in which I want to "update" to the smalldatetime version of the data. I know this will truncate the ms, but I don't care about that. Also the min date that can be used with smalldatetime is Jan 1 1900.

Not sure how to go about doing this.

you could use the CONVERT function. check out BOL for CONVERT functions.

sample:

SELECTCONVERT(varchar,getdate(), 101)
|||

So lets say I have 2 fields, "oldTime" and "newTime"

oldTime is a datetime data type

newTime is a smalldatetime data type

I want to run a query like

update myTableset newTime = oldTimewhere ...etc...

I get this error

Msg 298, Level 16, State 1, Line 1

The conversion from datetime data type to smalldatetime data type resulted in a smalldatetime overflow error.

Then I ran:

update myTable
set newTime =
(SELECTCONVERT(varchar, oldTime, 101))

I get the error:

Msg 296, Level 16, State 3, Line 1

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

The statement has been terminated.

I'm sure this is because of the date portion in oldTime is < 1900

Would dateadd(dd, 1, oldTime) be be the best way to go about this?

|||

you are probably better off using a VARCHAR instead of smalldatetime. If you have to use smalldatetime, then you cannot put in values with YEAR < 1900. Or you could use one of the CONVERT functions to put only the time part.

for example:

DECLARE @.tsmalldatetime, @.t2datetimeSET @.t2 ='Jul 19 1800 1:14PM'SET @.t =convert(varchar,@.t2,114)PRINT @.t

Wednesday, March 21, 2012

datetime to date??

When I send a query 'SELECT somedatecolumn FROM...' I got resultset
containing datetime. It's ok because it is a datetime field (or
smalldatetime) but it is a little frustrating because I'm interested only in
date. Is it possible to get only a date, without the time? My previous
database was in Visual FoxPro and there I had date columns but on MSDE or
MSSQL it seems I only can have datetime or smalldatetime.
Thx.
On Sat, 29 Jan 2005 20:04:47 +0100, Rutko wrote:

>When I send a query 'SELECT somedatecolumn FROM...' I got resultset
>containing datetime. It's ok because it is a datetime field (or
>smalldatetime) but it is a little frustrating because I'm interested only in
>date. Is it possible to get only a date, without the time? My previous
>database was in Visual FoxPro and there I had date columns but on MSDE or
>MSSQL it seems I only can have datetime or smalldatetime.
>Thx.
>
Hi Rutko,
The short answer: no.
The long answer: http://www.karaszi.com/SQLServer/info_datetime.asp
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||See the DATEPART functions in the Books On Line.
Jim
"Rutko" <rutko22001@.yahoo.com> wrote in message
news:egcU0VjBFHA.1040@.TK2MSFTNGP09.phx.gbl...
> When I send a query 'SELECT somedatecolumn FROM...' I got resultset
> containing datetime. It's ok because it is a datetime field (or
> smalldatetime) but it is a little frustrating because I'm interested only
> in
> date. Is it possible to get only a date, without the time? My previous
> database was in Visual FoxPro and there I had date columns but on MSDE or
> MSSQL it seems I only can have datetime or smalldatetime.
> Thx.
>

DateTime string insert into sql datetime column fails

Iam trying to write to a DateTime field in MSSQL from wonderware
intouch. The problem is that I keep getting the error that the string
I'm using is not a valid datetime string....has anybody experienced
this and what was the workaround?
Thanks
GaryThis should arm you with enough information to understand why the operation
fails:
http://www.karaszi.com/SQLServer/info_datetime.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
<GaryCharlotte@.Charter.net> wrote in message
news:1145071019.588279.87720@.i39g2000cwa.googlegroups.com...
> Iam trying to write to a DateTime field in MSSQL from wonderware
> intouch. The problem is that I keep getting the error that the string
> I'm using is not a valid datetime string....has anybody experienced
> this and what was the workaround?
> Thanks
> Gary
>|||Thank you Tibor.
I have tried various combinations including the recommended on that
site ie '02/23/1998 14:23:05'
Still no joy. I wonder if this is a wonderware sqlinsert problem...|||> I have tried various combinations including the recommended on that
> site ie '02/23/1998 14:23:05'
That's not recommended, it will fail if, for example, your dateformat is
dmy.
What does "no joy" mean? Does it fail? With what error? Did you try a
safe standard format like
'19980223 14:23:05'
?|||OK I found out what the problem is. If you use a SQLInsertprepare and
SQLInsertexecute it fails no matter what format you use.
Used SQLConnect, SQLInsert and SQLDisconnect and it works great!
Thanks for the help Tiborsql

Datetime returns NULL from stored proc in VB

I execute a stored procedure in VB and get several results... the last one (a date time field) is always returned as NULL even though I can see in enterprise manager that it IS infact set... any ideas?How are you retrieving the results? In a recordset or as a command ouput parameter?|||I'm using ADODB.Recordset|||Huh. I think we're gonna need to see some code on this one.|||I am using a record set. Hopefully you are using ADO 2.7 or higher by now...|||I meant, it would be helpful to see the code you are using, to help troubleshoot the problem.

But, try inserting this after you retrieve your recordset:

Dim mField As Field
For Each mField In mRecordset.Fields
Debug.Print mField.Name & ": " & mField.Type
Next mField

(replacing mRecordset with your recordset name)

Just to see what datatype is being returned. This may help point to the problem.|||Thanks, I really appreciate it... I'll try it right now :)|||The value of the field type is 135 which is

Const adDBTimeStamp = 135 (&H87)|||Any one else got an idea?|||Is TimeStamp the field type in SQL, or is it supposed to be DateTime? They are not the same.|||It's a DateTime data type (just checked in EM)... I know they're not the same... I don't know why it's returning time stamp and why it's coming up NULL (I'm retrieving it into a string, then I tried a Date type)|||As a test, in your sp, try casting the column to be DATETIME in the output query. See if this changes the datatype in VB.|||I fixed it :-D

DateTime Question?

Hi All!

I have a SP that does a search in a table. I have a datetime field. But I would like to only search by date, not by datetime. I noticed it works only if the time is 12:00am. But if it's different, the date won't show. I do need to display the date and time.

here is my SP

PROCEDURE [dbo].[SearchHist]

-- Add the parameters for the stored procedure here

@.FName nvarchar(50),

@.LName nvarchar(50),

@.DOB nvarchar(50),

@.Acct nvarchar(50),

@.Login nvarchar(50),

@.Status nvarchar (50),

@.Rmark nvarchar(255),

@.Room nvarchar (50),

@.Age nvarchar(50),

@.Sdate datetime,

@.Type nvarchar(50),

@.Misc bit

AS

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

If LEN(@.FName)>0 OR LEN(@.LName)>0 OR LEN(@.DOB)>0 OR LEN(@.Acct)>0 OR

LEN (@.Rmark)>0 OR LEN (@.Room)>0 OR LEN (@.Age)>0 OR LEN(@.Sdate)>0

OR LEN (@.Type)>0 OR LEN (@.Misc)>0 OR (@.Login)>0

-- Insert statements for procedure here

SELECT Archive_Orders.First_Name, Archive_Orders.Last_Name, Archive_Orders.Account_Number, Archive_Orders.Remarks, Locations.Loct_Desc,

Archive_Orders.Rm_Desc, Archive_Orders.Age, Archive_Orders.Service_Date_Time, Archive_Orders.Type, Archive_Orders.Order_ID,

Order_Status.Status, Archive_Orders.Misc

FROM Archive_Orders INNER JOIN

Locations ON Archive_Orders.Location_ID = Locations.Location_ID INNER JOIN

Order_Status ON Archive_Orders.Status_ID = Order_Status.Status_ID

WHERE (Archive_Orders.First_Name = @.FName) OR

(Archive_Orders.Last_Name = @.Lname) OR

(Archive_Orders.DOB = @.DOB) OR

(Archive_Orders.Account_Number = @.Acct) OR

(Order_Status.Status = @.Status) OR

(Archive_Orders.Remarks = @.Rmark) OR

(Archive_Orders.Rm_Desc = @.Room) OR

(Archive_Orders.Age = @.Age) OR

(Archive_Orders.Service_Date_Time = @.SDate) OR

(Archive_Orders.Type = @.Type) OR

(Archive_Orders.Misc = @.Misc) OR

(Archive_Orders.Login = @.Login)

END

TIA!

Rudy

If you're looking to return Orders for a particular day, i tend to use DATEDIFF as its quick and simple.

eg

WHERE DATEDIFF(D, Archive_Orders.Service_Date_Time, @.SDate) = 0

HTH!

|||

There are multiple approaches to searching dates. You could for every row strip off the time value, using a construct like for example:

Code Snippet

convert( datetime, convert(varchar(10), your_date_col, 120), 120)

and then check for equality with the value you are looking for. This has a big disadvantage though when searching historical data which tend to contain many records: you're asking sql server to first convert every value in the table and then evaluate all of those converted values against your selection criterium, i.e. it will be slow.

The better approach is to select all datetime values within the range from your_date 0:00 and before your_date + 1 0:00. Thus your where clause will look something like:

Code Snippet

where your_datetime_col >= @.your_search_date

and your_datetime_col < dateadd( day, 1, @.your_search_date)

This way SQL can more easily answer your query.

You may also want to ensure in your sp (depending on your requirements) that the @.your_search_date value does not have a time component in it by adding something like this before your select statement:

Code Snippet

select @.your_search_date = convert( datetime, convert(varchar(10), @.your_search_date, 120), 120)

|||

Of the methods mentioned, I like this one (mentioned by rrozema) better:

Code Snippet

where your_datetime_col >= @.your_search_date

and your_datetime_col < dateadd( day, 1, @.your_search_date)

with this method your search can take advantage of a date index -- provided that one is available -- and use a seek. The other methods are likely to scan.

|||

Here's how I do a Date search for a SINGLE day. Let's assume you're looking for all records where occurs any time on August 9, 2007 (i.e. '8/9/2007 00:00:00' to '8/9/2007 23:59:59.999')

Code Snippet

declare @.TargetDay int

set @.TargetDay = datediff(d,0,'09 August 2007')

select *

from <MyTable> where DateDiff(d,0, <MyDate>) = @.TargetDay

|||

@.rusag2: Your solution will most likely do a scan too. i.e. If there are many of rows in the table, this is going to be very slow as SQL can not take advantage of indexes (given that any are available).

|||

Absolutely agreed. If the column is indexed, you might take advantage of that like this:

Code Snippet

declare @.dtStart datetime, @.dtEnd datetime

set @.dtStart = '09 Aug 2007'

set @.dtEnd = dateadd(d,1,@.dtSTart)

set @.dtEnd = dateadd(ms, -1, @.dtEnd)

select * from <MyTable> where <DateField> between @.dtStart and @.dtEnd

|||

There are a couple of other potholes that you need to be aware of. My understanding here is that we are in the context of a stored procedure and that the date is a parameter of the stored procedure. In the context of a stored procedure, a where clause such as this:

Code Snippet

where your_datetime_col >= @.your_search_date
and your_datetime_col < dateadd( day, 1, @.your_search_date)

has an advantage over a where clause in which you load values into a second variable like this:

Code Snippet

declare @.dtStart datetime, @.dtEnd datetime
set @.dtStart = '09 Aug 2007'
set @.dtEnd = dateadd(d,1,@.dtSTart)
set @.dtEnd = dateadd(ms, -1, @.dtEnd)
select * from where between @.dtStart and @.dtEnd

because the first where clause can take advantage of parameter sniffing and has a better chance of an index seek than the second query.

Also, the upper bound of the first where clause uses a "strictly less than" comparator whereas BETWEEN uses a "less than or equal to" comparator for the upper bound. To compensate you need to STILL include the less than operator like:

Code Snippet

select * from <MyTable> where <DateField> between @.dtStart and @.dtEnd

and <DateField> < @.dtEnd

|||

Thank you all!!

Great suggestions!!

(Archive_Orders.Service_Date_Time >= @.SDate

and Archive_Orders.Service_Date_Time < dateadd(day, 1, @.SDate))

This one worked very nicely. Thanks rrozema!!

Rudy

DateTime question

I'm trying to update a datetime field in my database with the current
date and time. The following command inserts the date as 1/13/06 when
most systems recognize 38728 as 1/11/06. Why does sql server 2005
increase this value by two days. The code that uses this database is
written in C# and makes heavy use of DateTime especially DateTime.Now,
which thinks 38728 is 1/11/06 so I'm not looking for a different way to
add this record, I want sql server to understand that 38728 is 1/11/06
and not 1/13/06.
Please help!
INSERT INTO [dbo].[Test]
([EID]
,[modifiedBy]
,[modifiedOn])
VALUES
(9999,
'TestHarness',
38728)What result does the following give you :-
select @.@.datefirst
Try SET @.@.DateFirst (two less than the result from above) before running
your insert
--
HTH. Ryan
"BetaD" <dhorth@.horth.com> wrote in message
news:1136994628.541049.108210@.g44g2000cwa.googlegroups.com...
> I'm trying to update a datetime field in my database with the current
> date and time. The following command inserts the date as 1/13/06 when
> most systems recognize 38728 as 1/11/06. Why does sql server 2005
> increase this value by two days. The code that uses this database is
> written in C# and makes heavy use of DateTime especially DateTime.Now,
> which thinks 38728 is 1/11/06 so I'm not looking for a different way to
> add this record, I want sql server to understand that 38728 is 1/11/06
> and not 1/13/06.
> Please help!
> INSERT INTO [dbo].[Test]
> ([EID]
> ,[modifiedBy]
> ,[modifiedOn])
> VALUES
> (9999,
> 'TestHarness',
> 38728)
>|||You don't use the product correctly. You express datetimes in SQL Server as a string, not as a
number. Unfortunately, SQL Server accepts a number (implicit datatype conversion) and thereby
exposes the internals of the product. And that happens to be different from some other systems, as
you have noticed. You cannot change the behavior in this regard. Check out:
http://www.karaszi.com/SQLServer/info_datetime.asp
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"BetaD" <dhorth@.horth.com> wrote in message
news:1136994628.541049.108210@.g44g2000cwa.googlegroups.com...
> I'm trying to update a datetime field in my database with the current
> date and time. The following command inserts the date as 1/13/06 when
> most systems recognize 38728 as 1/11/06. Why does sql server 2005
> increase this value by two days. The code that uses this database is
> written in C# and makes heavy use of DateTime especially DateTime.Now,
> which thinks 38728 is 1/11/06 so I'm not looking for a different way to
> add this record, I want sql server to understand that 38728 is 1/11/06
> and not 1/13/06.
> Please help!
> INSERT INTO [dbo].[Test]
> ([EID]
> ,[modifiedBy]
> ,[modifiedOn])
> VALUES
> (9999,
> 'TestHarness',
> 38728)
>|||Why not just take the c# datetime and convert to SqlDateTime and store that?
--
William Stacey [MVP]
"BetaD" <dhorth@.horth.com> wrote in message
news:1136994628.541049.108210@.g44g2000cwa.googlegroups.com...
> I'm trying to update a datetime field in my database with the current
> date and time. The following command inserts the date as 1/13/06 when
> most systems recognize 38728 as 1/11/06. Why does sql server 2005
> increase this value by two days. The code that uses this database is
> written in C# and makes heavy use of DateTime especially DateTime.Now,
> which thinks 38728 is 1/11/06 so I'm not looking for a different way to
> add this record, I want sql server to understand that 38728 is 1/11/06
> and not 1/13/06.
> Please help!
> INSERT INTO [dbo].[Test]
> ([EID]
> ,[modifiedBy]
> ,[modifiedOn])
> VALUES
> (9999,
> 'TestHarness',
> 38728)
>sql

DateTime question

I'm trying to update a datetime field in my database with the current
date and time. The following command inserts the date as 1/13/06 when
most systems recognize 38728 as 1/11/06. Why does sql server 2005
increase this value by two days. The code that uses this database is
written in C# and makes heavy use of DateTime especially DateTime.Now,
which thinks 38728 is 1/11/06 so I'm not looking for a different way to
add this record, I want sql server to understand that 38728 is 1/11/06
and not 1/13/06.
Please help!
INSERT INTO [dbo].[Test]
([EID]
,[modifiedBy]
,[modifiedOn])
VALUES
(9999,
'TestHarness',
38728)What result does the following give you :-
select @.@.datefirst
Try SET @.@.DateFirst (two less than the result from above) before running
your insert
HTH. Ryan
"BetaD" <dhorth@.horth.com> wrote in message
news:1136994628.541049.108210@.g44g2000cwa.googlegroups.com...
> I'm trying to update a datetime field in my database with the current
> date and time. The following command inserts the date as 1/13/06 when
> most systems recognize 38728 as 1/11/06. Why does sql server 2005
> increase this value by two days. The code that uses this database is
> written in C# and makes heavy use of DateTime especially DateTime.Now,
> which thinks 38728 is 1/11/06 so I'm not looking for a different way to
> add this record, I want sql server to understand that 38728 is 1/11/06
> and not 1/13/06.
> Please help!
> INSERT INTO [dbo].[Test]
> ([EID]
> ,[modifiedBy]
> ,[modifiedOn])
> VALUES
> (9999,
> 'TestHarness',
> 38728)
>|||You don't use the product correctly. You express datetimes in SQL Server as
a string, not as a
number. Unfortunately, SQL Server accepts a number (implicit datatype conver
sion) and thereby
exposes the internals of the product. And that happens to be different from
some other systems, as
you have noticed. You cannot change the behavior in this regard. Check out:
http://www.karaszi.com/SQLServer/info_datetime.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"BetaD" <dhorth@.horth.com> wrote in message
news:1136994628.541049.108210@.g44g2000cwa.googlegroups.com...
> I'm trying to update a datetime field in my database with the current
> date and time. The following command inserts the date as 1/13/06 when
> most systems recognize 38728 as 1/11/06. Why does sql server 2005
> increase this value by two days. The code that uses this database is
> written in C# and makes heavy use of DateTime especially DateTime.Now,
> which thinks 38728 is 1/11/06 so I'm not looking for a different way to
> add this record, I want sql server to understand that 38728 is 1/11/06
> and not 1/13/06.
> Please help!
> INSERT INTO [dbo].[Test]
> ([EID]
> ,[modifiedBy]
> ,[modifiedOn])
> VALUES
> (9999,
> 'TestHarness',
> 38728)
>|||Why not just take the c# datetime and convert to SqlDateTime and store that?
William Stacey [MVP]
"BetaD" <dhorth@.horth.com> wrote in message
news:1136994628.541049.108210@.g44g2000cwa.googlegroups.com...
> I'm trying to update a datetime field in my database with the current
> date and time. The following command inserts the date as 1/13/06 when
> most systems recognize 38728 as 1/11/06. Why does sql server 2005
> increase this value by two days. The code that uses this database is
> written in C# and makes heavy use of DateTime especially DateTime.Now,
> which thinks 38728 is 1/11/06 so I'm not looking for a different way to
> add this record, I want sql server to understand that 38728 is 1/11/06
> and not 1/13/06.
> Please help!
> INSERT INTO [dbo].[Test]
> ([EID]
> ,[modifiedBy]
> ,[modifiedOn])
> VALUES
> (9999,
> 'TestHarness',
> 38728)
>

DateTime question

I'm trying to update a datetime field in my database with the current
date and time. The following command inserts the date as 1/13/06 when
most systems recognize 38728 as 1/11/06. Why does sql server 2005
increase this value by two days. The code that uses this database is
written in C# and makes heavy use of DateTime especially DateTime.Now,
which thinks 38728 is 1/11/06 so I'm not looking for a different way to
add this record, I want sql server to understand that 38728 is 1/11/06
and not 1/13/06.
Please help!
INSERT INTO [dbo].[Test]
([EID]
,[modifiedBy]
,[modifiedOn])
VALUES
(9999,
'TestHarness',
38728)
What result does the following give you :-
select @.@.datefirst
Try SET @.@.DateFirst (two less than the result from above) before running
your insert
HTH. Ryan
"BetaD" <dhorth@.horth.com> wrote in message
news:1136994628.541049.108210@.g44g2000cwa.googlegr oups.com...
> I'm trying to update a datetime field in my database with the current
> date and time. The following command inserts the date as 1/13/06 when
> most systems recognize 38728 as 1/11/06. Why does sql server 2005
> increase this value by two days. The code that uses this database is
> written in C# and makes heavy use of DateTime especially DateTime.Now,
> which thinks 38728 is 1/11/06 so I'm not looking for a different way to
> add this record, I want sql server to understand that 38728 is 1/11/06
> and not 1/13/06.
> Please help!
> INSERT INTO [dbo].[Test]
> ([EID]
> ,[modifiedBy]
> ,[modifiedOn])
> VALUES
> (9999,
> 'TestHarness',
> 38728)
>
|||You don't use the product correctly. You express datetimes in SQL Server as a string, not as a
number. Unfortunately, SQL Server accepts a number (implicit datatype conversion) and thereby
exposes the internals of the product. And that happens to be different from some other systems, as
you have noticed. You cannot change the behavior in this regard. Check out:
http://www.karaszi.com/SQLServer/info_datetime.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"BetaD" <dhorth@.horth.com> wrote in message
news:1136994628.541049.108210@.g44g2000cwa.googlegr oups.com...
> I'm trying to update a datetime field in my database with the current
> date and time. The following command inserts the date as 1/13/06 when
> most systems recognize 38728 as 1/11/06. Why does sql server 2005
> increase this value by two days. The code that uses this database is
> written in C# and makes heavy use of DateTime especially DateTime.Now,
> which thinks 38728 is 1/11/06 so I'm not looking for a different way to
> add this record, I want sql server to understand that 38728 is 1/11/06
> and not 1/13/06.
> Please help!
> INSERT INTO [dbo].[Test]
> ([EID]
> ,[modifiedBy]
> ,[modifiedOn])
> VALUES
> (9999,
> 'TestHarness',
> 38728)
>
|||Why not just take the c# datetime and convert to SqlDateTime and store that?
William Stacey [MVP]
"BetaD" <dhorth@.horth.com> wrote in message
news:1136994628.541049.108210@.g44g2000cwa.googlegr oups.com...
> I'm trying to update a datetime field in my database with the current
> date and time. The following command inserts the date as 1/13/06 when
> most systems recognize 38728 as 1/11/06. Why does sql server 2005
> increase this value by two days. The code that uses this database is
> written in C# and makes heavy use of DateTime especially DateTime.Now,
> which thinks 38728 is 1/11/06 so I'm not looking for a different way to
> add this record, I want sql server to understand that 38728 is 1/11/06
> and not 1/13/06.
> Please help!
> INSERT INTO [dbo].[Test]
> ([EID]
> ,[modifiedBy]
> ,[modifiedOn])
> VALUES
> (9999,
> 'TestHarness',
> 38728)
>
sql

datetime query

my database contains 1 field with "datetime" in sql server
hw can i query the database to just compare the date section of that field ? and not the time
WHERE CONVERT(VARCHAR(10),datecolumn,101) >= '08/06/2005'
|||Although Dinakar's suggestion will work, it will not perform as well as something like this:
WHERE datecolumn >= '20050806' AND datecolumn < '20050807'
When you perform a function on a column (such as CONVERT) this willmake the query non-sargable and will therefore not take advantage of anindex on the column.
For more tips on performance, seeSQL Server Transact-SQL WHERE Clause.

datetime problem

my asp .net application is coming along nicely. however, i would like to record the users last login time. i have a datetime field, and when i insert or update using Now() as the data for the field, i simply get 1/1/1900 12:00:00 in the last_login field. Same thing using Today(). any suggestions? am i using the wrong data type? i need to be able to do date comparisons as i plan on connecting my site with a forum, and this would be the easiest way to determine if there were new posts from the user's perspective.

TIA

Use GetDate() in your SQL to input the current time.|||

Unless your business object has a say in what the date/time stamp value is, there is little reason to send it over the network both ways. Might as well have the database provide the value and hand it back to you.

|||

thanks mikesdotnetting! worked like a charm!

|||

Don't forget that getdate(0 and Now() work on different servers which can be in different time zones :)

DateTime parameter without time part

There is a post that tells how to do this:
>>Use =Today() and set the field format to d
but where do you set the field format of a parameter?
--
Michael White
Programmer/Analyst
Marion County, ORHave you tried inserting the parameter on the report and then right click
properties and set format?
Another option is using code file.
"Michael" <xxx.xxx.xxx> wrote in message
news:%23g16Kjn%23EHA.1392@.tk2msftngp13.phx.gbl...
> There is a post that tells how to do this:
>>Use =Today() and set the field format to d
> but where do you set the field format of a parameter?
> --
> Michael White
> Programmer/Analyst
> Marion County, OR
>|||I don't think you can set the format of a parameter past the fact that it is
a date, but the time does NOT have to be entered when using the parameter.
Some people choose to use the string data type instead of date...
--
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Michael" <xxx.xxx.xxx> wrote in message
news:%23g16Kjn%23EHA.1392@.tk2msftngp13.phx.gbl...
> There is a post that tells how to do this:
> >>Use =Today() and set the field format to d
> but where do you set the field format of a parameter?
> --
> Michael White
> Programmer/Analyst
> Marion County, OR
>|||Wayne described what I do. I create the parameter as a string. My stored
procedures then convert the string to datetime datatypes. If you don't do
somthing similar, the parameter fields on the report append hh:mm:ss to the
parameter value supplied.
Mardy
"Wayne Snyder" wrote:
> I don't think you can set the format of a parameter past the fact that it is
> a date, but the time does NOT have to be entered when using the parameter.
> Some people choose to use the string data type instead of date...
> --
> Wayne Snyder, MCDBA, SQL Server MVP
> Mariner, Charlotte, NC
> www.mariner-usa.com
> (Please respond only to the newsgroups.)
> I support the Professional Association of SQL Server (PASS) and it's
> community of SQL Server professionals.
> www.sqlpass.org
> "Michael" <xxx.xxx.xxx> wrote in message
> news:%23g16Kjn%23EHA.1392@.tk2msftngp13.phx.gbl...
> > There is a post that tells how to do this:
> >
> > >>Use =Today() and set the field format to d
> >
> > but where do you set the field format of a parameter?
> >
> > --
> > Michael White
> > Programmer/Analyst
> > Marion County, OR
> >
> >
>
>

Monday, March 19, 2012

Datetime Parameter Format

Hi,

I'm trying to test a stored procedure in VB and SQL Express 2005 - it has a smalldatetime field.


It works fine from the VB side using the data source Preview Data facility but when I try it in SQL Management Studio I get errors no matter what format I try!

I'm sure I'm missing something very simple - thanks in advance.

USE [Bookings]
GO

DECLARE @.return_value int

EXEC @.return_value = [dbo].[spAddReservation]
@.RES_TBL_ID = 1,
@.RES_TTL_ID = 1,
@.RES_Diner_Surname = N'Bloggs',
@.RES_Date = 28/05/2007 18:15:00,
@.RES_Meal_ID = 1,
@.RES_STA_ID = 1,
@.RES_OCC_ID = 1,
@.RES_STF_ID = 1

SELECT 'Return Value' = @.return_value

GO

The error this generates is
Msg 102, Level 15, State 1, Line 8 Incorrect syntax near '/'.

I've tried with quotes (single or double) but then I get a convert error!
You need to include your date info quotes
You could set current datetime format by SET DATEFORMAT statement.
Also you could use CONVERT function:
@.Res_date = convert(datetime, '28/05/2007 18:15:00', 131)

|||

You won't get datetime conversion errors if you were to use the ISO standard date format = year/month/day,

e.g., '2007/05/28 18:15:00'

|||

See function CONVERT in BOL. If you use styles 112 (ISO) or 126 (ISO8601), then SQL Server will interprete correctly datetime constants no matter the settings for LANGUAGE and DATEFORMAT.

> @.RES_Date = 28/05/2007 18:15:00

@.RES_Date = '2007-05-28 18:15:00'

AMB

|||

I hope the error you got because of the missing quote..

When I check the dateformat it is DMY. (it may cause /throw another error if the system dateformat is different).

It is not bad idea to use the DATEFORMAT if the entier db uses the single format.

Try the following code..

Code Snippet

SET DATEFORMAT DMY;

USE [Bookings]

GO

DECLARE@.return_value int

EXEC@.return_value = [dbo].[spAddReservation]

@.RES_TBL_ID = 1,

@.RES_TTL_ID = 1,

@.RES_Diner_Surname = N'Bloggs',

@.RES_Date = '28/05/2007 18:15:00',

@.RES_Meal_ID = 1,

@.RES_STA_ID = 1,

@.RES_OCC_ID = 1,

@.RES_STF_ID = 1

SELECT'Return Value' = @.return_value

|||Thanks for this - worked a treat!

I had assumed the date format would be as per culture setting and was misled by the fact that that worked OK inside VB Express.