How to cast GETDATE() to be only time ignore day,month and year [duplicate] - sql

This question already has answers here:
Compare time part of DateTime data type in SQL Server 2005
(6 answers)
Closed 4 years ago.
I have a table for store takt-time. It's compound with "day_shift(1)" and "night_shift(0)" like below.
If I put current time(by using GETDATE() function ).
I need to know this current time is "day(1)" or "night(0)". my query like this.
SELECT [day_shift_flag]
FROM [company_working_time]
WHERE GETDATE() BETWEEN [start_time] AND[end_time]
But it can't found because a date received from GETDATE() isn't '1900-01-01'
How to compare a only time and ignore day,month,year.
What should I do?

You can cast a DateTime as a Time data type. For example:
SELECT
[day_shift_flag]
FROM [company_working_time]
WHERE
CAST(GETDATE() AS TIME) BETWEEN CAST([start_time] AS TIME) AND CAST([end_time] AS TIME)

By Following Way You can Ignore the day,month,year
SELECT [day_shift_flag]
FROM [company_working_time]
WHERE DatePart(HH,GETDATE()) BETWEEN DatePart(HH,[start_time]) AND DatePart(HH,[end_time])

Related

Find rows that have yesterdays date -- Invalid column? [duplicate]

This question already has answers here:
How to use alias column name in where clause in SQL Server
(5 answers)
Using new columns in the "where" clause
(5 answers)
Closed 20 days ago.
Goal: I am trying to get the rows in a table that have yesterdays date (but the original column is in datetime).
What I've tried:
I was able to figure out how to get yesterdays date from https://learnsql.com/cookbook/how-to-get-yesterdays-date-in-t-sql/
But I also had to cast the DateTime from the table to date to match that yesterdays date was in Date format. So the SO articles like SQL statement to select all rows from previous day did not work.
When I try to find the matching rows from my DB Table, it says:
Invalid column name 'YesterdayDate'
SQL:
SELECT cast(DateEnded as date) AS YesterdayDate
FROM [dbo].[V]
WHERE YesterdayDate = DATEADD(DAY, -1, CAST(GETDATE() AS date));
How can I correctly do this?
In fact don't do it that way. You want to avoid calling functions on columns in your WHERE clause as they can make the query unsargable i.e. unable to use indexes. Instead use the actual column, and rather than converting to a date use a datetime window of the previous day.
SELECT CAST(DateEnded AS date) AS YesterdayDate
FROM [dbo].[V]
WHERE DateEnded >= DATEADD(DAY, -1, CAST(GETDATE() AS date))
AND DateEnded < CAST(GETDATE() AS date);

Unexpected results when subtracting milliseconds in SQL [duplicate]

This question already has answers here:
DateTime2 vs DateTime in SQL Server
(17 answers)
Closed 3 years ago.
I'm trying to do some DateTime logic in SQL:
SET #DayEnd = DATEADD(MILLISECOND, -1, #BeginNextDay)
If, for instance, #BeginNextDay is '2019-02-04 00:00:00.000' and I'm performing the code above, I would expect #DayEnd to be '2019-02-03 29:59:59:999'. Unfortunately this isn't the case, the result is '2019-02-04 00:00:00.000'.
When I'm subtracting 12 milliseconds, the result is '2019-02-03 23:59:59.987'.
When I'm substracting 10 milliseconds, the result is '2019-02-03 23:59:59.990' as expected.
Can somebody explain to me what SQL is doing that gives me this (for me) unexpected result?
Don't do it! Just use:
SET #DayEnd = #BeginNextDay; -- if you even need this
And change your logic to be:
WHERE datecol < #DayEnd
rather than:
WHERE datecol <= #DayEnd -- or equivalently using BETWEEN
Don't fiddle with milliseconds to define time periods. Use >= and < to define the period.

getdate() function [duplicate]

This question already has answers here:
How to return only the Date from a SQL Server DateTime datatype
(46 answers)
Closed 6 years ago.
How can I display only date while using getdate() function?
When I am using
select getdate() as Today's Date
It is giving me:
2016-05-18 08:32:07.100
But I only need:
2016-05-18
How to get this?
Use convert. The first argument truncates the length, and the last specifies the format you want the date in:
SELECT Convert(varchar(10), getdate(), 103) as [Today's Date]
If you are using SQL Server 2012+, you may also want to look at FORMAT - which in my opinion is a bit nicer to read/use.
Example: SELECT FORMAT(GETDATE(), 'dd/MM/yyyy')
See this msdn link for more info

sql server query current date from database [duplicate]

This question already has answers here:
how to get current datetime in SQL?
(9 answers)
Closed 7 years ago.
I want to find few data from SQL Server that only query by current date. The purpose is to view today transaction only
Here is my code
SELECT * FROM dbo.Student.studentProfile
WHERE TransactionDate = curdate()
RESULT
Curdate is not a recongined built-in function name
If you are looking for the current date:
WHERE TransactionDate = cast(getdate() as date)
Or if you prefer ANSI standards:
WHERE TransactionDate = cast(CURRENT_TIMESTAMP as date)
Different sql implementations (ie SQL Server, Mysql, postgresql etc) can have different methods supported. For SQL Server the method you want to use is GETDATE() instead of CURDATE()
The documentation for this method is here: https://msdn.microsoft.com/en-us/library/ms188383.aspx
Try this.
select * from dbo.Student.studentProfile
WHERE TransactionDate >= dateadd(day,datediff(day,1,GETDATE()),0)
AND TransactionDate < dateadd(day,datediff(day,0,GETDATE()),0)

query to select data from this day [duplicate]

This question already has answers here:
In SQL Server, what is the best way to filter items for an entire day
(3 answers)
Closed 8 years ago.
I have a database table that has Datelogged columns from type Datetime (Sql server 2008 r2)
The Datelogged value is like this 2014-10-22 12:57:36.583
I want to do a query to select all the rows that its date is today (I care about year, month, and day) but not (hour, second.)
This is my query
string query = "SELECT * FROM Orders WHERE PrintTime is NULL WHERE Datelogged = #Datelogged";
but I didn't know what should I do to tell the query to compare just on year-month-day
Based on #Aaron Bertrand comment
It appears that it is better to use a Date Range from current day midnight (00:00:00) to < tomorrow at midnight.
Query copied from his comment.
DateLogged >= CONVERT(DATE, #DateLogged) AND
DateLogged < DATEADD(DAY, 1, CONVERT(DATE, #DateLogged))
Also see: Bad habits to kick : mis-handling date / range queries By Aaron Bertrand
(Old Answer)
You can use CONVERT(DATE, Datelogged) to get Date part without time.
"SELECT * FROM Orders WHERE PrintTime is NULL AND CONVERT(DATE, Datelogged) = #Datelogged"
Make sure you pass the parameter value using Date property in C# like:
cmd.Paramaters.AddWithValue("#Datelogged", DateTime.Today);// or DateTime.Now.Date
Also make sure to remove multiple WHERE from your query and use AND or OR to combine two conditions depending on your requirement.
Get the minimum date time range for current date and maximum date time range for today. Then, compare it with the logged date value.
Conversion operator on any table column adds extra conversion overhead and leads to inefficient use of index. Should be avoided when possible.
SELECT * FROM Orders
WHERE PrintTime is NULL
AND (Datelogged > dateadd(DD, -1, cast( GETDATE() as date)) AND Datelogged < dateadd(DD, 1, cast( GETDATE() as date)));