SQL Compare INT to GETDATE - sql

I'm having trouble figuring this out and have tried everything on here. I know it's simple...
Our dates are stored as int in the table. EX 20130409. How do I get SQL to return dates that are less than today's date?
I've been using different combinations of cast and convert but keep getting either overflow errors or conversion failed.
Here is some recent code:
SELECT
DBO.SPSYS07.LOC_CODE,
CONVERT(DATETIME,CAST(DBO.SPSYS07.REQ_D_DATE AS CHAR(8)),101) AS [CONVERTED_REQ_DATE],
DBO.SPSYS07.REQ_D_DATE
FROM DBO.SPSYS07
WHERE SPSYS07.SHIP_DATE <= convert(int,convert(varchar(8),getdate(),112))

Try declaring today's date and using that
Declare todaysDate date
Set todaysDate = getdate()
Then convert todaysDate to a string, then remove '-' between year, month and days, and then....
Select..... Where shipdate <= todaysDate

You just need to change the '20130101' for your int value.
DECLARE #date date
SELECT #date = CONVERT(date, CAST('20130101' AS CHAR(12)), 112)
SELECT * FROM yourTable where #date < getdate()
regards

Related

SQL Convert data time format (yyyy-mm-dd 00:00:00.000) to yyyymmdd as int

It must be very simple, but I don't know SQL language very well.
I need to filter data by date which is in this format:
How to do it right to filter data this way?
FROM [TableName] where
FileDate>=20220505
I've already tried the command LEFT and CAST but with no success
Something like this may work:
declare #now Datetime = getdate();
declare #intNow int = cast(cast(datepart(year, #now) as varchar(4)) + RIGHT('00'+CAST(datepart(month, #now) AS VARCHAR(2)),2) + RIGHT('00'+CAST(datepart(day, #now) AS VARCHAR(2)),2) as int)
Although if you have your date to check against in the right format e.g. using:
declare #dateToCheck Datetime = cast(cast(20220505 as varchar) as datetime)
And then
FileDate>= #dateToCheck
it should work
You can create an integer representation of your datetime by multiplying and adding the date parts:
year * 10000 20220000
month * 100 500
day 5
-------------------------
20220505
...
FROM [TableName]
WHERE (DATEPART(year, [FileDate]) * 10000) + (DATEPART(month, [FileDate]) * 100) + (DATEPART(day, [FileDate])) >= 20220505
However I'd still look into fixing the condition input format instead.
Credit to #Rangani in Yesterday's date in SSIS package setting in variable through expression for "multiply and add instead of string concat" trick

How to convert datetime to date without using convert function in sql server

I'm trying to convert datetime to date using convert function. But it is taking large as there is huge data in the table.
Is there any other way to do this without using convert function in less time.
Query:
Select *
from address
where convert(date,record-created_date) between '6/29/2016' and '6/30/2016'
You can simply remove CONVERT function as following query, so no time is spent for converting, but instead you have to add time to your [record-created_date] column in the WHERE clause:
SELECT *
FROM [address]
WHERE [record-created_date] BETWEEN '6/29/2016 00:00:00:000' AND '6/30/2016 23:59:59:999'
With SQL 2008 you can use CAST:
select cast(record-created_date as date)
In older versions, you can use DATEADD/DATEDIFF:
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, record-created_date))
You can define 2 constants in advanced, thus avoid using convert function on the column:
declare #min_date datetime
declare #max_date datetime
set #min_date = convert(datetime, '06/29/2016', 101)
-- Max date needs to be the next date of your end date
set #max_date = convert(datetime, '06/30/2016', 101) + 1
Select *
from address
where record-created_date >= #min_date
and record-created_date < #max_date
Just omit the conversion and add one day to the end date:
where record_created_date between '6/29/2016' and '7/1/2016'
This is interpreted as <= '7/1/2016 0:00', so use this short form only if is okay that midnight of the following day is included. Otherwise use the long form:
where record_created_date >= '6/29/2016' and record_created_date < '7/1/2016'
...or add the time:
where record_created_date between '6/29/2016' and '6/30/2016 23:59:59.999'
Btw., I think it is better to use the date format '2016-06-29' for hardcoded dates.
Anyway the main reason for being slow is most likely a missing index for this column.

Substract Date from GETDATE() to get "time since"

I have a elements that have values in DATETIME format in column 'Date Created'.
I would like to be able to extract value how long time ago it was from time at which query is executed (as a reference take GETDATE())
When executing this:
UPDATE #LPs
SET LastUsed = (GETDATE() - DateCreated)
I am getting 1900-01-01, I believe that is due to SQL Server limitations for minimal DATETIME.
How should I right is so I can get values like "2 years 3 months", "2 months" (any datetime format)?
Sql server does not have a built in function that returns date differences as a formatted string like you want, but it does have a DATEDIFF function that will return the number of date parts between two dates:
DECLARE #Now datetime = GETDATE(),
#Date datetime = GETDATE() + 5 -- five days from now
SELECT #Now as Now,
#Date as Date,
DATEDIFF(Day, #Now, #Date) As DateDiff
You can get the number of months between the dates and calculate the string yourself using the modulu operator
SELECT DATEDIFF(day, DateCreated, GETDATE())
FROM table
maybe it works

How to get month/day to compare with another month/day without year

I need to select month/day to compare with another month/day to determine which day is greater. For example, to compare 2/28 with 3/18. What would be the date format I can use to compare the days.
In SQL Server, you can convert dates to strings using the convert function:
https://msdn.microsoft.com/en-us/library/ms187928.aspx
The format you want to use is mm/dd/yy, which is style 1 on the convert function. Then you can simply take the left 5 characters:
select left(convert(char(8),YourDate,1),5) as mm_dd
If you're only trying to display the month and day, you can use this:
DECLARE #myDate DATETIME2 = '02/28/2015'
SELECT CAST(MONTH(#myDate) AS VARCHAR(2)) + '/' + CAST(DAY(#myDate) AS VARCHAR(2))
SELECT LEFT(CONVERT(VARCHAR,#DATE,1),5)
You can use the format function?
select format(getdate(),'dd/MM')
Try using the DATEPART function to get the day of year for your dates.
DECLARE #StartDate DATETIME = '20150130';
DECLARE #EndDate DATETIME = '20141225';
SELECT
#StartDate AS StartDate,
DATEPART(DY, #StartDate) AS StartDy,
#EndDate AS EndDate,
DATEPART(DY, #EndDate) AS EndDy;

How to filter only the date from a string stored in a varchar

Ii have values stored in the SQL Server in the following manner : 02-Jul-12 12:00:00 AM here the time and minutes, seconds can be anything like 02-Jul-12 12:15:52 PM ,02-Jul-12 6:02:12 AM so on.
I want to have a where condition which will omit the time and take the data based on the date like the following where some_Date='02-Jul-12'
How would I do this?
SELECT * FROM whatever WHERE some_Date LIKE '02-Jul-12%';
If you are on SQL2008 or later, you can cast your DATETIME to DATE.
See this post: http://blog.sqlauthority.com/2012/09/12/sql-server-get-date-and-time-from-current-datetime-sql-in-sixty-seconds-025-video/
But in a WHERE-clause it is better to search between dates, like this:
DECLARE #startDate DATETIME = '02-Jul-2012'
DECLARE #endDate DATETIME = DATEADD(DAY, 1, #startDate)
SELECT * FROM [table] WHERE [some_Date] BETWEEN #startDate AND #endDate
SELECT * FROM dbo.tbl_MyTable
WHERE
REPLACE(CONVERT(VARCHAR(9), DateTimeValueColumn, 6), ' ', '-')='02-Jul-12'
or
On chage in code is instead of using getdate function voncert you datestring in datetime format and do compare this follow query will work for you
SELECT * FROM dbo.tbl_MyTable
WHERE
CAST(CONVERT(CHAR(10), DateTimeValueColumn, 102) AS DATE) =
CAST(CONVERT(CHAR(10),GETDATE(),102) AS DATE)
If you are storing dates as characters -- which is not recommended -- you should at least use ISO format: YYYY-MM-DD hh:mm:ss. This makes the date useful for sorting and comparisons ("<" works, ">" works, "between" works as well as equals).
To extract the date, you can then use left(datestr, 10). In your format, you would use:
where left(datestr, 9) = '01-Jan-13'
If you are storing the fields as a datetime or smalldatetime, you may think they are stored as a string. They are not. They are stored as some number of days since some particular date, with day parts stored as fractional days. If you are using SQL Server 2005 or greater, then the best way is:
where cast(datetime as date) = '2013-01-01' -- I recommend ISO formats, even for constants. '20130101' is even better
To select rows with today's date (not time)
select * from myTable where datediff(dd, dateColumn, getdate()) = 0