[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Out-of-range datetime value

Posted on 2008-06-16
8
Medium Priority
?
1,981 Views
Last Modified: 2008-06-18
       
       
      <cfset datepassed = #DATEFORMAT(now(), "dd/mm/yy")#>

        SELECT  CONVERT(VARCHAR(10), TIMEDATE , 120) as varDate
        FROM orders
        WHERE YEAR(TIMEDATE)  = YEAR('#datepassed#')
        AND MONTH(TIMEDATE)  = MONTH('#datepassed#')
        AND DAY(TIMEDATE)  = DAY('#datepassed#')

ERROR:

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
0
Comment
Question by:pigmentarts
  • 3
  • 3
  • 2
8 Comments
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 21797913
Whenever you use Datetime, you can either use mm/dd/yyyy or you can use a string of yyyymmdd.

If you are using a machine that has the Regional settings like UK then they will want the dd/mm/yyyy settings, then I would use the universal format yyyymmdd instead.
0
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 21797916
And it is DATETIME, not TIMEDATE.
0
 
LVL 52

Expert Comment

by:Mark Wills
ID: 21810433
OK,  the    CONVERT(VARCHAR(10), TIMEDATE , 120)    is using a format number. That number is telling convert how to read/ interpret the variable TIMEDATE. Now if TIMEDATE is a legitimate datetime column, then no problems, it knows how to format. However, when using a string / variable such as datepassed, then it has to understand how it is formatted... So, before you can use datetime functions such as year, month, day, then it has to recognise the string as a date...

By the looks of things, you do not want time as a component, so, probably best to do something like :

SELECT  CONVERT(VARCHAR(10), TIMEDATE , 120) as varDate
FROM orders
WHERE convert(datetime,CONVERT(varchar(20), TIMEDATE , 106))  = convert(datetime,'#datepassed#',103)

basically allowing the datetime to do it's match without the time component, and expressing both fields as a legitimate datetime construct.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 12

Author Comment

by:pigmentarts
ID: 21810992
thank you for all your help
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 21812932
mark_aills:

using your code i still get this...

Syntax error converting datetime from character string.
0
 
LVL 52

Expert Comment

by:Mark Wills
ID: 21813417
use a 4 digit year in datepassed
0
 
LVL 52

Accepted Solution

by:
Mark Wills earned 2000 total points
ID: 21813502
this works : select convert(datetime,'21/01/2008',103)
this fails : select convert(datetime,'21/01/08',103)

this works: select convert(datetime,'21/01/08',03)
this fails : select convert(datetime,'21/01/2008',03)

even though the books online show it as yy, it isn't really... remember how I said it must match - it is really , really fussy....

have a look at the codes below...


Without century (yy)      With century (yyyy)  Standard       Input/Output 
-                         0 or 100             Default        mon dd yyyy hh:miAM (or PM)
1                         101                  U.S.           mm/dd/yyyy
2                         102                  ANSI           yy.mm.dd
3                         103                  British/French dd/mm/yy
4                         104                  German         dd.mm.yy        
5                         105                  Italian        dd-mm-yy
6                         106                  -              dd mon yy
7                         107                  -              Mon dd, yy
8                         108                  -              hh:mm:ss
-                         9 or 109             Default + ms   mon dd yyyy hh:mi:ss:mmmAM (or PM)
10                        110                  USA            mm-dd-yy
11                        111                  JAPAN          yy/mm/dd
12                        112                  ISO            yymmdd
-                         13 or 113            Europe         dd mon yyyy hh:mm:ss:mmm(24h)
14                        114                  -              hh:mi:ss:mmm(24h)
-                         20 or 120            ODBC canonical yyyy-mm-dd hh:mi:ss(24h)
-                         21 or 121            ODBC canonical yyyy-mm-dd hh:mi:ss.mmm(24h)
-                         126                  ISO8601        yyyy-mm-ddThh:mm:ss.mmm (no spaces)
                          127                  ISO8601        yyyy-mm-ddThh:mm:ss.mmmZ   where Z = time zone(no spaces)               
-                         130                  Hijri          dd mon yyyy hh:mi:ss:mmmAM
-                         131                  Hijri          dd/mm/yy hh:mi:ss:mmmAM
 

Open in new window

0
 
LVL 12

Author Comment

by:pigmentarts
ID: 21813602
i see what you mean now. just had to get my head around it. working now thanks
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article, we will show how to detach and attach a database and then show how to repair a corrupt database and attach it, If it has some errors. We will show how to detach and attach using SSMS or using T-SQL sentences.
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question