SQL Server: How to query date in a certain format - sql-server

I am pretty new to SQL and hope someone here can help me with the following:
I have a table in SQL Server with one column storing dates and I have a simple stored procedure to select data from this table.
Currently it fetches the date including hours and minutes.
How can I achieve that it fetches the date in format yyyy-mm-dd (without the hours and minutes) ?
My column is called "logDate" and my table is called "logTable".
Thanks for any help with this, Tim

If you only want to store the date, convert the column to a date type, not a datetime.
If you want to keep the date and time in the data, but just display the date
select convert(date, logDate) from logTable
If you want to return the date as a string, use convert
select convert(varchar(10), logDate, 120) from logTable

Related

Creating SQL views in VBA

I am trying to create a view on a table called petients in my database. The table has five columns. One of them is the column which I want to keep patient admitted date. It data type is datetime so I want to create a query that filters the data in this table based on current date. For example I want create a view that shows only details of petients who have been recorded on the current day.
Here is my code:
CREATE VIEW [dbo].[recent petients]
AS
SELECT petient_id, name, age, contact
FROM [petients]
WHERE [date] = 'date.Today'
I am getting an error saying that failed to convert date to string. Can you help me to solve it, or where is my code wrong?
Your code looks like SQL Server code. If so, I would recommend:
SELECT petient_id, name, age, contact
FROM [patients]
WHERE [date] = CONVERT(date, GETDATE());
As a note: This version is much better than DATEDIFF() because it allows the use of an index on patient([date]).
If the "date" column has a time component, you can use:
WHERE CONVERT(date, [date]) = CONVERT(date, GETDATE())
Note that this is also index-safe in SQL Server.
I'm assuming you are using Transact-SQL from Microsoft SQL Server, but you should specify the sql dialect you are using.
Since the datetime field type generally includes also a time, it is better to use the DATEDIFF function: https://learn.microsoft.com/it-it/sql/t-sql/functions/datediff-transact-sql?view=sql-server-ver15
In your case, to consider only the record where date=today, the difference in days must be zero:
--SQL QUERY
WHERE DATEDIFF(day, GETDATE(), [date]) = 0
day identifies the element you want to consider the difference. A list of names or abbreviations can be found in the link
GETDATE() returns now datetime
2nd and 3rd arguments are the dates you want to make the difference between

Comparing dates with current date in Sql server

I have a table which has list of some events with dates. I am trying to write a stored procedure that will return only the upcoming events.
I have written the following query in the stored procedure:
SELECT *
FROM Events
WHERE tDate >= (select CAST(GETDATE() as DATE))
But this is not returning correct result. This is also showing results that have dates less than current date. How to write a query that will return all the events that have date equal or greater than today's date.
Edit: Dates that have been entered on the table have the format yyyy/dd/mm and getdate() returns date in the format yyyy/mm/dd. I think this is causing the problem. Dates that have been entered into the table has been taken using jquery date picker. Any solution to this problem?
Not sure why you have an additional select
SELECT *
FROM Events
WHERE tDate >= CAST(GETDATE() as DATE)
your DATE data is incorrectly stored within Sql Server. When your application passes the string '2015-09-04' and you save that your date column, it is saved as 4th Sept 2015 and not 9th April 2015. Hence your query returns such rows as they are greater than GETDATE().
Example
DECLARE #D VARCHAR(10) = '2015-09-04'
SELECT CONVERT(VARCHAR(20),CONVERT(DATE,#D),109)
you need to fix your data and then use a CONVERT with style when saving dates in your table from application, using something like this. CONVERT(DATE, '20150409',112)
DECLARE #D VARCHAR(10) = '20150409'
SELECT CONVERT(VARCHAR(20),CONVERT(DATE,#D,112),109)
Refer these threads for more info:
Impossible to store certain datetime formats in SQL Server
Cast and Convert

Select condition on date for datetime field

I want to impose date condition on a date time field in SQL Server.
The datetime field is like this 2011-01-19 17:57:18.350 and when I execute below query it yields no results.
select top 1000 *
from [dbo].[RouteState]
where convert (date, logtime, 101) = '12-01-2015'
Can someone help me what's going wrong here?
There is no need to convert the datetime column to anything. Use a closed-open interval instead and change the format of your string literal to yyyymmmdd to make sure that SQL Server will interpret the date value in the same way regardless of regional and date format settings.
select top 1000 *
from [dbo].[RouteState]
where logtime >= '20150112' and
logtime < '20150113';
For more info on casting a column to date you can have a look at Cast to date is sargable but is it a good idea?

sql server date stored in table comparing with the system date

really struggling to convert the below date format 10.11.2013 in the format
of the system date.
I have a request to compare column data with the system date.
In the column the date is storing as DD.MM.YYYY
Or we need to convert system date (getdate()) to the date values stored in the database table.
Thanks in Advance
Regards,
Dev
try this:
convert system date (getdate()) to the date values stored in the
database table.
Gives formats as DD.MM.YYYY
select CONVERT( CHAR(10),GETDATE(),104) -- outputs 11.09.2014
with Your Column
select CONVERT( CHAR(10),MyColumn,104)

SQL Server 2008 - date format issue while inserting

I am trying to insert date into a table but the date and format of inserted date is messed up. The datatype in the table is Date. My insert script is as below.
insert into Trans(ID, TDate, Description)
values(1, CONVERT(datetime, 25-02-2012, 101), 'Opening')
I am trying to insert in dd/MM/yyyy format and I want it in the same format in my table. But in my table the date is 1894-07-22 !!
I want the date to be inserted exactly as the format I wish and I want to see the inserted date as 25-02-2012 in the table.
What is wrong here ? Can somebody help ?
Try CONVERT(datetime ,'25-02-2012', 103)
use some single quotes around the value
choose your format accordingly (101 is US, meaning mm/dd/yyyy)
see http://msdn.microsoft.com/en-us/library/ms187928(v=sql.100).aspx for more details
you should use single quotes around your date. If you want date format of dd/mm/yyyy format then you will want to use the convert(datetime, '25-02-2012', 103)
insert into Trans(ID,TDate,Description)
values(1,CONVERT(datetime,'25-02-2012',103),'Opening')
if you use convert(varchar, getdate(), 101) the format of the date will be mm/dd/yyyy.
There are several helpful links to use as a reference for datetime conversions:
How to format datetime & date
Tips & tricks SQL Server Date formats
insert into emp
(EMPNO,ENAME,DEPTNO,JOB,SAL,COMM,MGR,HIREDATE)values
(7839,'KING',10,'PRESIDENT',5000,NULL,NULL,'17-11-81')

Resources