Sunday, February 12, 2012

cast from string to DateTime type

HI I am using a C++ builder routine that fills a datacontrol from the result
s
of a query. Just wondering if there is a way to cast from string type to
DateTime as the data is on a Microsoft SQL 2000 server, would like to perfor
m
the cast in the query?
Thanks
--
Paul G
Software engineer.There are two T-SQL functions CAST and CONVERT which you use for type
conversions. SQL Server Books Online has all the details and examples for
using them.
Anith|||Hi thanks for the response, I tried the code below but get the error
missing operator in query expression Cast(StartTime as DateTime)="3/03/2005
"
StartTime is a string column in a table that looks like "3/03/2005 10:11:45"
Select * from tablename
WHERE CAST(StartTime AS DateTime)= "3/03/2005 12:12:00"
"Anith Sen" wrote:

> There are two T-SQL functions CAST and CONVERT which you use for type
> conversions. SQL Server Books Online has all the details and examples for
> using them.
> --
> Anith
>
>|||1. Datetime constants are represented as strings in the SQL language. String
s are surrounded by
single quotes in the SQL Language.
2. Since you have a nationalized datetime format (not recommended!), need to
use CONVERT and use the
3:rd format parameter (the style format). Else your code will fail with a no
n matching language
setting.
3. Since you compare against a string and the value in the table is a string
, why involve the
datetime datatype at all?
4. Why do you store datetimes as strings instead of datetimes?
5. You might want to check out: http://www.karaszi.com/SQLServer/in...ime.asp

Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:03322931-FD81-4F7A-940D-51EDD36B9C4C@.microsoft.com...[vbcol=seagreen]
> Hi thanks for the response, I tried the code below but get the error
> missing operator in query expression Cast(StartTime as DateTime)="3/03/200
5 "
> StartTime is a string column in a table that looks like "3/03/2005 10:11:4
5"
> Select * from tablename
> WHERE CAST(StartTime AS DateTime)= "3/03/2005 12:12:00"
> "Anith Sen" wrote:
>

No comments:

Post a Comment