Sunday, March 11, 2012

Compare datetime with a string

Hello!
In the database I have a column of type datetime called skip_start_time.
If I want to compare this skip_start_time with a character string for
example "2008-01-04 11:32:49" how
do I write this?
//TonyTony
CAST your character for DATETIME
"TonyJ" <johansson.andersson@.telia.com> wrote in message
news:ub8681xYIHA.5980@.TK2MSFTNGP04.phx.gbl...
> Hello!
> In the database I have a column of type datetime called skip_start_time.
> If I want to compare this skip_start_time with a character string for
> example "2008-01-04 11:32:49" how
> do I write this?
> //Tony
>|||declare @.startDate CHAR(20)
SET @.startDate ='2008-02-04 11:32:49'
IF (CAST(@.startDate as datetime) > GETDATE())
PRINT 'yes'
Jack Vamvas
___________________________________
Search IT jobs from multiple sources- http://www.ITjobfeed.com
"TonyJ" <johansson.andersson@.telia.com> wrote in message
news:ub8681xYIHA.5980@.TK2MSFTNGP04.phx.gbl...
> Hello!
> In the database I have a column of type datetime called skip_start_time.
> If I want to compare this skip_start_time with a character string for
> example "2008-01-04 11:32:49" how
> do I write this?
> //Tony
>|||Hello!
How do I write this in pure sql?
//Tony
"Jack Vamvas" <DEL_TO_REPLY@.del.com> skrev i meddelandet
news:es2dnWkCUeWm2j3anZ2dnUVZ8qGdnZ2d@.bt.com...
> declare @.startDate CHAR(20)
> SET @.startDate ='2008-02-04 11:32:49'
> IF (CAST(@.startDate as datetime) > GETDATE())
> PRINT 'yes'
>
>
> --
> Jack Vamvas
> ___________________________________
> Search IT jobs from multiple sources- http://www.ITjobfeed.com
>
>
> "TonyJ" <johansson.andersson@.telia.com> wrote in message
> news:ub8681xYIHA.5980@.TK2MSFTNGP04.phx.gbl...
> > Hello!
> >
> > In the database I have a column of type datetime called skip_start_time.
> > If I want to compare this skip_start_time with a character string for
> > example "2008-01-04 11:32:49" how
> > do I write this?
> >
> > //Tony
> >
> >
>|||On Jan 30, 2:55=A0pm, "TonyJ" <johansson.anders...@.telia.com> wrote:
> Hello!
> In the database I have a column of type datetime called skip_start_time.
> If I want to compare this skip_start_time with a character string for
> example "2008-01-04 11:32:49" how
> do I write this?
> //Tony
select convert(varchar(50),skil_start_time,120)|||Tony
Jack's script is pure SQL
"TonyJ" <johansson.andersson@.telia.com> wrote in message
news:ecgMXhyYIHA.4160@.TK2MSFTNGP03.phx.gbl...
> Hello!
> How do I write this in pure sql?
> //Tony
> "Jack Vamvas" <DEL_TO_REPLY@.del.com> skrev i meddelandet
> news:es2dnWkCUeWm2j3anZ2dnUVZ8qGdnZ2d@.bt.com...
>> declare @.startDate CHAR(20)
>> SET @.startDate ='2008-02-04 11:32:49'
>> IF (CAST(@.startDate as datetime) > GETDATE())
>> PRINT 'yes'
>>
>>
>> --
>> Jack Vamvas
>> ___________________________________
>> Search IT jobs from multiple sources- http://www.ITjobfeed.com
>>
>>
>> "TonyJ" <johansson.andersson@.telia.com> wrote in message
>> news:ub8681xYIHA.5980@.TK2MSFTNGP04.phx.gbl...
>> > Hello!
>> >
>> > In the database I have a column of type datetime called
>> > skip_start_time.
>> > If I want to compare this skip_start_time with a character string for
>> > example "2008-01-04 11:32:49" how
>> > do I write this?
>> >
>> > //Tony
>> >
>> >
>>
>|||SQL Server will implicitly covert a character string to a datetime when you
compare it to a datetime column. This is because datetime has a higher data
type precedence than character types:
SELECT *
FROM dbo.MyTable
WHERE MyDateColumn = '2008-01-04 11:32:49'
The sample datetime string you specified will work with most, but not all
date formats. You can use a date format independent string like
'2008-01-04T11:32:49' or '20080104 11:32:49' to ensure the string values is
converted as you expect regardless sessions settings. For example
SET LANGUAGE 'English'
SELECT CAST('2008-01-04 11:32:49' AS datetime)
SELECT CAST('2008-01-04T11:32:49' AS datetime)
SELECT CAST('20080104 11:32:49' AS datetime)
SET LANGUAGE 'French'
SELECT CAST('2008-01-04 11:32:49' AS datetime) --unexpected date returned
SELECT CAST('2008-01-04T11:32:49' AS datetime)
SELECT CAST('20080104 11:32:49' AS datetime)
Hope this helps.
Dan Guzman
SQL Server MVP
"TonyJ" <johansson.andersson@.telia.com> wrote in message
news:ub8681xYIHA.5980@.TK2MSFTNGP04.phx.gbl...
> Hello!
> In the database I have a column of type datetime called skip_start_time.
> If I want to compare this skip_start_time with a character string for
> example "2008-01-04 11:32:49" how
> do I write this?
> //Tony
>

No comments:

Post a Comment