简体   繁体   中英

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

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'

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

Assuming you are using MySQL, since you didn't state what you used you can use this:

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

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

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)

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