0

I have a query when I need to convert a string to datetime, I use

CONVERT(DATETIME, '')

but it's still not working. My column fechac is of type Datetime.

If I pass in for example '2019-11-20 00:03:56.120', the query works but I don't need the time because is a parameter from an application web and I send the date example '2019-11-20'. Thanks!

This is my query:

SELECT * 
FROM table 
WHERE cb.fechac = CONVERT(DATETIME, '2019-11-20');
1
  • Use the format yyyyMMdd. With the datetime datatype yyyy-MM-dd is ambiguous. Commented Nov 25, 2019 at 18:31

3 Answers 3

2

Use the format yyyyMMdd. With the datetime datatype yyyy-MM-dd is ambiguous. Otherwise, use a style code:

CONVERT(datetime,'2019-11-20',126);
CONVERT(datetime,'20191120')

Reading a lot through the lines in the comments here, however, are you actually after..?:

WHERE fechac >= '20191120'
  AND fechac < '20191121'

Final reading through the lines. it appears the OP is actuaklly passing a parameter (I assume of the data type date), therefore...

WHERE fechac >= @fechac
  AND fechac < DATEADD(DAY, 1, @fechac)
Sign up to request clarification or add additional context in comments.

13 Comments

Thank you, the query sintaxis is fine but give me 0 rows when I execute the query
Then that would mean there are no rows where the time is exactly 2019-11-20T00:00:00.000 @Rodrigo .
You're right there isn't, in that case how could I do it? I need the field fechac to be datetime but in this query I don't need the time because I use the time for auditory
"Because I need the time but i don't need for this query" That doesn't make sense. You either do, or you don't.
The part OP is actually after is buried inside the question!
|
0

This should work on any system, regardless of internationalization settings:

WHERE cb.fechac=convert(DATE,'20191120');

Comments

-1
SELECT *
FROM table cb
WHERE cast(cb.fechac as date) = convert(date, '2019-11-20', 23)

4 Comments

Thank you! That is the solution
This isn't the best idea at all... This can have signicant performances impacts, @Rodrigo . Use proper date logic with >= and <. Cast to date is sargable but is it a good idea?
@Larnu, where cb.fechac between convert(datetime, '2019-11-20T00:00:00.000', 126) and convert(datetime, '2019-11-20T23:59:59.999', 126) will also work. It looks like this will involve half as many reads (so twice as fast). I suppose it will come down to how often the code needs to be run and how clean the code should look for the next person who must support it.
Generally, using BETWEEN is just a bad idea when using dates that have a time component, @dougp . The >= AND < logic is much more concise. Especially as you can easily write things like DateColumn >= @Date and DateColumn < DATEADD(DAY, 1, @Date), rather than DateColumn BETWEEN @Date AND DATEADD(MILLISECOND, -3, DATEADD(DAY, 1, @Date)) (assuming DateColumn and @date is a datetime)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.