473,696 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.dtDa teTime,120),
count(convert(v archar,J1708Dat a.dtDateTime,12 0))

FROM Vehicle INNER JOIN J1708Data ON Vehicle.iID = J1708Data.iVehi cleId

WHERE (J1708Data.iPid Id = 303) AND
(J1708Date.dtDa teTime between '2006-10-25' AND '2006-10-28')
AND (Vehicle.sDescr iption = $VehicleID)

GROUP BY convert(varchar ,J1708Data.dtDa teTime,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.

Nov 2 '06 #1
2 12000
kirke wrote:
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.dtDa teTime,120),
count(convert(v archar,J1708Dat a.dtDateTime,12 0))

FROM Vehicle INNER JOIN J1708Data ON Vehicle.iID = J1708Data.iVehi cleId

WHERE (J1708Data.iPid Id = 303) AND
(J1708Date.dtDa teTime between '2006-10-25' AND '2006-10-28')
AND (Vehicle.sDescr iption = $VehicleID)

GROUP BY convert(varchar ,J1708Data.dtDa teTime,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.dtDateTi me,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.dtDateTi me,120)
--
MGFoster:::mgf0 0 <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/AwUBRUrBwIechKq OuFEgEQKUTQCg1z GcAeAViDrJQWxEN dcn2txbhxYAoO4o
1Mks6W+FiXviMMr Zi/lt4e3z
=vWR9
-----END PGP SIGNATURE-----
Nov 3 '06 #2
On 2 Nov 2006 10:53:08 -0800, kirke wrote:
>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="SELEC T convert(varchar ,J1708Data.dtDa teTime,120),
count(convert( varchar,J1708Da ta.dtDateTime,1 20))

FROM Vehicle INNER JOIN J1708Data ON Vehicle.iID = J1708Data.iVehi cleId

WHERE (J1708Data.iPid Id = 303) AND
(J1708Date.dtD ateTime between '2006-10-25' AND '2006-10-28')
AND (Vehicle.sDescr iption = $VehicleID)

GROUP BY convert(varchar ,J1708Data.dtDa teTime,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.dtDat eTime >= '20061025'
AND J1708Date.dtDat eTime < '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.dtDa teTime,120) won't
group by daym, since the conversion doesn't chop off the time portion.
The result of select convert(varchar , current_timesta mp, 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.dtDa teTime,120),
count(convert(v archar,J1708Dat a.dtDateTime,12 0))
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 MVP
Nov 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5382
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
2
4861
by: Rock | last post by:
Hi,all: how to convert VBA #date# format (ex: #2004/3/6#) to C# code ? here are vba code : Dim objUserPerm As Office.UserPermission Set objUserPerm = ActiveWorkbook.Permission.Add("user@domain.com", msoPermissionRead + msoPermissionEdit, #12/31/2005#) a to c# ?? I don't know how to convert this code to C#? and I have already try it use DateTime format, but it happen argument error. anyone have the same problem ? ...
1
3110
by: ABC | last post by:
How to convert a date string to datetime value with custom date format? e.g. Date String Date Format Result (DateTime value) "05/07/2004" "MM/dd/yyyy" May 7, 2004 "01062005" "ddMMyyyy" June 1, 2005 "09-07-05" "MM-dd-yy" Sept 7, 2005 Is there any functions to convert?
1
2199
by: h_ghanaty | last post by:
how i can convert gregorian date to jalaly(Iran date) with vb.net
1
4886
by: Raja | last post by:
Hi Anyone has any idea of how to convert Julian Date (eg. 12744057862..) to a VB.NET date time. Thanks in advance.
1
4540
by: Sam | last post by:
How do I convert Julian Date to Calendar Date in ASP.Net 1.1 based on following guideline found at Internet? To convert Julian date to Gregorian date: double JD = 2299160.5; double Z = Math.Floor(JD+0.5); double W = Math.Floor((Z - 1867216.25)/36524.25); double X = Math.Floor(W/4);
4
15403
by: Daniel Kaseman | last post by:
How do I convert a date into a serial number? (I'm trying to enter a FROM date and a TO date, then make my PROGRESS BAR show how close I am to the TO date.) get it? I know that MS Excel converts Dates into serial numbers, but I can't figure out how to do it with VB.Net. Please help, thanks.
2
21073
by: Kakishev | last post by:
I have a problem at how best to convert a Date into a text field and keep the format dd/mmm/yyyy (01-FEB-2007). The problem is that dates are imported from SQL into an access front end Database. When the dates are imported they are in the format dd/mm/yyyy. However on the reports the dates are in the in the format dd/mmm/yyyy. I now need to allow the fields to accept an asterix (*) as a prefix to the date to show dates that have not...
3
2838
by: lyne_asp | last post by:
Please help me to convert excel date to asp date. Here is my code objConnEx.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Netroot\WebApp\testprograms\Shuttle_Service\Attachment\BusRequest.xls;Extended Properties=Excel 8.0;HDR=Yes;IMEX=1;" set objRS = server.createObject("ADODB.recordset") SQL = "SELECT * from " objRS.Open SQL,objConnEx, 1
1
9770
by: smdmca | last post by:
I have Julian date, I want to convert it into date. Is there any function in MySql to convert Julian date to date eg- Julian Date- 2455116 Date - Oct - 12, 2009
0
8597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4611
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1992
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.