Select DateTime field based only on the date - sql

How can I query a DateTime field only based on the date ignoring the time in the field?
select * from TABLE_NAME
where DATE = '2020-01-08'

You can achieve by this way, demo in db<>fiddle
select * from TABLE_NAME
where CAST([DATE] AS DATE) = CAST('2020-01-08' AS DATE)

You can do date comparison:
mydatetime >= cast('2020-01-08' as date) and mydatetime < cast('2020-01-09' as date)
Or you can cast() your datetime to a date (which is shorter to write, but far less efficient, since it cannot take advantage of an index on the datetime column):
cast(mydatetime as date) = cast('2020-01-08' as date)

Related

Select today's date as default

I have a question which I'm not sure if possible or not but let's say I have a table with Employeeid, Date and AttendanceStatus i.e "Present"/"Absent".
I can get the status of all employees for all dates or for dates I specify in the query.
My question is that is it possible to get today's date by default so whenever I run the query it give me the data rows for today's instead of all the records from the database. I want to use default so that I don't have to change the date everyday.
Thanks in advance
Try this:
select * from TABLE_NAME where
where cast([Date] as date) = cast(getdate() as date)
Also, as already mentioned, you could create a view:
create view V_TABLE_NAME as
select * from TABLE_NAME where
where cast([Date] as date) = cast(getdate() as date)
You can apply where clause for current day;
select * from table where
Date > cast(getdate() as date) and
Date < DATEADD(day,1,cast(getdate() as date))
Or you can create a view;
create view v_table
as
select * from table where
Date > cast(getdate() as date) and
Date < dateadd(day,1,cast(getdate() as date))
Then query;
select * from v_table
Try This Logic
DECLARE #MyDate DATE
SELECT
*
FROM YourTable
WHERE DateField = ISNULL(#MyDate,GETDATE())
Here, If You are not passing any values in the Parameter #MyDate, it will take Current Date as Default. The following can also be used
DECLARE #MyDate DATE
SELECT
*
FROM YourTable
WHERE
DateField = CASE WHEN ISDATE(#MyDate) = 1 THEN #MyDate ELSE GETDATE() END

How to cast a datetime format to date as string in SQL Server

I am struggling to find a solution for the below issue.
date1 = 31-08-2017 12:10:00
I want to cast it as string and need to take date (31-08-2017) alone.
This is my SQL statement:
select *
from table_name
where cast(date1 as varchar) = '2017-08-30'
Here '2017-08-30' is string; when I ran the above select command it's showing o records as date1 is varchar but time also is included.
Can anyone tell me how to split date column alone as a string?
this will help you :-
select * from table_name WHERE CONVERT(varchar(23), [YourDateColumn], 121)= '2017-08-30 00:00:00.000'
above will accept both date and time
if you want to use only date then try this :-
select * from table_name WHERE CONVERT(varchar(10), [YourDateColumn], 20)= '2017-08-30 00:00:00.000'
If your date1's type is DateTime, Try this,
select *
from table_name
where CAST(date1 AS DATE) = CAST('2017-08-30' AS DATE)
This will help you.
select *
from table_name
Where CONVERT(VARCHAR(100),date1,102) = '2017.08.30'
date1 is column name value= '30-08-2017 12:10:00'
Output will be 30-08-2017

how to get before date Rates using sqlserver?

select rateperkg from K_FS_FeedMrpDetails where date = getdate()-1
I want to display rateperkg column before date values
Try this
get Yesterday
select rateperkg from K_FS_FeedMrpDetails where date = DATEADD(d,-1,GETDATE())
see this link for more details.
I think this will helps you
http://blog.sqlauthority.com/2008/08/29/sql-server-few-useful-datetime-functions-to-find-specific-dates/
This code will set time to zero.
declare #DaVal as datetime
--111 will return yyyy/MM/dd
--101 will return MM//dd/yyyy
set #DaVal = convert(datetime, convert(varchar(25), DATEADD(d,-1,GETDATE()), 111))
select rateperkg from K_FS_FeedMrpDetails where date = #DaVal
Try using DATEADD() function and cast it to Date to remove time part like below
SELECT rateperkg
FROM k_fs_feedmrpdetails
WHERE date = Cast(Dateadd(day, -1, Getdate()) AS DATE);
If your date column stores date and time then use the below condition
where date >=convert(date,getdate()-1)
and date < getdate()
else
where date = convert(date,getdate()-1)

How can I obtain only MSSQL rows that have been created since Jan 1st?

I have a script that writes database information to a csv based on a SQL query I wrote. I was recently tasked with modifying the query to return only rows where the DateTime field has a date that is newer then Jan. 1 of this year. The following query does not work:
$startdate = "01/01/2013 00:00:00"
SELECT Ticket, Description, DateTime
FROM [table]
WHERE ((Select CONVERT (VARCHAR(10), DateTime,105) as [DD-MM-YYYY])>=(Select CONVERT (VARCHAR(10), $startdate,105) as [DD-MM-YYYY]))"
The format of the DateTime field in the database is in the same format as the $startdate variable. What am I doing wrong? Is my query incorrectly formated? Thanks.
DECLARE #startdate datetime
SELECT #startdate = '01/01/2013'
SELECT Ticket, Description, DateTime
FROM [table]
WHERE DateTime >= #startdate
you can get year based records through DATEDIFF
DECLARE #startdate datetime = '01/01/2013';
SELECT Ticket, Description, DateTime
FROM [table]
WHERE DATEDIFF(YEAR,#startdate,DateTime) = 0 //It gives 0 if years are the same and DateTime is your column
You can get the start of the current year using:
DateAdd( year, Year( GetDate() ) - 1900, 0 )
The query then becomes:
select Ticket, Description, [DateTime]
from [Table]
where [DateTime] >= DateAdd( year, Year( GetDate() ) - 1900, 0 )
If you really don't want rows from January first then change the comparison from >= to >.
DECLARE #startdate datetime
SELECT #startdate = '01/01/2013'
SELECT Ticket, Description, DateTime
FROM [table]
WHERE DateTime >= #startdate
Interesting question that says as much about interpreting the requirement as about the SQL. When next year comes you will want to select next year's tickets. So it should be
SELECT Ticket, Description, DateTime
FROM [table]
WHERE datePart(yy,DateTime)=datePart(yy,getDate())
DECLARE #startdate datetime = '01/01/2013';
SELECT Ticket, Description, DateTime
FROM [table]
WHERE DATEDIFF(DD,#startdate,DateTime) >= 0

How do I convert hh:mm:ss to hh:mm in SQL Server?

How do I convert hh:mm:ss to hh:mm in SQL Server?
select Count(Page) as VisitingCount,Page,CONVERT(VARCHAR(8),Date, 108) from scr_SecuristLog
where Date between '2009-05-04 00:00:00' and '2009-05-06 14:58'
and [user] in(select USERNAME
from scr_CustomerAuthorities )
group by Page,Date order by [VisitingCount] asc
CONVERT(VARCHAR(5),Date, 108)-- Gets only HH:mm
In general, the set of timestamps is not well-ordered, this means you cannot get a "last" timestamp whose time part up to minutes is 2009-05-06 14:58.
In SQL Server, which keeps the time part of a datetime as a number of 1/300 second fractions after midnight, this "last" timestamp would be 2009-05-06 14:58:59.997, but this is not guaranteed to be compatible with future releases of with other TIMESTAMP storage methods.
That means you'll need to split your BETWEEN condition into two conditions, one of which being strict less than the next minute:
select Count(Page) as VisitingCount,Page,CONVERT(VARCHAR(8),Date, 108) from scr_SecuristLog
where Date >= '2009-05-04 00:00:00'
AND Date < DATEADD(minute, 1, '2009-05-06 14:58')
and [user] in(select USERNAME
from scr_CustomerAuthorities )
group by Page,Date order by [VisitingCount] asc
This solution will efficiently use indexes on Date
SELECT Convert(varchar(5), GetDate(), 108)
Using varchar(5) will automatically truncate the date to remove the seconds.
I dont think there is a built in function; usually do something like this
SET #time = '07:45'
SET #date = CONVERT(DATETIME,#time)
SELECT #date
SELECT LEFT(CONVERT(VARCHAR,#date,108),5)
For this specific need you should use the between method as noted by Quassnoi's answer. However, the general problem can be solved with:
select dateadd(second, -datepart(second, #date), #date)
One way would be to use the RIGHT() function to crop the Date. Something like:
RIGHT(CONVERT(VARCHAR(8),Date, 108),5)
This will only work if number of characters is constant e.g. there is a leading zero if applicable. (Sorry havn't got SQL server here to test).
A better way is to use the T-SQL datepart function to split and then re-concatinate the date parts so:
DARTPART("hh", CONVERT(VARCHAR(8),Date, 108))+":"+DARTPART("mi", CONVERT(VARCHAR(8),Date, 108))
References:
http://msdn.microsoft.com/en-us/library/ms174420.aspx
http://msdn.microsoft.com/en-us/library/ms187928.aspx
http://msdn.microsoft.com/en-us/library/ms177532.aspx
To get hh:mm format from today's date:
select getdate()
select convert(varchar(5),getdate(),108)
To get hh:mm format from any datetime column in a table data:
select date_time_column_name from table_name where column_name = 'any column data name'
select convert(varchar(5),date_time_column_name,108) from table_name
where column_name = 'any column data name'
select creationdate from employee where Name = 'Satya'
select convert(varchar(5),creationdate,108) from employee
where Name = 'Satya'
SELECT CAST(CAST(GETDATE() AS Time(0)) AS VARCHAR(5))