Using DateTime in inlined SQL

Using DateTime in inlined SQL


I'm wondering if there is easy way or best practice to inline DateTime in the parameter for advanced SQL?
When I have a dynamical SQL part, that includes DateTime, I would compose it as, e.g.:
{Entity}.[Modified] > CAST('2015-01-21 10:00:00' as datetime)

I was looking for some built-in formatting function that will format my DateTime as above, but couldn't find any appropriate. The functions available use server date format configuration, which I can not rely on, so I can easily get SQL error about varchar can not be converted to datetime.
I had to write custom function that builds this string straightforward, by using separate date parts and combining them.

But I think there should be some better way. Is it?
If what you are looking is a function that includes the CAST then there's none.
FormatFullDateTime would give you '2015-01-21 10:00:00'
Off course the question is not in CAST, the question is how to format a date strictly as YYYY-MM-DD so that it can be used in SQL. And I don't mind against another aproach without character string, if it easy to use - this is just the way I'm used to do it. I was thinking that I'm probably not the first who uses DateTime in inlined SQL, so there should be some popular simple approach.

FormatFullDateTime gives me "23-January-2015 10:49:50", which is also what it's documentation states. Well, I have just checked that this value can be parsed by SQL, so I guess this can be used... Maybe it's even better than YYYY-MM-DD, because month name guarantees that it won't be confused with day.

Hi Igor,

I think Andre misstyped that, the one that gives the format you need is FormatShortDateTime

João Rosado
I stand corrected :)
Thanx João