Sunday, February 12, 2012

Convert datetime to char/nvarchar or the opposite (in specific

Omnibuzz,
In the beginning of my code, I take the date from the table and its contents
(in c#) is: "11/05/2006 12:20:35"”.
When I want to pass it to sql the real value of the column there (from the
query analyzer) is:
2006-05-11 12:20:35.487
So when I query:
TDate = cast('11/05/2006 12:20:35' as datetime)
They not equal.
Do I need to separate the year month seconds and more and than to compare
them?
Or there is other way?
"Omnibuzz" wrote:

> Hi,
> Converting datetime to varchar for comparison is not a good option.
> But you can convert varchar to datetime.
> Can't you use somthing like this for comparison?
> Tdae = cast('11/05/2006 12: 20: 35' as datetime)
> Or have I understood the question wrong?
> --
> -Omnibuzz (The SQL GC)
> http://omnibuzz-sql.blogspot.com/
>
>Hi,
When you are sending it from the C# code to the SP, format the date as
follows.
DateValue.ToString("yyyy-MM-dd hh:mm:ss.fff")
Hope this helps.
--
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/|||Hi Omnibuzz,
First thank you, I tried to run it but it does not compile.
The two error messages are:
1)The best overloaded method match for ‘string.tostring
(system.iformatprovider)’ has some invalid arguments.
2)Argument ‘1’: cannot convert from string to system.iformatprovider.
"Omnibuzz" wrote:

> Hi,
> When you are sending it from the C# code to the SP, format the date as
> follows.
> DateValue.ToString("yyyy-MM-dd hh:mm:ss.fff")
> Hope this helps.
> --
> -Omnibuzz (The SQL GC)
> http://omnibuzz-sql.blogspot.com/
>|||You should be format the date variable.
Something like this..
DateTime dt
dt = System.DateTime.Now
dt.ToString("yyyy-MM-dd hh:mm:ss.fff")
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/|||Omnibuzz thank you , in the end I solve it like this:
triggerTime = yearTriggerDate + "-" + monthTriggerDate + "-" +
dayTriggerDate + " " + hoursTriggerDate + ":" + minutesTriggerDate + ":" +
secondsTriggerDate;
and then pass the variable.
"Omnibuzz " wrote:

> You should be format the date variable.
> Something like this..
> DateTime dt
> dt = System.DateTime.Now
> dt.ToString("yyyy-MM-dd hh:mm:ss.fff")
>
> --
> -Omnibuzz (The SQL GC)
> http://omnibuzz-sql.blogspot.com/
>

No comments:

Post a Comment