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

Thursday, March 29, 2012

convert adDBtimeStamp to datetime

Hello,
I use a SQL Server with ODBC driver and >NET C#.
I want to convert a date field with adDBtimeStamp format to a DATETIME
format, but no solution could be found till now.
My query is as follows:
SELECT received_at as EXPR1
FROM TTable
WHERE datepart(w(datetime(received_at)))>10
Has anybody any solution for conversion?
*** Sent via Developersdex http://www.examnotes.net ***What is the SQL datatype of "received_at" and what are you attepting to
do with it? adDBtimeStamp maps to a DATETIME type in SQL Server so no
conversion should be necessary. However your example code isn't valid
in SQL Server - there is no WEEK or DATETIME function and your syntax
for DATEPART is wrong.
Maybe the following was what you intended, assuming you are in fact
dealing with a DATETIME column:
SELECT received_at AS expr1
FROM TTable
WHERE DATEPART(WEEK,received_at)>10 ;
David Portas
SQL Server MVP
--|||
It seems that no conversion is made and no explicit conversion could be
applied.
for the sequence:
SELECT received_at AS expr1
FROM TTable
WHERE DATEPART(WEEK,received_at)>10
the outcome is:
Driver]Expected lexical element not found: )
*** Sent via Developersdex http://www.examnotes.net ***|||Lucian,
If the adDBtimeStamp values are seen as strings of the
form 'yyyymmddhhmmss', try this:
declare @.t table (
adDBtimeStamp char(14)
)
insert into @.t values ('20051012171534')
select
convert(datetime,
substring(adDBtimeStamp,1,8) + space(1) +
substring(adDBtimeStamp,9,2) + ':' +
substring(adDBtimeStamp,11,2) + ':' +
substring(adDBtimeStamp,12,2),
112) as SQLdt
from @.t
Steve Kass
Drew University
Lucian Baltes wrote:

>Hello,
>I use a SQL Server with ODBC driver and >NET C#.
>I want to convert a date field with adDBtimeStamp format to a DATETIME
>format, but no solution could be found till now.
>My query is as follows:
>SELECT received_at as EXPR1
>FROM TTable
>WHERE datepart(w(datetime(received_at)))>10
>Has anybody any solution for conversion?
>
>
>*** Sent via Developersdex http://www.examnotes.net ***
>

Tuesday, March 27, 2012

convert a datetime

If I issue:
SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS bigint) AS
Expr1
I get 36653 as the result.
If I issue:
SELECT CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS bigint) AS
Expr1
I get the same result even though the times differ by one minute.
Can anyone explain what is going on here and how I get unique integral
values from a conversion of a datetime value?
Many thanks.
Hi, Andrew
If you convert a datetime to a number, you get the number of days
elapsed from Jan 1, 1900, with a fractional portion representing the
time. For example, you will get different results from these queries:
SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS float)
SELECT CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS float)
You will also get different results if you do this:
SELECT CAST(CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS
binary(8)) AS bigint)
SELECT CAST(CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS
binary(8)) AS bigint)
For an explanation, here is a quote from Books Online:
Values with the datetime data type are stored internally by
Microsoft SQL Server as two 4-byte integers. The first 4 bytes store
the number of days before or after the base date, January 1, 1900.
The base date is the system reference date. Values for datetime
earlier than January 1, 1753, are not permitted. The other 4 bytes
store the time of day represented as the number of milliseconds
after midnight.
The smalldatetime data type stores dates and times of day with less
precision than datetime. SQL Server stores smalldatetime values as
two 2-byte integers. The first 2 bytes store the number of days
after January 1, 1900. The other 2 bytes store the number of minutes
since midnight. Dates range from January 1, 1900, through
June 6, 2079, with accuracy to the minute.
However, you should not rely on a physical implementation of a data
type. Why do you want a number instead of a datetime value ?
Razvan
|||Razvan Socol wrote:

>For an explanation, here is a quote from Books Online:
> Values with the datetime data type are stored internally by
> Microsoft SQL Server as two 4-byte integers. The first 4 bytes store
> the number of days before or after the base date, January 1, 1900.
> The base date is the system reference date. Values for datetime
> earlier than January 1, 1753, are not permitted. The other 4 bytes
> store the time of day represented as the number of milliseconds
> after midnight.
>
>
Just for the record, this documentation is incorrect. The "other 4
bytes" represent
the number of 1/300-second intervals after midnight, not the number of
milliseconds.
Steve Kass
Drew University

>Razvan
>
>
|||Thanks for the detailed explanation. I had read the BO section but assumed
cast/convert would account for both bytes when converting to bigint.
However, it obviously ignores the lower byte.
My objective is to subtract two datetimes in order to report an elapsed
time. Is there a better way to do this?
Regards,
Andrew
"Razvan Socol" <rsocol@.gmail.com> wrote in message
news:1133679477.157752.260590@.g14g2000cwa.googlegr oups.com...
> Hi, Andrew
> If you convert a datetime to a number, you get the number of days
> elapsed from Jan 1, 1900, with a fractional portion representing the
> time. For example, you will get different results from these queries:
> SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS float)
> SELECT CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS float)
> You will also get different results if you do this:
> SELECT CAST(CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS
> binary(8)) AS bigint)
> SELECT CAST(CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS
> binary(8)) AS bigint)
> For an explanation, here is a quote from Books Online:
> Values with the datetime data type are stored internally by
> Microsoft SQL Server as two 4-byte integers. The first 4 bytes store
> the number of days before or after the base date, January 1, 1900.
> The base date is the system reference date. Values for datetime
> earlier than January 1, 1753, are not permitted. The other 4 bytes
> store the time of day represented as the number of milliseconds
> after midnight.
> The smalldatetime data type stores dates and times of day with less
> precision than datetime. SQL Server stores smalldatetime values as
> two 2-byte integers. The first 2 bytes store the number of days
> after January 1, 1900. The other 2 bytes store the number of minutes
> since midnight. Dates range from January 1, 1900, through
> June 6, 2079, with accuracy to the minute.
> However, you should not rely on a physical implementation of a data
> type. Why do you want a number instead of a datetime value ?
> Razvan
>
|||> My objective is to subtract two datetimes in order to report an elapsed
> time. Is there a better way to do this?
Try DATEDIFF. You can specify a datepart appropriate for the resolution you
need.
SELECT DATEDIFF(ms, '20000508 12:35:29.998', '20000508 12:36:29.998') AS
ElapsedMilliseconds
Hope this helps.
Dan Guzman
SQL Server MVP
"Andrew Chalk" <achalk@.magnacartasoftware.com> wrote in message
news:%235KvzjO%23FHA.1168@.TK2MSFTNGP10.phx.gbl...
> Thanks for the detailed explanation. I had read the BO section but assumed
> cast/convert would account for both bytes when converting to bigint.
> However, it obviously ignores the lower byte.
> My objective is to subtract two datetimes in order to report an elapsed
> time. Is there a better way to do this?
> Regards,
> Andrew
> "Razvan Socol" <rsocol@.gmail.com> wrote in message
> news:1133679477.157752.260590@.g14g2000cwa.googlegr oups.com...
>
|||Exactly what I needed. Many thanks!
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:O90$$5P%23FHA.2832@.TK2MSFTNGP14.phx.gbl...
> Try DATEDIFF. You can specify a datepart appropriate for the resolution
> you need.
> SELECT DATEDIFF(ms, '20000508 12:35:29.998', '20000508 12:36:29.998') AS
> ElapsedMilliseconds
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Andrew Chalk" <achalk@.magnacartasoftware.com> wrote in message
> news:%235KvzjO%23FHA.1168@.TK2MSFTNGP10.phx.gbl...
>
|||Hi Andrew,
While converting datetime to numeric value, time part is returned in decimal
values. Try returning the value in Float instead of BigInt as below
SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS float) AS Expr1
--36652.52534718364
SELECT CAST(CAST('2000-05-08 12:37:29.998' AS datetime) AS float) AS Expr1
--36652.526041628087
Ok.
Deepak Sant
"Andrew Chalk" wrote:

> If I issue:
> SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS bigint) AS
> Expr1
> I get 36653 as the result.
> If I issue:
> SELECT CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS bigint) AS
> Expr1
> I get the same result even though the times differ by one minute.
> Can anyone explain what is going on here and how I get unique integral
> values from a conversion of a datetime value?
> Many thanks.
>
>
|||Thats wacky, because this will lead to the misunderstandable values,
due to the imprecision of datetime:
SELECT CAST(CAST('2000-05-08 12:36:29.995' AS datetime) AS float)
AS Expr1
--36652.52534718364
SELECT CAST(CAST('2000-05-08 12:36:29.996' AS datetime) AS float)
AS Expr1
--36652.52534718364
SELECT CAST(CAST('2000-05-08 12:36:29.997' AS datetime) AS float)
AS Expr1
--36652.52534718364
SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS float)
AS Expr1
--36652.52534718364
So you gotta be careful,
HTH, Jens Suessmeyer.
|||Hi Andrew,
While converting datetime to numeric if time part is included it always
returns in decimal values, so try for Float instead of Bigint datatype
SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS float) AS Expr1
SELECT CAST(CAST('2000-05-08 12:37:29.998' AS datetime) AS float) AS Expr1
Deepak Sant
"Andrew Chalk" wrote:

> If I issue:
> SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS bigint) AS
> Expr1
> I get 36653 as the result.
> If I issue:
> SELECT CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS bigint) AS
> Expr1
> I get the same result even though the times differ by one minute.
> Can anyone explain what is going on here and how I get unique integral
> values from a conversion of a datetime value?
> Many thanks.
>
>

convert a datetime

If I issue:
SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS bigint) AS
Expr1
I get 36653 as the result.
If I issue:
SELECT CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS bigint) AS
Expr1
I get the same result even though the times differ by one minute.
Can anyone explain what is going on here and how I get unique integral
values from a conversion of a datetime value?
Many thanks.Hi, Andrew
If you convert a datetime to a number, you get the number of days
elapsed from Jan 1, 1900, with a fractional portion representing the
time. For example, you will get different results from these queries:
SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS float)
SELECT CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS float)
You will also get different results if you do this:
SELECT CAST(CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS
binary(8)) AS bigint)
SELECT CAST(CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS
binary(8)) AS bigint)
For an explanation, here is a quote from Books Online:
Values with the datetime data type are stored internally by
Microsoft SQL Server as two 4-byte integers. The first 4 bytes store
the number of days before or after the base date, January 1, 1900.
The base date is the system reference date. Values for datetime
earlier than January 1, 1753, are not permitted. The other 4 bytes
store the time of day represented as the number of milliseconds
after midnight.
The smalldatetime data type stores dates and times of day with less
precision than datetime. SQL Server stores smalldatetime values as
two 2-byte integers. The first 2 bytes store the number of days
after January 1, 1900. The other 2 bytes store the number of minutes
since midnight. Dates range from January 1, 1900, through
June 6, 2079, with accuracy to the minute.
However, you should not rely on a physical implementation of a data
type. Why do you want a number instead of a datetime value ?
Razvan|||
Razvan Socol wrote:

>For an explanation, here is a quote from Books Online:
> Values with the datetime data type are stored internally by
> Microsoft SQL Server as two 4-byte integers. The first 4 bytes store
> the number of days before or after the base date, January 1, 1900.
> The base date is the system reference date. Values for datetime
> earlier than January 1, 1753, are not permitted. The other 4 bytes
> store the time of day represented as the number of milliseconds
> after midnight.
>
>
Just for the record, this documentation is incorrect. The "other 4
bytes" represent
the number of 1/300-second intervals after midnight, not the number of
milliseconds.
Steve Kass
Drew University

>Razvan
>
>|||Thanks for the detailed explanation. I had read the BO section but assumed
cast/convert would account for both bytes when converting to bigint.
However, it obviously ignores the lower byte.
My objective is to subtract two datetimes in order to report an elapsed
time. Is there a better way to do this?
Regards,
Andrew
"Razvan Socol" <rsocol@.gmail.com> wrote in message
news:1133679477.157752.260590@.g14g2000cwa.googlegroups.com...
> Hi, Andrew
> If you convert a datetime to a number, you get the number of days
> elapsed from Jan 1, 1900, with a fractional portion representing the
> time. For example, you will get different results from these queries:
> SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS float)
> SELECT CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS float)
> You will also get different results if you do this:
> SELECT CAST(CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS
> binary(8)) AS bigint)
> SELECT CAST(CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS
> binary(8)) AS bigint)
> For an explanation, here is a quote from Books Online:
> Values with the datetime data type are stored internally by
> Microsoft SQL Server as two 4-byte integers. The first 4 bytes store
> the number of days before or after the base date, January 1, 1900.
> The base date is the system reference date. Values for datetime
> earlier than January 1, 1753, are not permitted. The other 4 bytes
> store the time of day represented as the number of milliseconds
> after midnight.
> The smalldatetime data type stores dates and times of day with less
> precision than datetime. SQL Server stores smalldatetime values as
> two 2-byte integers. The first 2 bytes store the number of days
> after January 1, 1900. The other 2 bytes store the number of minutes
> since midnight. Dates range from January 1, 1900, through
> June 6, 2079, with accuracy to the minute.
> However, you should not rely on a physical implementation of a data
> type. Why do you want a number instead of a datetime value ?
> Razvan
>|||> My objective is to subtract two datetimes in order to report an elapsed
> time. Is there a better way to do this?
Try DATEDIFF. You can specify a datepart appropriate for the resolution you
need.
SELECT DATEDIFF(ms, '20000508 12:35:29.998', '20000508 12:36:29.998') AS
ElapsedMilliseconds
Hope this helps.
Dan Guzman
SQL Server MVP
"Andrew Chalk" <achalk@.magnacartasoftware.com> wrote in message
news:%235KvzjO%23FHA.1168@.TK2MSFTNGP10.phx.gbl...
> Thanks for the detailed explanation. I had read the BO section but assumed
> cast/convert would account for both bytes when converting to bigint.
> However, it obviously ignores the lower byte.
> My objective is to subtract two datetimes in order to report an elapsed
> time. Is there a better way to do this?
> Regards,
> Andrew
> "Razvan Socol" <rsocol@.gmail.com> wrote in message
> news:1133679477.157752.260590@.g14g2000cwa.googlegroups.com...
>|||Exactly what I needed. Many thanks!
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:O90$$5P%23FHA.2832@.TK2MSFTNGP14.phx.gbl...
> Try DATEDIFF. You can specify a datepart appropriate for the resolution
> you need.
> SELECT DATEDIFF(ms, '20000508 12:35:29.998', '20000508 12:36:29.998') AS
> ElapsedMilliseconds
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Andrew Chalk" <achalk@.magnacartasoftware.com> wrote in message
> news:%235KvzjO%23FHA.1168@.TK2MSFTNGP10.phx.gbl...
>|||Hi Andrew,
While converting datetime to numeric value, time part is returned in decimal
values. Try returning the value in Float instead of BigInt as below
SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS float) AS Exp
r1
--36652.52534718364
SELECT CAST(CAST('2000-05-08 12:37:29.998' AS datetime) AS float) AS Exp
r1
--36652.526041628087
Ok.
Deepak Sant
"Andrew Chalk" wrote:

> If I issue:
> SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS bigint) AS
> Expr1
> I get 36653 as the result.
> If I issue:
> SELECT CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS bigint) AS
> Expr1
> I get the same result even though the times differ by one minute.
> Can anyone explain what is going on here and how I get unique integral
> values from a conversion of a datetime value?
> Many thanks.
>
>|||Thats wacky, because this will lead to the misunderstandable values,
due to the imprecision of datetime:
SELECT CAST(CAST('2000-05-08 12:36:29.995' AS datetime) AS float)
AS Expr1
--36652.52534718364
SELECT CAST(CAST('2000-05-08 12:36:29.996' AS datetime) AS float)
AS Expr1
--36652.52534718364
SELECT CAST(CAST('2000-05-08 12:36:29.997' AS datetime) AS float)
AS Expr1
--36652.52534718364
SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS float)
AS Expr1
--36652.52534718364
So you gotta be careful,
HTH, Jens Suessmeyer.|||Hi Andrew,
While converting datetime to numeric if time part is included it always
returns in decimal values, so try for Float instead of Bigint datatype
SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS float) AS Exp
r1
SELECT CAST(CAST('2000-05-08 12:37:29.998' AS datetime) AS float) AS Exp
r1
Deepak Sant
"Andrew Chalk" wrote:

> If I issue:
> SELECT CAST(CAST('2000-05-08 12:36:29.998' AS datetime) AS bigint) AS
> Expr1
> I get 36653 as the result.
> If I issue:
> SELECT CAST(CAST('2000-05-08 12:35:29.998' AS datetime) AS bigint) AS
> Expr1
> I get the same result even though the times differ by one minute.
> Can anyone explain what is going on here and how I get unique integral
> values from a conversion of a datetime value?
> Many thanks.
>
>

convert a date stored as a string into a datetime

Hello forum,

Is it possible to convert a date stored as a string into a datetime with integration services 2005? My attempts with the “data conversion” fail. The string type form of the date is ‘yyyy-mm-dd’ and the desired result for use in a Union All is ‘dd/mm/yyyy 12:00:00AM.’This outcome is needs so that match on the date can populate a fact table, as the results are coming from two different databases.

All advice/help welcomed.

Ian

Use the Derived Column transform, and add this expression:

Code Snippet

(DT_DATE)((SUBSTRING(StringDate,6,2) + "-" + SUBSTRING(StringDate,9,2) + "-" + SUBSTRING(StringDate,1,4)))

Tip: Because there is no domain integrity inherent in the string date format, be certain to include an error output on your Derived Column transform.

|||

Use a dervide column with substring to re-order the date format; at the end cast it as date:

Code Snippet

(DT_DATE)(SUBSTRING(StrDAte,9,2) + "/" + SUBSTRING(StrDAte,6,2) + "/" + SUBSTRING(StrDAte,1,4))

|||Since this is a common topic today, I blogged on it, with a little more detail than what is posted here: http://bi-polar23.blogspot.com/2007/05/having-trouble-getting-date.htmlsqlsql

convert a datatype

i have table tt.
it contains two fields one is ttno int,doj datetime . i want to
convert to datetime to varchar .
how it is ... give me some examplesNo problem, look at the CONVERT function for a specific format you want
to extract. I prefer using the ISO one, though its good for ordering
and international.

CONVERT(VARCHAR(10),GETDATE(),112)

HTH, jens Suessmeyer.

--
http://www.sqlserver2005.de
--|||Check the topic CONVERT in SQL Server Books Online.

--
Anith|||Anith Sen (anith@.bizdatasolutions.com) writes:
> Check the topic CONVERT in SQL Server Books Online.

Actually, the topic is CAST and CONVERT, lest someone looks in the wrong
place and don't find it.

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

Convert 1084313300 (Ten Digit) value to Datetime

I have a field in a table that contains ten digit value representing a datetime. Is there any way to convert it to default datetime format
Thanks
Message posted via http://www.sqlmonster.com
Imran Irfan via SQLMonster.com wrote:
> I have a field in a table that contains ten digit value representing
> a datetime. Is there any way to convert it to default datetime format
> Thanks
Please don't multipost. Cross-post the message to multiple groups
instead if you think multiple groups are necessary. See my repsonse in
"Clients"
David
sqlsql

Convert 1084313300 (Ten Digit) value to Datetime

I have a field in a table that contains ten digit value representing a datetime. Is there any way to convert it to default datetime format
Thanks

--
Message posted via http://www.sqlmonster.comHi,
Whats the column data type. is it int or varchar?? what's the date that
reprasenting 1084313300 ??

Cheers
Dishan|||On Thu, 23 Dec 2004 02:04:02 GMT, Imran Irfan via SQLMonster.com wrote:

> I have a field in a table that contains ten digit value representing a datetime. Is there any way to convert it to default datetime format
> Thanks

I'm guessing that it represents a unix time. If that's true, you want

select dateadd(second,1084313300,'1970-01-01')
which returns
2004-05-11 22:08:20.000

You may want or need timezone corrections as well...

Convert 1084313300 (Ten Digit) value to Datetime

I have a field in a table that contains ten digit value representing a datetime. Is there any way to convert it to default datetime format.
Plese Help me
Thanks
Message posted via http://www.sqlmonster.com
Imran Irfan via SQLMonster.com wrote:
> I have a field in a table that contains ten digit value representing
> a datetime. Is there any way to convert it to default datetime
> format. Plese Help me
> Thanks
We would need to know what format the 10 digit value was using in order
to convert it. Do you know what it is using? How do you currently
interpret the value in your application? i.e. is there an algorithm in
the application that clearly shows what's being done at the application
level?
David Gugick
Imceda Software
www.imceda.com
|||I assume that is a UNIX timestamp (seconds since 01/01/1970):
http://www.aspfaq.com/show.asp?id=2451
Jacco Schalkwijk
SQL Server MVP
"Imran Irfan via SQLMonster.com" <forum@.SQLMonster.com> wrote in message
news:b4d9fd153fd8404c87ca2328c54bcbd9@.SQLMonster.c om...
>I have a field in a table that contains ten digit value representing a
>datetime. Is there any way to convert it to default datetime format.
> Plese Help me
> Thanks
> --
> Message posted via http://www.sqlmonster.com

convert / group by date

Hi,
I have a datetime column named dtDateTime.
its format is "Oct 27 2006 12:00:00 "
I want to group by only date part of it and count

my code is

$sql1="SELECT convert(varchar,J1708Data.dtDateTime,120),
count(convert(varchar,J1708Data.dtDateTime,120))

FROM Vehicle INNER JOIN J1708Data ON Vehicle.iID = J1708Data.iVehicleId

WHERE (J1708Data.iPidId = 303) AND
(J1708Date.dtDateTime between '2006-10-25' AND '2006-10-28')
AND (Vehicle.sDescription = $VehicleID)

GROUP BY convert(varchar,J1708Data.dtDateTime,120)";

However, convert part, group by part doesnt' work at all.
(i couldn't check count part)

can you find where's the problem?
Thx.kirke wrote:

Quote:

Originally Posted by

Hi,
I have a datetime column named dtDateTime.
its format is "Oct 27 2006 12:00:00 "
I want to group by only date part of it and count
>
my code is
>
>
$sql1="SELECT convert(varchar,J1708Data.dtDateTime,120),
count(convert(varchar,J1708Data.dtDateTime,120))
>
FROM Vehicle INNER JOIN J1708Data ON Vehicle.iID = J1708Data.iVehicleId
>
WHERE (J1708Data.iPidId = 303) AND
(J1708Date.dtDateTime between '2006-10-25' AND '2006-10-28')
AND (Vehicle.sDescription = $VehicleID)
>
GROUP BY convert(varchar,J1708Data.dtDateTime,120)";
>
>
However, convert part, group by part doesnt' work at all.
(i couldn't check count part)
>
can you find where's the problem?


--BEGIN PGP SIGNED MESSAGE--
Hash: SHA1

I'd have done it like this (use VARCHAR(10) or CHAR(10) for the date
instead of an unspecified size):

SELECT CONVERT(VARCHAR(10),J.dtDateTime,120) As theDate,
COUNT(*) As theCount
FROM Vehicle As V INNER JOIN J1708Data As J
ON V.iID = J.iVehicleId
WHERE J.iPidId = 303
AND J.dtDateTime BETWEEN '2006-10-25' AND '2006-10-28 23:23:59'
AND V.sDescription = @.VehicleID
GROUP BY CONVERT(VARCHAR(10),J.dtDateTime,120)
--
MGFoster:::mgf00 <atearthlink <decimal-pointnet
Oakland, CA (USA)
** Respond only to this newsgroup. I DO NOT respond to emails **

--BEGIN PGP SIGNATURE--
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBRUrBwIechKqOuFEgEQKUTQCg1zGcAeAViDrJQWxENdcn2t xbhxYAoO4o
1Mks6W+FiXviMMrZi/lt4e3z
=vWR9
--END PGP SIGNATURE--|||On 2 Nov 2006 10:53:08 -0800, kirke wrote:

Quote:

Originally Posted by

>Hi,
>I have a datetime column named dtDateTime.
>its format is "Oct 27 2006 12:00:00 "
>I want to group by only date part of it and count
>
>my code is
>
>
>$sql1="SELECT convert(varchar,J1708Data.dtDateTime,120),
>count(convert(varchar,J1708Data.dtDateTime,120))
>
>FROM Vehicle INNER JOIN J1708Data ON Vehicle.iID = J1708Data.iVehicleId
>
>WHERE (J1708Data.iPidId = 303) AND
>(J1708Date.dtDateTime between '2006-10-25' AND '2006-10-28')
>AND (Vehicle.sDescription = $VehicleID)
>
>GROUP BY convert(varchar,J1708Data.dtDateTime,120)";
>
>
>However, convert part, group by part doesnt' work at all.
>(i couldn't check count part)
>
>can you find where's the problem?
>Thx.


Hi kirke,

Have you tried to run the query? If so, what were the results? Were they
incoorrect, or did you get an error message. If the latter, then what
was that message?

I don't see any real problems with your data, thoough I would change a
few things:

* The date format. yyyy-mm-dd is not safe, becuase it can be interpreted
as yyyy-dd-mm for some country settings. Remve the dashes to get the
unambiguous yyyymmdd format.

* The use of BETWEEN means that rows with a startdate of 28th oct 2006
at exactly midnight will be included, but startdates on the same day
with a later time are excluded. The solution MGFoster proposes for this
(to include a time portion of 23:59:59) is not good enough - for
smalldatetime, this will be rounded up to the next minute, which is
midnight of the 29th of october; for datetime, you'll still miss rows
with a startdate in the last second of the day. You should replace
BETWEEN with a >= and a < condition:
AND J1708Date.dtDateTime >= '20061025'
AND J1708Date.dtDateTime < '20061029' -- Note the increased end day!
If you store all dates with the default time component of midnight, then
this is not necessary - but since it doesn't hurt either, I'd advice you
to accustom yourself to always using this techniques when comparing
datetimes.

The expression GROUP BY convert(varchar,J1708Data.dtDateTime,120) won't
group by daym, since the conversion doesn't chop off the time portion.
The result of select convert(varchar, current_timestamp, 120) for
instance is "2006-11-03 23:18:22", so you end up grouping by second.

Here's what I would try:

SELECT convert(varchar,J1708Data.dtDateTime,120),
count(convert(varchar,J1708Data.dtDateTime,120))
SELECT DATEADD(day, DATEDIFF(day, 0, d.DateTime), 0) AS TheDate,
COUNT(*) AS TheCount
FROM Vehicle AS v
INNER JOIN J1708Data AS d
ON v.VehicleID = d.VehicleId
WHERE d.PidId = 303
AND d.DateTime >= '20061025'
AND d.DateTime < '20061029'
AND v.Description = $VehicleID
GROUP BY DATEDIFF(day, 0, d.DateTime);

--
Hugo Kornelis, SQL Server MVPsqlsql

Conversion query

I have 2 date variables passed into my store procedure as follows
@.StartDate datetime,
@.EndDate datetime
however I am getting a conversion error when running the command below which
says "Syntax error converting datetime from character string"
PRINT ('INSERT INTO ' + @.NewSubsList + '(SubRef)
SELECT DISTINCT SubRef
FROM Subscriptions
WHERE (PubCode = ' + @.PubCode + ') AND DateEntered >= ' + @.StartDate +
') AND (DateEntered <= ' + @.EndDate + ')')
Any suggestions would be welcome.
ThanksYou have to explictly cast the datetime values to characters like so
PRINT ('INSERT INTO ' + @.NewSubsList + '(SubRef)
SELECT DISTINCT SubRef
FROM Subscriptions
WHERE (PubCode = ' + @.PubCode + ') AND DateEntered >= '
+ Cast(@.StartDate As VarChar(20))
+ ') AND (DateEntered <= ' + Cast(@.EndDate As VarChar(20)) + ')')
Thomas
"Pete" <Pete@.discussions.microsoft.com> wrote in message
news:E78EE524-27E2-47AF-96E4-60B250AFA884@.microsoft.com...
>I have 2 date variables passed into my store procedure as follows
> @.StartDate datetime,
> @.EndDate datetime
> however I am getting a conversion error when running the command below whi
ch
> says "Syntax error converting datetime from character string"
> PRINT ('INSERT INTO ' + @.NewSubsList + '(SubRef)
> SELECT DISTINCT SubRef
> FROM Subscriptions
> WHERE (PubCode = ' + @.PubCode + ') AND DateEntered >= ' + @.StartDate +
> ') AND (DateEntered <= ' + @.EndDate + ')')
> Any suggestions would be welcome.
> Thanks

Conversion problems between mssql and access

Hello

When I trying to insert data with datatype datetime or smalldatetime from SQL Server into a table in a linked access database I get this error :

Server: Msg 257, Level 16, State 3, Line 1
Implicit conversion from data type smalldatetime to float is not allowed. Use the CONVERT function to run this query.

I dont understand why it try to insert it as a float?!Because from a SQL Server point of view DateTime values ARE float values.

So you cannot do an implict cast but you have to do explictly by using the T-SQL statement "CONVERT"

Look in Books On Line for better help on conversions.|||Hello

I have tried CONVERT. But I dont know to which datatype I should convert my source value.
I have converted to varchar and nvarchar but I still got the same error.|||can you post your code, and specify from what kind of data type you what convert to?|||INSERT INTO LINKEDACCESS...ContactTarget
Select ChangedBy, ChangedDate, PersonIdNo,IdNo,TargetCode,TargetCodeproductCode,T argetCodePotentialCode
from tblContactTarget
WHERE Id NOT IN(select Id from LINKEDACCESS...ContactTarget CT
WHERE CT.Target = tblContactTarget.TargetCode)

ChangedDate is of datatype Datetime in SQL and Date/Time in Access.
This INSERT will trigger the error I wrote about.|||Try to convert it to a varchar. use the CONVERT function so that you can even specify the dateformat you need to have

Thursday, March 22, 2012

CONVERSION FROM CHAR(4) TO DATETIME

i have another problem.and it's now on converting a char(4) to datetime
here is the situation
J_TIM < F_TIM

J_TIM is datetime while F_TIM is char of 4

example

J_TIM = 20:30
F_TIM = 2030

how can i convert F_TIM to datetime so that i can compare them.
?

thanksYou can try something like:

select *
from sometable
where convert(datetime, '20060101 ' + J_TIM) <
convert(datetime,'20060101 ' + substring(F_TIM,1,2) + ':' +
substring(F_TIM,3,2))

paul_zaoldyeck wrote:

Quote:

Originally Posted by

i have another problem.and it's now on converting a char(4) to datetime
here is the situation
J_TIM < F_TIM
>
J_TIM is datetime while F_TIM is char of 4
>
example
>
J_TIM = 20:30
F_TIM = 2030
>
how can i convert F_TIM to datetime so that i can compare them.
?
>
thanks

|||On 22 Nov 2006 22:46:19 -0800, paul_zaoldyeck wrote:

Quote:

Originally Posted by

>i have another problem.and it's now on converting a char(4) to datetime
>here is the situation
>J_TIM < F_TIM
>
>J_TIM is datetime while F_TIM is char of 4
>
>example
>
>J_TIM = 20:30
>F_TIM = 2030
>
>how can i convert F_TIM to datetime so that i can compare them.
>?
>
>thanks


Hi Paul,

DECLARE @.F_TIM char(4);
SET @.F_TIM = '2030';
SELECT STUFF(@.F_TIM, 3, 0, ':');
SELECT CAST(STUFF(@.F_TIM, 3, 0, ':') AS datetime);

Note that the reply posted by othellomy will work, but won't enable you
to efficiently use an index on the J_TIM column (in case there is one).

Also read Tibor's ultimate guide to the datetime datatype. You'll find
it at http://www.karaszi.com/SQLServer/info_datetime.asp
--
Hugo Kornelis, SQL Server MVP|||thanks for the codes...they all worked out...
regards!!!
Hugo Kornelis wrote:

Quote:

Originally Posted by

On 22 Nov 2006 22:46:19 -0800, paul_zaoldyeck wrote:
>

Quote:

Originally Posted by

i have another problem.and it's now on converting a char(4) to datetime
here is the situation
J_TIM < F_TIM

J_TIM is datetime while F_TIM is char of 4

example

J_TIM = 20:30
F_TIM = 2030

how can i convert F_TIM to datetime so that i can compare them.
?

thanks


>
Hi Paul,
>
DECLARE @.F_TIM char(4);
SET @.F_TIM = '2030';
SELECT STUFF(@.F_TIM, 3, 0, ':');
SELECT CAST(STUFF(@.F_TIM, 3, 0, ':') AS datetime);
>
Note that the reply posted by othellomy will work, but won't enable you
to efficiently use an index on the J_TIM column (in case there is one).
>
Also read Tibor's ultimate guide to the datetime datatype. You'll find
it at http://www.karaszi.com/SQLServer/info_datetime.asp
>
--
Hugo Kornelis, SQL Server MVP

conversion from CHAR to DATETIME error

on a column DateNew = DateTime

i am trying :
INSERT INTO [dbo].[Users] (DateNew) VALUES ('2003/01/31 10:04:14')

and i get an error :
conversion of char data type to datetime data type resulted in an out of range datetime value

I had never this error before , do you know why ?
i must enter a yyyy/mm/dd format because this database will be used for Fr and Us langages

thank you for helpingIm not getting any error with ur insert statement.I think u are passing date value as a variable which is in char.

try trim that variable on both side using ltrim,rtrim before inserting.|||lookup SET DATEFORMAT in Books online. Maybe that would help.|||You can try this -
insert <tablename>
select convert(datetime,'2003/01/31 10:04:14')

Well, for more info check this out...
http://groups.google.co.in/group/microsoft.public.sqlserver.programming/browse_frm/thread/ada70fc46e7005ac/419fae5346ac4bc0?lnk=st&q=dateformat()+in+ms+sql+server+200&rnum=3&hl=en#419fae5346ac4bc0|||try this instead --

INSERT INTO [dbo].[Users] (DateNew) VALUES ('2003-01-31 10:04:14')|||INSERT INTO [dbo].[Users] (DateNew) VALUES ('2003/01/31 10:04:14')

insert <tablename>
select convert(datetime,'2003/01/31 10:04:14')

or this one

try this instead --

INSERT INTO [dbo].[Users] (DateNew) VALUES ('2003-01-31 10:04:14')
__________________

These statements all are working fine in my machine also,so no problem in statements...I think mailler made a point ..plz check that.
Joydeep|||i got it with
INSERT INTO [dbo].[Users] (DateNew) VALUES (convert(datetime,'2003/01/31 10:04:14',111))

thank you|||i got it with convert(datetime,'2003/01/31 10:04:14',111)

thank you|||I know that I'm being pendantic here, but I'd invest a bit of time now into making your application much more portable/flexible/etc. The ISO 8601 (http://www.iso.org/iso/en/prods-services/popstds/datesandtime.html) format for date/time information is CCYY-MM-DD HH:MM:SS.TTT and that format is used by virtually the entire computing universe. It has been adopted by W3C (http://www.w3.org/TR/NOTE-datetime) which means that almost anywhere you find time on the Internet, you'll find it in this format.

You'll almost certainly save yourself lots of time and energy if you switch to using this format now, instead of having to switch to it later!

As a side note, if you expect your application to grow to the point where you may need to support more than one server, I'd suggest you spend the time to convert your application to use UCT (aka GMT) now too... This is easy to do up front, and almost impossible to do "after the fact" due to many very difficult problems caused by different locales.

-PatP|||...due to many very difficult problems caused by different locales...
PatP
You mean time zones, right?|||You mean time zones, right?You can think of the problem that way, but it is really more complex than just time zone... A locale rolls the problem up into a nice tidy (but not simple bundle). Time zones reflect a difference between local time and UCT, essentially a time offset. The problem comes from Daylight Savings Time, where different locales observe different shift dates, not all of which adjust by the same amount (some only move 30 minutes).

Unravelling the mess is easy if done while recording an event because it is easy for a computer to find UCT from its local time if necessary. Once the time is stored, there may not be any way to recover true UCT again. This gets really hard to explain, but there have been a couple of good whitepapers done on the problem.

-PatP|||Thanks a lot I shall do it at once but HOW do you convert a normal date into ISO 8601 ?

what is the SQL command for it ?|||for the moment I store all my dates time inthe format
yyyy/MM/dd hh:mm:ss

2000/12/31 18:50:06
I have just to replace / by - ?

thanks a lot|||for the moment I store all my dates time inthe format
yyyy/MM/dd hh:mm:ss

2000/12/31 18:50:06
I have just to replace / by - ?

thanks a lotYes! Exactly.

This is a relatively small change "up front", but it makes your date/time format match the format used by nearly everything else. That makes your code much easier to port to other programming languages, databases, etc at a later time. It is a small investment up front, that can pay off hugely in the future.

As a side note, SQL Server stores the data internally in a completely different form... Once you get the data into a column or variable, the work has been done. The only place you need to change anything is in the actual conversion from a character representation to a DATETIME.

-PatP|||Pat I did it , I have inserted 100 rows in my SQL database
in the good format but the database seems change it for the french format
31/12/2005 18:20:45

Once you get the data into a column or variable
I store the date in a datetime format column ?!

thanks a lot

and for searching any row in my database where a date
>
<
=
<>
=<
<=
to another date but on the date not on the datetime (yyyy-MM-dd) ?|||What is actually happening is that the database stores the value internally as a bunch of bits... They don't look like anything to the average human eye, and are logically close to a pair of integer counters. When your application retrieves the DATETIME value, the client component of the software converts those bits to a human readable form based on the locale that I mentioned in an earlier post, and the rules for that conversion happen to make the converted text appear "French" on your machine.

There are a number of ways to search for dates within a range (such as entered at any time on a given date). I prefer to do this by finding the minimum value (the very start of the day, at midnight) and the maximum value (or just past the maximum value if that is easier), then finding values between the minimum and maximum that I've selected. So for instance to find values that happened on Saint Valentine's Day 2006, I'd use:SELECT *
FROM myTable
WHERE '2006-02-14' <= myDate -- Note "equals"
AND myDate < '2006-02-15' -- Note no "equals"Using this logic is a bit strange at first, but it allows the database to use indicies to find dates of interested quickly and easily. That makes it possible to pick the rows for one day out of ten years worth of data in seconds instead of hours!

-PatP|||you helped me a lot Pat !!! in a few answers more than a few weeks looking everywhere, thanks a TON

a last question !!

I am using now

WHERE CONVERT(CHAR(10), myDate, 120) = CONVERT(CHAR(10), myDateValue, 120)

it is very easy with server side language to generate it, but for the database on millions of rows (the application will be very big) is it faster or slower than your method ?

WHERE '2006-02-14' <= myDate AND myDate < '2006-02-15'

because with your method I must add a day to the normal date and it is more complicated for server side programming

thanks again for helping|||I am using now

WHERE CONVERT(CHAR(10), myDate, 120) = CONVERT(CHAR(10), myDateValue, 120)

it is very easy with server side language to generate it, but for the database on millions of rows (the application will be very big) is it faster or slower than your method ?slower, much slower

first of all, you don't have to convert a datetime value such as '2006-02-15' to datetime, as you do on the right side of that condition, because the database will treat it that way (as a datetime value) by default

however, if you convert your table column to a string, as you do on the left side of that condition, then the database cannot use the index, if any, on that column, and will do a table scan

in other words, performing a function on a column means that the condition is not sargable (http://netknowledgenow.com:81/CS/blogs/onmaterialize/archive/2006/01/11/65.aspx) (this link is not working today but it was fine yesterday, it's a really good explanation -- you can also do a quick search to find other articles which also explain that word)|||then i must absolutly keep this only way ? :

SELECT FROM myTable
WHERE '2006-02-14' <= myDate AND myDate < '2006-02-15'

but in my database datetimes are stored in that was yyyy/mm/dd hh:mm:ss
and for the moment i couldnt get any row comparing yyyy/mm/dd hh:mm:ss to yyyy/mm/dd

of course the column is a datetime datatype|||then i must absolutly keep this only way ? :

SELECT FROM myTable
WHERE '2006-02-14' <= myDate AND myDate < '2006-02-15'that is the only way to achieve good performance (except you need to change the first operand from <= to >=)

but in my database datetimes are stored in that was yyyy/mm/dd hh:mm:ssno, actually, they are not stored that way -- datetimes are stored as two integers|||and is it better to use a datetime columns or a smalldatetime
all my dates are starting after 2000 ?

thank you|||that depends on whether you need precision in the time|||smalldatetime : Date and time data from January 1, 1900, through June 6, 2079,
with an accuracy of one minute

datetime :Date and time data from January 1, 1753, through December 31, 9999,
with an accuracy of 3.33 milliseconds

if i dont need (who needs ?) a precision of 1 minutes is it better for performances on millions of rows to use smalldatetime ?|||now I get all int that way and it seems to work :

-----------

>= 2006-02-10

SELECT FROM Users
WHERE (DateColumn > '2006-02-11')

-----------

< 2006-02-10

SELECT FROM Users
WHERE (DateColumn < '2006-02-10')
-----------
<= 2006-02-10

SELECT FROM Users
WHERE (DateColumn < '2006-02-11')
-----------

= 2006-02-10
SELECT FROM Users
WHERE
(DateColumn >= '2006-02-10')
AND
(DateColumn < '2006-02-11')

-----------

<> 2006-02-10

SELECT FROM Users
WHERE
(DateColumn > '2006-02-11')
OR
(DateColumn <= '2006-02-10')
-----------

>= 2006-02-15

SELECT FROM Users
WHERE (DateColumn < '2006-02-15')|||if i dont need (who needs ?) a precision of 1 minutes is it better for performances on millions of rows to use smalldatetime ?Yes, a SMALLDATETIME will perform better than a DATETIME for many reasons. Maybe looking at things from the machine's perspective will help (and maybe that will just confuse issues even more):DECLARE @.d DATETIME, @.s SMALLDATETIME

SELECT @.d = GetUTCDate()
SELECT @.s = @.d

SELECT @.d, Convert(VARBINARY(20), @.d)
SELECT @.s, Convert(VARBINARY(20), @.s)

SELECT @.d = DateAdd(minute, 1, @.d)
SELECT @.s = @.d

SELECT @.d, Convert(VARBINARY(20), @.d)
SELECT @.s, Convert(VARBINARY(20), @.s)-PatP|||thanks again a lot Pat that was really usefull, a deep help

thank you to everybody|||2006-02-16 03:10:53.967 | 0x0000976A00346E9E

2006-02-16 03:11:00 | 0x976A00BF

2006-02-16 03:11:53.967 | 0x0000976A0034B4EE

2006-02-16 03:11:53.967 | 0x0000976A0034B4EE

2006-02-16 03:12:00 | 0x976A00C0

here is the result of your Query
not easy to read and understand|||hard to understand?

these numbers -- 0x0000976A00346E9E, 0x976A00BF -- show you exactly how datetime values are stored internally in sql server

:)|||not easy to read and understandThe results show a couple of the issues that I was trying to explain, in a concrete form (so we don't have to talk abstractly, but can deal with real values. Please bear with me, this explanation is long, but I think it will help.

The first two results show the difference between a DATETIME (as displayed in character form) and how that DATETIME value converts to both raw binary (on the same line), and how it converts to a SMALLDATETIME (which appears one line down), and also to the SMALLDATETIME expressed as raw binary. All of the values are different, but they represent the same moment in time in different ways!

A DATETIME is accurate to +/- 3 milliseconds (I know the docs say 3.33, but that is a case where the doc writers took some liberty with what is actually stored... There's no such thing as a third of a bit). It is actually stored as a bunch of bits that mean very little to the untrained eye, except for one minor thing I'll get to later.

A SMALLDATETIME is accurate to +/- 1 minute. When you assign a DATETIME to a SMALLDATETIME, rounding takes place to the nearest whole minute. The same time is represented in a slightly less acurate form. The binary value is also quite a bit smaller, and radically different.

As an interesting side note (of little practical value), note that the value 976A appears in both of the binary strings (although in different places). This is not an accident. It has to do with how the date values are actually stored.

Another more interesting note is that the binary values of a DATETIME and the corresponding SMALLDATETIME are not directly comparable. If you take the time to understand the details, you can work around this, but it is of very little use except as an academic exercise.

Things become a bit more interesting when we add a minute to the original DATETIME value. The character form makes it easy to see this addition, and it makes perfect sense.

When we convert the changed DATETIME value to a SMALLDATETIME, the same rounding takes place, and the binary values are still quite different from each other.

The interesting part comes when you compare the binary values of the DATETIME before and after adding a minute, and the binary values of the SMALLDATETIME before and after adding a minute. The important part to notice is that the binary values of the later values have larger binary values too! There is a direct, one to one relationship between the time and the corresponding binary value.

This is why Rudy pointed out earlier that computing the minimum and maximum values of interest was much more efficient than converting the date values to character form and comparing them... You can convert a DATETIME into many different character forms, most of which are not usable for range computations like this, so in order to search for a character form SQL Server has to query every possible row. SQL Server understands dates as either DATETIME or SMALLDATETIME values, and knows how to search an index for values in a specific range. This means that it can "ride the index" to only the rows of interest in your range, and it knows exactly when it has reached the end of that range. For large sets of data, this is MANY times more efficient!

-PatP|||wow ! perfect !
I never like to apply something without understanding it
now I get a better idea of the way SQL is working with dates

and I think that generally i need only smalll
datetime datatype, i had never used it before

thanks a lot once more !|||One more thing to throw out, just so you don't get surprised... The SMALLDATETIME datatype allows entry of temporal (time based) values from 1900-01-01 through 2079-06-06 23:39. This is fine for many purposes (I won't be alive to deal with any problems it might cause when it runs out, and I sincerely doubt that SQL Server will still be in use (at least in its present form) 70 years from now! However, many contracts (like Japanese mortgages) already extend well past that limit, so I usually use DATETIME even though a SMALLDATETIME would do.

I'm a lazy bum... If I can code/create something once then safely forget about it, I'll almost always do that instead of something a bit simpler that will work for a while, but might be a problem for me (or my successor) in the future. I won't do a lot of work to avoid a potential problem, but I'll do easy things to avoid getting a call at 03:00 wondering why a job failed and how soon can I get it fixed!

-PatP|||Pat your code works fine for me in SQL 2005, but a customer with SQL 2000 get errors again everywhere on dates, i have started a new thread here >> http://dbforums.com/showthread.php?t=1212861

i am really lost with dates .. and i dont know what to do

thanks again

Conversion failed when converting datetime from character string

I have a strange problem that I need help troubleshooting. I have the
following statement in a stored procedure:
SELECT IsNull(NullIf(Convert(varchar(20), Cast(Value AS datetime), 126), ''),
'')
FROM #TFieldValues TFV
WHERE TFV.DataType = 'Date'
When this statement is run, it returns the following error;
Msg 241, Level 16, State 1, Procedure <the name of my procedure>, Line 142
Conversion failed when converting datetime from character string.
The field #TFieldValues.Value is created as varchar(2000).
So, I run the following statement, and 21 rows are returned, where 8 are date
values and 13 are empty strings:
SELECT Value FROM #TFieldValues WHERE DataType = 'Date'
The 8 date values returned are the following:
2/23/2006
03/21/2006
08/23/2006
1O/18/2OO5
1O/18/2OO5
1O/18/2OO5
02/26/2007
02/26/2007
I then run the following statement
SELECT
TFV.Value
FROM #TFieldValues TFV
WHERE
CASE
WHEN ISDATE(Value) = 0 THEN 0
WHEN ISDATE(Value) = 1 THEN 1
END = 1
AND TFV.DataType = 'Date'
Instead of 8 date values being returned, I only return 5, which are the
following:
2/23/2006
03/21/2006
08/23/2006
02/26/2007
02/26/2007
In just looking at the returns in the grid in Management Studio, when I run
the select statement that returned the 8 date values, it appears the
1O/18/2OO5 values are of a different font size. This can probably even be
seen as you compare the zero's from the following paste:
08/23/2006
1O/18/2OO5
Any ideas on validation, or handling this situation?
Message posted via droptable.com
http://www.droptable.com/Uwe/Forums.aspx/sql-server/200703/1
"cbrichards via droptable.com" <u3288@.uwe> wrote in message
news:6f39ac268793b@.uwe...
>I have a strange problem that I need help troubleshooting. I have the
> following statement in a stored procedure:
> SELECT IsNull(NullIf(Convert(varchar(20), Cast(Value AS datetime), 126),
> ''),
> '')
> FROM #TFieldValues TFV
> WHERE TFV.DataType = 'Date'
> When this statement is run, it returns the following error;
> Msg 241, Level 16, State 1, Procedure <the name of my procedure>, Line 142
> Conversion failed when converting datetime from character string.
> The field #TFieldValues.Value is created as varchar(2000).
> So, I run the following statement, and 21 rows are returned, where 8 are
> date
> values and 13 are empty strings:
> SELECT Value FROM #TFieldValues WHERE DataType = 'Date'
> The 8 date values returned are the following:
> 2/23/2006
> 03/21/2006
> 08/23/2006
> 1O/18/2OO5
> 1O/18/2OO5
> 1O/18/2OO5
> 02/26/2007
> 02/26/2007
> I then run the following statement
> SELECT
> TFV.Value
> FROM #TFieldValues TFV
> WHERE
> CASE
> WHEN ISDATE(Value) = 0 THEN 0
> WHEN ISDATE(Value) = 1 THEN 1
> END = 1
> AND TFV.DataType = 'Date'
> Instead of 8 date values being returned, I only return 5, which are the
> following:
> 2/23/2006
> 03/21/2006
> 08/23/2006
> 02/26/2007
> 02/26/2007
> In just looking at the returns in the grid in Management Studio, when I
> run
> the select statement that returned the 8 date values, it appears the
> 1O/18/2OO5 values are of a different font size. This can probably even be
> seen as you compare the zero's from the following paste:
> 08/23/2006
> 1O/18/2OO5
No - it isn't a font issue. These are capital O characters, not zeros.
Switch to a font that uses slashed zeros and you will more clearly see this.
Consider this one of the "advantages" to using the EAV data model - store
anything

Conversion failed when converting datetime from character string

I have a strange problem that I need help troubleshooting. I have the
following statement in a stored procedure:
SELECT IsNull(NullIf(Convert(varchar(20), Cast(Value AS datetime), 126), ''),
'')
FROM #TFieldValues TFV
WHERE TFV.DataType = 'Date'
When this statement is run, it returns the following error;
Msg 241, Level 16, State 1, Procedure <the name of my procedure>, Line 142
Conversion failed when converting datetime from character string.
The field #TFieldValues.Value is created as varchar(2000).
So, I run the following statement, and 21 rows are returned, where 8 are date
values and 13 are empty strings:
SELECT Value FROM #TFieldValues WHERE DataType = 'Date'
The 8 date values returned are the following:
2/23/2006
03/21/2006
08/23/2006
1O/18/2OO5
1O/18/2OO5
1O/18/2OO5
02/26/2007
02/26/2007
I then run the following statement
SELECT
TFV.Value
FROM #TFieldValues TFV
WHERE
CASE
WHEN ISDATE(Value) = 0 THEN 0
WHEN ISDATE(Value) = 1 THEN 1
END = 1
AND TFV.DataType = 'Date'
Instead of 8 date values being returned, I only return 5, which are the
following:
2/23/2006
03/21/2006
08/23/2006
02/26/2007
02/26/2007
In just looking at the returns in the grid in Management Studio, when I run
the select statement that returned the 8 date values, it appears the
1O/18/2OO5 values are of a different font size. This can probably even be
seen as you compare the zero's from the following paste:
08/23/2006
1O/18/2OO5
Any ideas on validation, or handling this situation?
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200703/1"cbrichards via SQLMonster.com" <u3288@.uwe> wrote in message
news:6f39ac268793b@.uwe...
>I have a strange problem that I need help troubleshooting. I have the
> following statement in a stored procedure:
> SELECT IsNull(NullIf(Convert(varchar(20), Cast(Value AS datetime), 126),
> ''),
> '')
> FROM #TFieldValues TFV
> WHERE TFV.DataType = 'Date'
> When this statement is run, it returns the following error;
> Msg 241, Level 16, State 1, Procedure <the name of my procedure>, Line 142
> Conversion failed when converting datetime from character string.
> The field #TFieldValues.Value is created as varchar(2000).
> So, I run the following statement, and 21 rows are returned, where 8 are
> date
> values and 13 are empty strings:
> SELECT Value FROM #TFieldValues WHERE DataType = 'Date'
> The 8 date values returned are the following:
> 2/23/2006
> 03/21/2006
> 08/23/2006
> 1O/18/2OO5
> 1O/18/2OO5
> 1O/18/2OO5
> 02/26/2007
> 02/26/2007
> I then run the following statement
> SELECT
> TFV.Value
> FROM #TFieldValues TFV
> WHERE
> CASE
> WHEN ISDATE(Value) = 0 THEN 0
> WHEN ISDATE(Value) = 1 THEN 1
> END = 1
> AND TFV.DataType = 'Date'
> Instead of 8 date values being returned, I only return 5, which are the
> following:
> 2/23/2006
> 03/21/2006
> 08/23/2006
> 02/26/2007
> 02/26/2007
> In just looking at the returns in the grid in Management Studio, when I
> run
> the select statement that returned the 8 date values, it appears the
> 1O/18/2OO5 values are of a different font size. This can probably even be
> seen as you compare the zero's from the following paste:
> 08/23/2006
> 1O/18/2OO5
No - it isn't a font issue. These are capital O characters, not zeros.
Switch to a font that uses slashed zeros and you will more clearly see this.
Consider this one of the "advantages" to using the EAV data model - store
anythingsqlsql

Conversion failed when converting datetime from character string

I have a strange problem that I need help troubleshooting. I have the
following statement in a stored procedure:
SELECT IsNull(NullIf(Convert(varchar(20), Cast(Value AS datetime), 126), '')
,
'')
FROM #TFieldValues TFV
WHERE TFV.DataType = 'Date'
When this statement is run, it returns the following error;
Msg 241, Level 16, State 1, Procedure <the name of my procedure>, Line 142
Conversion failed when converting datetime from character string.
The field #TFieldValues.Value is created as varchar(2000).
So, I run the following statement, and 21 rows are returned, where 8 are dat
e
values and 13 are empty strings:
SELECT Value FROM #TFieldValues WHERE DataType = 'Date'
The 8 date values returned are the following:
2/23/2006
03/21/2006
08/23/2006
1O/18/2OO5
1O/18/2OO5
1O/18/2OO5
02/26/2007
02/26/2007
I then run the following statement
SELECT
TFV.Value
FROM #TFieldValues TFV
WHERE
CASE
WHEN ISDATE(Value) = 0 THEN 0
WHEN ISDATE(Value) = 1 THEN 1
END = 1
AND TFV.DataType = 'Date'
Instead of 8 date values being returned, I only return 5, which are the
following:
2/23/2006
03/21/2006
08/23/2006
02/26/2007
02/26/2007
In just looking at the returns in the grid in Management Studio, when I run
the select statement that returned the 8 date values, it appears the
1O/18/2OO5 values are of a different font size. This can probably even be
seen as you compare the zero's from the following paste:
08/23/2006
1O/18/2OO5
Any ideas on validation, or handling this situation?
Message posted via droptable.com
http://www.droptable.com/Uwe/Forum...server/200703/1"cbrichards via droptable.com" <u3288@.uwe> wrote in message
news:6f39ac268793b@.uwe...
>I have a strange problem that I need help troubleshooting. I have the
> following statement in a stored procedure:
> SELECT IsNull(NullIf(Convert(varchar(20), Cast(Value AS datetime), 126),
> ''),
> '')
> FROM #TFieldValues TFV
> WHERE TFV.DataType = 'Date'
> When this statement is run, it returns the following error;
> Msg 241, Level 16, State 1, Procedure <the name of my procedure>, Line 142
> Conversion failed when converting datetime from character string.
> The field #TFieldValues.Value is created as varchar(2000).
> So, I run the following statement, and 21 rows are returned, where 8 are
> date
> values and 13 are empty strings:
> SELECT Value FROM #TFieldValues WHERE DataType = 'Date'
> The 8 date values returned are the following:
> 2/23/2006
> 03/21/2006
> 08/23/2006
> 1O/18/2OO5
> 1O/18/2OO5
> 1O/18/2OO5
> 02/26/2007
> 02/26/2007
> I then run the following statement
> SELECT
> TFV.Value
> FROM #TFieldValues TFV
> WHERE
> CASE
> WHEN ISDATE(Value) = 0 THEN 0
> WHEN ISDATE(Value) = 1 THEN 1
> END = 1
> AND TFV.DataType = 'Date'
> Instead of 8 date values being returned, I only return 5, which are the
> following:
> 2/23/2006
> 03/21/2006
> 08/23/2006
> 02/26/2007
> 02/26/2007
> In just looking at the returns in the grid in Management Studio, when I
> run
> the select statement that returned the 8 date values, it appears the
> 1O/18/2OO5 values are of a different font size. This can probably even be
> seen as you compare the zero's from the following paste:
> 08/23/2006
> 1O/18/2OO5
No - it isn't a font issue. These are capital O characters, not zeros.
Switch to a font that uses slashed zeros and you will more clearly see this.
Consider this one of the "advantages" to using the EAV data model - store
anything

Conversion failed when converting datetime from character string

Hi,

I receive an Error Message: Conversion failed when converting datetime from character string when I try to run this.

Can someone point out what I'm doing wrong?

SELECT Principal,

SUM(CASE WHEN Recdate BETWEEN '=@.LYbegin' AND '=@.LYend' THEN

Amount ELSE 0 END) AS LY,

SUM(CASE WHEN Recdate BETWEEN '=@.TYbegin' AND '=@.TYend' THEN

Amount ELSE 0 END) AS TY

FROM dbo.Checks

GROUP BY Principal

If I execute the query with the dates it works fine:

SELECT Principal,

SUM(Case When RecDate BETWEEN '1-1-2005 00:00:00.000' AND '1-30-2005 00:00:00.000' THEN Amount else 0 end)AS LY,

SUM(Case When RecDate BETWEEN '2-1-2005 00:00:00.000' AND '2-28-2005 00:00:00.000' THEN Amount else 0 end)AS TY

FROM Checks

GROUP BY Principal

Thanks,

Terry McCullagh

I suppose you are trying to use a parameterized query statement in the RS query designer. You should use the following commandtext to have query parameters being detected and working:

SELECT Principal,
SUM(CASE WHEN Recdate BETWEEN @.LYbegin AND @.LYend THEN Amount ELSE 0 END) AS LY,
SUM(CASE WHEN Recdate BETWEEN @.TYbegin AND @.TYend THEN Amount ELSE 0 END) AS TY
FROM dbo.Checks
GROUP BY Principal

-- Robert

|||

Robert,

That works great.

Thank you,

Terry McCullagh

Conversion Error...nvarchar to Datetime

Hi Group,
I am new with SQL Server..I am working with SQL Server 2000.
I am storing the date in a nvarchar column of atable... Now I want to
show the data of Weekends..Everything is OK...But the problem is
arising with Conversion of nvarchar to date...to identify the
weekends...Like..Here DATEVALUE is a nvarchar column...But getting the
error..Value of DATEVALUE like dd-mm-yyyy...04-08-2004

------------------
Server: Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.
------------------
------Actual Query----------
Select DATEVALUE,<Other Column Names> from Result where
Datepart(dw,convert(Datetime,DATEVALUE))<>1 and
Datepart(dw,convert(Datetime,DATEVALUE))<>7
------------------
Thanks in advance..
Regards
Arijit Chatterjee(arijitchatterjee123@.yahoo.co.in) writes:
> I am new with SQL Server..I am working with SQL Server 2000.
> I am storing the date in a nvarchar column of atable... Now I want to
> show the data of Weekends..Everything is OK...But the problem is
> arising with Conversion of nvarchar to date...to identify the
> weekends...Like..Here DATEVALUE is a nvarchar column...But getting the
> error..Value of DATEVALUE like dd-mm-yyyy...04-08-2004

Best is to store date values in datetime columns. If you use character
format, you should use char (the n just doubles the space with no gain
for it, and the var is pointless since size is fixed), and you should use
the format YYYYMMDD. Furthermore, you should attach a constraint to the
columns

datecol char(8) CONSTRAINT ck_tbl_datecol CHECK (isdate(datecol) = 1)

to ascertain that you don't get illegal values.

Storing dates in a format like DD-MM-YYYY is going to give all sorts of
headache. 04-08-2004 could be interpreted as Aug 4th or April 8th, depending
on language and datefromat settings. (And, in case of humans, of the
perceptions of the user.) You can't sort on this format (unless you really
want 3 Aug to come before 4 June).

The format YYYYMMDD sorts well, and is always interpreted in the same way.

See also http://www.karaszi.com/SQLServer/info_datetime.asp.

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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||See this

declare @.t table (d varchar(20))
insert into @.t values('10-apr-2005')
insert into @.t values('10-MAy-2005')
insert into @.t values('10-Jun-2005')
insert into @.t values('10-Jul-2005')
Select * from @.t where Datepart(dw,convert(Datetime,d))<>1 and
Datepart(dw,convert(Datetime,d))<>7

Madhivanan|||Thanks for your great help..
Regards
Arijit Chatterjee

Conversion ERROR

This is the error message I get: :(
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.

This is the query:
Select Qual_ins.CompanyCode, Qual_ins.ParticipantCode, Qual_ins.Ins_Code, Qual_ins.Plan_Code,
dbo.PremiumRate(Qual_Ins.Crit,Qual_Ins.PQB_Spec,Pl an_Mas.Extend_Fee,
Qual_Ins.Adjpremium,Qual_Ins.Adjpremiumper,Qual_In s.Adjpremend,
GetDate(),Qual_Ins.Cover_Amt, Plan_Mas.CR_A, Plan_Mas.CR_B,
Plan_Mas.CR_C, Plan_Mas.CR_D, Plan_Mas.CR_E, Plan_Mas.CR_F,
Plan_Mas.CR_G, Plan_Mas.CR_H, Plan_Mas.CR_I, Plan_Mas.CR_J,
Plan_Mas.CR_K, Plan_Mas.CR_L, Plan_Mas.CR_M, Plan_Mas.CR_N,
Plan_Mas.CR_O) AS PremiumRate
FROM Qual_ins, Plan_Mas
WHERE Qual_Ins.CompanyCode = 'ACME'
AND Qual_ins.ParticipantCode = 4
AND Plan_Mas.CompanyCode = Qual_ins.CompanyCode
AND Plan_Mas.Ins_Code = Qual_ins.Ins_Code
AND Plan_Mas.Plan_Code = Qual_ins.Plan_Code
Order BY Qual_ins.Ins_Code

Please let me know if you need to see my PremiumRate (User Defined Function) in order to help me elimate this error message.
Any help is appreciate!
I'm new to this... "Hello" to all!

ShuviBased on the error message, I'd guess that you are trying to convert a string (CHAR or VARCHAR) to a DATETIME or a SMALLDATETIME. If that is the case, then one or more rows in your data isn't a valid date string.

-PatP

Conversion datetime to string

Hi
Can we convert DateTime to String. If so how?
Thanks in advance.

Mahathi.My problem has been solved.