简体   繁体   English

SQL-日期时间转换+参数

[英]SQL - Datetime conversion + Parameters

Lots of stuff on the web about this one but i just cant get anything to work correct. 网路上有很多关于这个的东西,但是我什么都无法正常工作。

I need to find everything from a particular table where the date from my parameter is found in the 'timestamp'datetime on this table 我需要从特定表中查找所有内容,在该表的“ timestamp” datetime中可以找到参数中的日期

So lets say. 因此,可以说。

Select g.* 
From egtable..headerinfo as g
WHERE g.istatus BETWEEN 5 AND 10 AND
REPLACE(LEFT(CONVERT (varchar, timestamp, 101),10),' ','-') = convert(datetime,'<%parameterDate%>')

The timestamp displays 'as yyyy-mm-dd hh:mm:ss' my parameter date would 'be mm/dd/yyyy' 时间戳显示为“ yyyy-mm-dd hh:mm:ss”,我的参数日期将为“ mm / dd / yyyy”

My goal is to ultimately strip out the time, convert the date and then compare to parameters. 我的目标是最终剔除时间,转换日期,然后与参数进行比较。 Any help on this would be greatly appreciated. 任何帮助,将不胜感激。 I've tried countless combinations and nothing seems to work :( 我尝试了无数种组合,但似乎没有任何效果:(

Thanks B 谢谢B

Assuming you are using MySQL, since you didn't state what you used you can use this: 假设您使用的是MySQL,由于您未说明使用的内容,因此可以使用以下命令:

Select g.* 
From egtable..headerinfo as g
WHERE g.istatus BETWEEN 5 AND 10 AND
 timestamp = MAKEDATE(SUBSTRING('<%parameterDate%>', 4, 2), 
                      SUBSTRING('<%parameterDate%>', 1, 2), 
                      SUBSTRING('<%parameterDate%>', 6, 4))

MAKEDATE in MySQL creates a date field for you based on receiving day, month, year input MySQL中的MAKEDATE根据接收的日,月,年输入为您创建日期字段

If you're using SQL Server (your query looks like that) and your SQL Server version number is 2008 and higher, you can easily get rid of the time part of datetime by casting it to date 如果您使用的是SQL Server(您的查询看起来像这样)并且您的SQL Server版本号为2008或更高版本,则可以通过将date强制转换为date来轻松摆脱datetime的时间部分

cast(datetimecolumn as date)

In your case it will be: 在您的情况下,它将是:

Select g.* 
From egtable..headerinfo as g
WHERE g.istatus BETWEEN 5 AND 10 AND
cast([timestamp] as date) = cast('<%parameterDate%>' as date)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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