0
I need to make a SELECT in my SQL Server 2008 database and I’m not getting it.
I need to convert a date to the formatted one yyyy/MM/dd HH:mm:ss
, however I need to convert in any way that works in any SQL Server from 2008.
0
I need to make a SELECT in my SQL Server 2008 database and I’m not getting it.
I need to convert a date to the formatted one yyyy/MM/dd HH:mm:ss
, however I need to convert in any way that works in any SQL Server from 2008.
0
This code below is fully functional. Note that the Convert is for varchar, but the type Datetime receives varchar values without problems.
select convert(varchar,getdate(),121)
Another thing you can see is formatting dates per session, within the sql server command
SET DATEFORMAT dmy;
0
If your field is DateTime
you don’t need to make conversions to varchar
to place a WHERE
, just send the date in format 'yyyyMMdd hh:mm:ss'
that SQL Server will recognize regardless of version or DATEFORMAT
that is on the server.
If the field is only of the date type, just be in the format 'yyyyMMdd'
Example:
SELECT * FROM TABLE WHERE Campo > 0 AND DataHora = '20160315 16:50:25'
Or, if the field is date only:
SELECT * FROM TABLE WHERE Campo > 0 AND Data = '20160315'
The problem with queries involving date is that you should remember that if the type includes date and time, you will only get the result of equality only if the date and time are equal. If you want to do a search only on the independent date of the time, you will have to do a WHERE with a track, for example:
SELECT * FROM TABLE WHERE Campo > 0 AND DataHora >= '20160315' AND DataHora < '20160316'
0
SELECT * FROM TABLE WHERE Campo > 0 AND Convert(varchar,DataHora,103) = '2016-03-15 16:50:25'
Browser other questions tagged sql sql-server
You are not signed in. Login or sign up in order to post.
This here might solve: Problems to convert data mon dd yyyy hh:mm in what format the date comes?
– rray
my date on the seat is saved like this: yyyy-mm-dd hh:mm:ss and it comes a normal date dd/mm/yyyy hh:mm:ss
– Phiii_Cunha
would be able to post your SELECT ?
– David
@Phiii_cunha, processing the data in your application would not be an option?
– Rubico
I treated my return to pass yyyy-mm-dd hh:mm:ss ... it worked, but I wanted to pass yyyy/MM/dd HH:mm:ss to maintain the standard. SELECT itself is quite simple SELECT * FROM TABLE WHERE Field > 0 AND Convert(varchar, Datahora, 120) = '2016-03-15 16:50:25'
– Phiii_Cunha