Select DATE part from TIMESTAMP in SQL Server? - sql

I have this part of a query made in POSTGRE and I want to change it for using it in SQL Server, but there is an error in DATE() function.
AND DATE(A.SERVERTIME) = DATE(B.SERVERTIME) + 1
SERVERTIME is TIMESTAMP datatype.
I want to know how to get the date part from SERVERTIME.

Assuming you want to add days;
AND cast(A.SERVERTIME as date)= cast(DATEADD(day, 1, B.SERVERTIME) as date)
Edit: As Larnu warned, timestamp can not be casted to date. However it can be casted to date if it goes through DATEADD function.
Hence it should be;
AND cast(DATEADD(day, 0, A.SERVERTIME) as date)= cast(DATEADD(day, 1, B.SERVERTIME) as date)

Related

How to get the records of last month in SQL Server?

I want to get the records of last month based on my table [Sales], variable "SoldDate" in SQL Server.
For example, if today is 29/09/2021, I want to get the data for 01/08/2021-31/08/2021
The "SoldDate" variable is in the format 2018-04-11 00:00:00.000.
I would really appreciate your help! I have no idea how to get this to work :(
You can use eomonth() :
SELECT DATEADD(DAY, 1, EOMONTH(SoldDate, -2)) AS StartDate, EOMONTH(SoldDate, -1) AS EndDate
The best way to search through an indexed table is to calculate what date range you need, and filter on that. You can use DATEADD and EOMONTH for this. Note that since you have a time component, you cannot just use EOMONTH
SELECT *
FROM YourTable
WHERE SoldDate >= DATEADD(day, 1, EOMONTH(GETDATE(), -2))
AND SoldDate < DATEADD(day, 1, EOMONTH(GETDATE(), -1))
EOMONTH gives you the end of a month, the second parameter denotes how many months to shift.
Note correct use of the half-open interval >= AND <, this dealls correctly with the time component.

SQL date comparision in yyyy/mm/dd

Not being that great at sql, i've reached my limit.
I have a date in the yyyy/mm/dd format and i need to get all records "from a week ago"
I think i need some conversion stuff to be done cause this
d.date_begin >= DATEADD(day,-7, GETDATE())
is not working :), i'm TERRIBLE at convert and data type..
This will work if you want records from 7 days ago up to and including today's records
CAST(d.date_begin AS DATE) >= CAST(DATEADD(day,-7, GETDATE()) AS DATE)
where DATEDIFF(month,Your_date_column,getdate()) < 3
SQL server 2012 onwards, if date_begin is of datatype date
where d.date_begin >= cast(DATEADD(day,-7, GETDATE()) as date)
This will get anything in the last 7 days, regardless of time
You should store date/time values using native formats. Ok, sometimes we cannot. But you can easily convert your values to the correct format:
where cast(replace(d.date_begin, '/', '') as date) >= DATEADD(day, -7, GETDATE())
I should note that SQL Server is pretty good about conversions, so your initial code should not generate any errors -- unless you have unusual internationalization settings.
Or, actually, a better way to do this is to convert the current value to a string:
where d.date_begin >= format(dateadd(day, -7, getdate()), 'yyyy/MM/dd')
This is better because it is "sargable", meaning that SQL Server can use an index on the column if available.
SELECT * FROM tbl_name
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Don't manipulate d.date_begin. Making calculation on a column while comparing can give bad performance. You should manipulate getdate() to get the same format as d.date_begin. In this case it works because the format is yyyy/MM/dd - the comparison will give the same result as if both columns were date columns.
WHERE
d.date_begin >= convert(char(10),DATEADD(day,-7, GETDATE()), 111)

How to get timestamps correspond to the current days date? With Microsoft SQL

The number of rows in the A table, where the date value of the timestamps in the [time_created] and / or [time_updated] fields correspond to the current day's date
You can use the CONVERT() function.
CONVERT(date, YOUR-COL-NAME-HERE)
So if you are looking for rows with today's date, you should have something like:
WHERE CONVERT(date, YOUR-COL-NAME-HERE) = CONVERT(date, GETDATE())
GETDATE() returns the current date as a datetime. You can use convert to extract the date part from it:
SELECT *
FROM mytable
WHERE CONVERT (DATE, GETDATE()) IN
(CONVERT(DATE, time_created), CONVERT(DATE, time_updated))

Convert nvarchar date to datetime

I have dates stored in a sql server database as nvarchar but I need to create a report and pull out data from the last day base on the date.
I use this when the data type is a DateTime:
SELECT *
FROM [table]
WHERE timein >= DateAdd(hh, -24, GETDATE())
I think I need to convert the GETDATE() -24 to a string to compare it to the db
The format needs to be like this:
April-30-15
Can anyone help me create a query that will select records for the past 24 hours using this date format?
Convert your timeIn string to a date and compare using dates not strings. If you replace the hyphens with spaces it will be able to cast to a date. I assume you want values since the start of previous day (ignoring the current time) so I cast that to a date also.
SELECT *
FROM [table]
WHERE cast(replace(timein, '-', ' ') as date) >= cast(DateAdd(dd, -1, GETDATE()) as date)

Get tomorrows date

I am trying to get tomorrows date in a sql statement for a date comparison but it is not working.
Below is my code:
select *
from tblcalendarentries
where convert(varchar,tblcalendarentries.[Start Time],101)
= convert(varchar, GETDATE() +1, 101)
To get tomorrows date you can use the below code that will add 1 day to the current system date:
SELECT DATEADD(day, 1, GETDATE())
GETDATE()
Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computer on which the instance of SQL Server is running.
DATEADD(datepart , number , date)
Returns a specified date with the specified number interval (signed integer) added to a specified datepart of that date.
So adding this to your code in the WHERE clause:
WHERE CONVERT(VARCHAR, tblcalendarentries.[Start Time], 101) =
CONVERT(VARCHAR, DATEADD(DAY, 1, GETDATE()), 101);
First off, GETDATE() will get you today's date in the following format:
2013-04-16 10:10:02.047
Then using DATEADD(), allows you to add (or subtract if required) a date or time interval from a specified date. So the interval could be: year, month, day, hour, minute etc.
Working with Timezones?
If you are working with systems that cross timezones, you may also want to consider using GETUTCDATE():
GETUTCDATE()
Returns the current database system timestamp as a datetime value. The database time zone offset is not included. This value represents the current UTC time (Coordinated Universal Time). This value is derived from the operating system of the computer on which the instance of SQL Server is running.
Try the below:
SELECT GETDATE() + 1
This adds one day to current date
Specify size of varchar in convert()
where convert(varchar(11),tblcalendarentries.[Start Time],101) = convert(varchar(11), GETDATE() +1, 101)
I would write:
where
DATEADD(day,DATEDIFF(day,0,tblcalendarentries.[Start Time]),0) =
DATEADD(day,DATEDIFF(day,0,GETDATE()),1)
This avoids converting dates to strings entirely, whilst also removing the time portion from both values.