I have a database that has startDate and endDate. I am trying to run this simple script to find and replace certain dates. Here is my script:
SET startDate = '2012-10-11 07:00'
AND
endDate = '2012-10-13 20:00'
where startDate = '2012-10-12 07:00'
AND
endDate = '2012-10-14 20:00'
I run it and receive this error:
Incorrect syntax near '='.: SET startDate => '2012-10-11 07:00' AND endDate = '2012-10-13 20:00' where startDate = '2012-10-12 07:00' AND endDate = '2012-10-14 20:00'
I really have no idea why it's not running correctly, but hopefully one of you clever bod's here will be able to point out my incredibly dumb mistake!
Here's hoping! =0)
you need to separate your columns to update with a comma rather than AND:
UPDATE YourTable
SET startDate = '2012-10-11 07:00',
endDate = '2012-10-13 20:00'
WHERE startDate = '2012-10-12 07:00'
AND endDate = '2012-10-14 20:00'
You should also use a culture insensitive date format like 'yyyyMMdd hh:mm:ss', e.g. your date 2012-10-11 07:00, would be interpretted as 11th October by some cultures, and 10th November by others.
Example on SQL Fiddle of why not use yyyy-mm-dd dates
Related
I am needing to replace the last four characters in a column.
It is currently a year in a char format and I need to replace with the current year.
The code I am using now is successful in removing the old year but is failing to input the new year that is needed.
My Current Code:
DECLARE #NewYear as Char
SELECT #NewYear = cast(YEAR(GETDATE()) as char(4))
UPDATE MyTable
SET ENDDATE = (substring(ENDDATE, -5, len(ENDDATE)) +#NewYear)
WHERE EndDate < StartDate
Original Value - 01/01/2017
Result - 01/01/
Desired Result - 01/01/2020
This is being used in SAS Proc SQl pass through - ANSI Standard SQL
Any help would be appreciated.
Thank you!
There is a problem in the declaration of your variable. It should be declared as char(4) - otherwise it defaults to char(1) and ends up with value '2' instead of 2020.
Also, you can use left instead o substring.
Here is one way to do it:
DECLARE #NewYear as Char(4)
SELECT #NewYear = CAST(YEAR(GETDATE()) as char(4))
UPDATE MyTable
SET ENDDATE = LEFT(ENDDATE , len(ENDDATE) - 4) + #NewYear
WHERE EndDate < StartDate
Note there is little benefit using a variable here. Very likely, the database will optimize the expression and not do the getdate()-based computation for each row.
UPDATE MyTable
SET ENDDATE = LEFT(ENDDATE , len(ENDDATE) - 4) + cast(YEAR(GETDATE()) as char(4))
WHERE EndDate < StartDate
I would suggest:
UPDATE MyTable
SET ENDDATE = STUFF(ENDDATE, 7, 4, DATENAME(year, GETDATE()))
WHERE EndDate < StartDate;
The WHERE clause makes no sense, because you are doing date comparisons.
Note that variables are not helpful here. You don't have to convert anything to a string, if you use DATENAME(), because it returns a string.
I thought for a moment they might be stored as dates, but you would never get 01/01/ as a valid date.
Suppose if my web service has parameter year with value 201718 as string. Then in my query, I want to find something in between 01-04-2017 and 31-03-2018. The date is a datetime in the database. In my query I will use something like this
select *
from table
where date >= '01-04-2017' and date <= '31-03-18'
How should I change the format to accept date as '31-03-18'?
When representing a date using strings in SQL Server, it's best to use yyyyMMdd.
For datetime values, it's best to use yyyy-MM-ddTHH:mm:ss.
The reason for this is that SQL Server will always treat these formats the same way, regardless of local settings of the server.
However, working with string representations of date/datetime is still a bad idea.
So suppose you have a stored procedure that will get the Years as string, what you want to do is to get date values from this string. Something like this:
CREATE PROCEDURE stp_SelectByYears)
(
#Years as char(6)
)
AS
DECLARE #DateFrom date, #DateTo date;
SELECT #DateFrom = DATEFROMPARTS(CAST(LEFT(#Years, 4) as int), 4, 1),
#DateTo = DATEFROMPARTS(CAST(LEFT(#Years, 2) + RIGHT(#Years, 2) as int), 3, 31)
SELECT *
FROM table
WHERE date >= #DateFrom and date <= #DateTo
GO
Please note that datefromparts was introduced in 2012 version, so if you are working on an older version you need to replace it with cast:
SELECT #DateFrom = CAST(LEFT(#Years, 4) + '0401' as date),
#DateTo = CAST(LEFT(#Years, 2) + RIGHT(#Years, 2) +'0331' as date)
I'm creating a function were I provide 3 inputs #FiscalYEar, #StartDate, #EndDate, I also declare a DATE parameter that the year will be -1 of #FiscalYear
SET #fyLowerBound = OCT 1 OF (#FiscalYear - 1)
how do I properly write the SET statement to make it work?
This should work:
DECLARE #FiscalYear INT = 2014,
#fyLowerBound DATE;
SET #fyLowerBound = CAST(CAST((#FiscalYear - 1) AS CHAR(4)) + '1001' AS DATE)
SELECT #fyLowerBound;
This gives 1st October 2013.
The premise being creating a string date in the format yyyyMMdd, in SQL Server this is the only culture insensitive date for DATETIME (yyyy-MM-dd will work for DATE), you then cast that string to a date (or datetime whatever your preference).
So the first step is to turn your integer date into a CHAR(4), you can then create october 1st of this year by concatenating '1001'. You now have a string that will be cast to a date.
SET #fyLowerBound = DATEADD(yy, -1, #FiscalYear)
This will give you a date that's a year less than #FiscalYear. Although I'm not entirely sure this is what you need, given that 'OCT' in your original statement.
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
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