简体   繁体   中英

How to compare date type in Sql to javascript date

I have a javascript date string in ISO format, eg

dtStart = 2016-09-05T07:00:00.000Z

and I want to compare it in an SQL query to a column that is label DateTimeCollected. An example of DateTimeCollected could be 2014-06-27T00:00:00.000Z .

How could I comare these two dates in SQL by using something like

DateTimeCollected >= ${dtStart}

in the query where dtStart is a string. I am using MSsql if that is of any help.

DateTimeCollected >= CONVERT(${dtStart}, @stringDate, 126)

If you are comparing the values in Sql, you should be able to simply do this:

DateTimeCollected >= CAST(@DateTimeString As DateTime). 

Assuming @DateTimeString is a char(23) variable containing a string representation of a datetime value following the ISO8601 format ( yyyy-mm-ddThh:mi:ss.mmm )

In fact, you can also do this without the casting, because Sql server will implicitly cast the string into a datetime data type. However, I beleive it's better to use explicit casting since it's more readable.

As I wrote in the comment, SQL Server should always cast the correct date if it's passed as a string following the ISO8601 format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM