Using REPLACE to change datetime with SET - sql

I haven't used this website before and am very new to SQL so please excuse any errors or if this question has been raised incorrectly - I have been asked to do something that is well outside of my remit and skill level!
I have many rows of data with a particular column - COLUMN - which has a DATETIME datatype, in a particular table - let's call it _data, and the data within it looks like this: 2019-11-12 17:00:00.000
I want to replace the YY-MM-DD portion of all of the columns in this table with today's date, 2019-11-28.
I am trying this code:
update DATABASE.dbo._data
set COLUMN = REPLACE(COLUMN,'2019-11-13','2019-11-28')
Which executes and informs me that every row in DATABASE has changed, but when I look at the data with a SELECT statement, it is the same as it was before. Why is this, do I need to do something different because the datatype is DATETIME? Google did not elucidate me and neither did w3schools or the other questions I searched for on here, but that's probably because I have no idea what I am doing.
I am using SQL SERVER 2012 on a Windows Server 2012 R2 box and I am running this code with SQL Server Management Studio v18.4.

DATETIMEFROMPARTS is your best friend here.
DECALRE #now DATETIME = GETDATE();
UPDATE DATABASE.dbo._data
SET COLUMN = DATETIMEFROMPARTS(
YEAR(#now),
MONTH(#now),
DAY(#now),
DATEPART(hour, COLUMN),
DATEPART(minute, COLUMN),
DATEPART(second, COLUMN),
DATEPART(millisecond, COLUMN));

This seems like a really add logic, but seems like the "easiest" method would be to use DATEADD and DATEDIFF:
UPDATE dbo.YourTable
SET DateTimeColumn = DATEADD(DAY, DATEDIFF(DAY, DateTimeColumn, GETDATE()), DateTimeColumn);

Related

Mistyped dates in SQL

I was wondering if there is a way to detect mistyped dates in SQL in a general sense.
For example:
Order1 - 2014
Order2 - 2104
Order3 - 2041
I am guessing a form of case statement using wildcards would do the trick. But I am kind of a beginner in that regard.
EDIT - Sorry, for clarification, my column actually contains YYYY-MM-DD. However, I am only concerned with the year formatting. The datatype is Date for TSQL.
Thank you!
Since your data is already in a date column you don't need to worry about whether the value is a valid date or not.
So basically you are looking - from what I can tell - to see if the date falls in an expected range.
So you might want to do something like
SELECT * FROM MYTABLE WHERE MYDATE NOT BETWEEN '01/01/1900' AND '12/31/2020'
(obviously you can make the range whatever values you want, either static values or values stored in an options table)
You could take this a step further and validate it on a dynamic range, something like this:
SELECT * FROM MYTABLE
WHERE MYDATE NOT BETWEEN DATEADD(YEAR, -5, GETDATE()) AND DATEADD(YEAR, 5, GETDATE())
A further step would be to validate it in a range, based on some other field in the table, such as a created-on date for the record, like this:
SELECT T.* FROM MYTABLE T
WHERE T.MYDATE NOT BETWEEN DATEADD(YEAR, -5, T.CREATEDON) AND DATEADD(YEAR, 5, T.CREATEDON)
This gives you a bit more flexibility, because the "incorrect" dates may actually be valid at another point in time.
This should give you some starting points from which you can flesh out your exact needs.
assuming you are using a varchar field (through the comments) try,
Where your_column not like '200_' or your_column not like '201_'
if you were using a int for year you could use a range
Where your_column not between 2000 and 2014

Convert float (YYYY) to datetime

I'm relatively new to SQL, so please forgive me if this is a dumb question. I've been trying for too long now to get this to work.
I have a column in table A that is a float column called ConstructionYear. It is populated with a simple 4 digit year (i.e. 2010, 2005, 1972, etc.). I need to populate table B's YearBuilt datetime field using these years. I've searched and searched and tried all sorts of different combinations of convert() and cast() that I've found online, but it's not working.
What I would like to happen is this:
'2008' -> '2008-01-01 00:00:00.000'
'2005' -> '2005-01-01 00:00:00.000'
'1986' -> '1986-01-01 00:00:00.000'
Instead of what is currently happening (using CAST(ConstructionYear AS DATETIME)):
'2008' -> '1905-07-02 00:00:00.000'
'2010' -> '1905-07-04 00:00:00.000'
'1984' -> '1905-06-08 00:00:00.000'
EDIT: Solution: cast(convert(varchar,#ConstructionYear) AS DATETIME)
So my problem had 2 main causes (other than me being new to sql).
I didn't know about the 1900 epoch that SQL Server uses for datetime. I could tell something was going on because of all teh 1905 datetimes i saw, but i didn't know that it was taking my 2005 year and counting it as days from 1900.
The year 1753. Why is 1753 the earliest year we can use? I probably had the right syntax at some point before i posted my question here, but it didn't run because my data had some years predating 1753. I assumed the error was with my code.
Check this example:
DECLARE #ConstructionYearas FLOAT
SET #ConstructionYear = 2012
SELECT FloatToDatetime = CAST(convert(varchar(4),#ConstructionYear) as datetime)
It will output:
2012-01-01 00:00:00.000
Basically use:
CAST(convert(varchar(4),#ConstructionYear) as datetime)
where #ConstructionYear is your Float variable
Hope it helps!
Besides #Ghost's answer, if you are using SQL SERVER 2012
You can use
DATEFROMPARTS(ConstructionYear, 1, 1)
reference: http://msdn.microsoft.com/en-us/library/hh213228.aspx
This should WORK but there's probably a better way
CAST(CAST(ConstructionYear as nvarchar(4)) + '-01-01' as datetime)
What your query is doing is taking the number of days you are providing and adding it to SQL servers epoch, January 1st, 1900, which gives the results you saw
DECLARE #y FLOAT;
SELECT #y = 2012;
SELECT DATEADD(YEAR, #y-1900, 0);
Why did you think the numeric value 2012 translated directly to a number of years? When in fact in SQL Server a numeric value translates to the number of days since 1900-01-01. So the following should yield the same results:
SELECT CONVERT(DATETIME, 2012);
SELECT DATEADD(DAY, 2012, '19000101');
As others have suggested, I strongly suggest you to store this data correctly. While you could use something a little safer like SMALLINT, why not use DATE? This comes with built-in validation, ability to use all kinds of date/time functionality directly without conversion, and doesn't have any of the inherent rounding problems you might experience with an approximate data type like FLOAT. If you're storing a date, use a date data type.

Date without the time

I'm working with SQL Server 2005.
I have a column called purchase_time of type datetime. How do I select this column with the time part - just the date.
Thanks,
Barry
EDIT:
Would it be safe to get the datetime and split it via Python on the first space, or is this format locale dependant?
In versions < 2008 (which, based on other comments to some of the answers, I believe you are running), the most efficient way is to keep it as a datetime type and use date math to avoid string conversions.
SELECT DATEADD(DAY, DATEDIFF(DAY, '20000101', purchase_time), '20000101')
FROM dbo.table;
EDIT
If you want the date only for display purposes, not for calculations or grouping, that is probably best handled at the client. You can do it in SQL simply by saying:
SELECT dt = CONVERT(CHAR(10), purchase_time, 120)
FROM dbo.table;
In SQL Server 2008 you can use the newly added date type:
select convert(date, purchase_time) from TableName
Update:
In versions prior to SQL 2008, I used the following solution for this problem:
select convert(datetime, convert(int, convert(float, purchase_time)))
from TableName

Error in Updating a table using datetime as parameter in Stored procedure

Here is my query:
UPDATE Mst_Attendance
SET FNLogged=#FNLogged,
ANLogged=#ANLogged,LogTime=#LogTime,LogOuttime=#LogOuttime
WHERE EmployeeId=#Employee_id AND Atdate = CONVERT(VARCHAR(10), #AtDate, 101) AS [MM/DD/YYYY]
-- Convert(Datetime,#AtDate)
SELECT * FROM Mst_Attendance where Atdate=#AtDate and EmployeeId=#Employee_id
Error occured near AS
Just remove the AS [MM/DD/YY] snippet. You don't need it, and it's not valid inside a WHERE clause.
And what in the world are you doing storing dates as strings in your database? That's just a bad idea. Are you trying to truncate the time portion?
AS in that context is used to give an alias to a column or table; there is no sense in an AS here, since that isn't a select.
You have already specified a format via CONVERT(VARCHAR(10), #AtDate, 101), but this also seems odd; dates are not strings. If you are matching on a datetime - keep everything as a datetime.
If you are actually trying to remove the time portion (leaving just a date), either a: don't send the time (cut it at the caller), or b: do something like:
set #date = cast(floor(cast(#date as float)) as datetime)

Best approach to remove time part of datetime in SQL Server

Which method provides the best performance when removing the time portion from a datetime field in SQL Server?
a) select DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
or
b) select cast(convert(char(11), getdate(), 113) as datetime)
The second method does send a few more bytes either way but that might not be as important as the speed of the conversion.
Both also appear to be very fast, but there might be a difference in speed when dealing with hundreds-of-thousands or more rows?
Also, is it possible that there are even better methods to get rid of the time portion of a datetime in SQL?
Strictly, method a is the least resource intensive:
a) select DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
Proven less CPU intensive for the same total duration a million rows by someone with way too much time on their hands: Most efficient way in SQL Server to get a date from date+time?
I saw a similar test elsewhere with similar results too.
I prefer the DATEADD/DATEDIFF because:
varchar is subject to language/dateformat issues
Example: Why is my CASE expression non-deterministic?
float relies on internal storage
it extends to work out first day of month, tomorrow, etc by changing "0" base
Edit, Oct 2011
For SQL Server 2008+, you can CAST to date i.e. CAST(getdate() AS date). Or just use date datatype so no time to remove.
Edit, Jan 2012
A worked example of how flexible this is: Need to calculate by rounded time or date figure in sql server
Edit, May 2012
Do not use this in WHERE clauses and the like without thinking: adding a function or CAST to a column invalidates index usage. See number 2 here Common SQL Programming Mistakes
Now, this does have an example of later SQL Server optimiser versions managing CAST to date correctly, but generally it will be a bad idea ...
Edit, Sep 2018, for datetime2
DECLARE #datetime2value datetime2 = '02180912 11:45' --this is deliberately within datetime2, year 0218
DECLARE #datetime2epoch datetime2 = '19000101'
select DATEADD(dd, DATEDIFF(dd, #datetime2epoch, #datetime2value), #datetime2epoch)
In SQL Server 2008, you can use:
CONVERT(DATE, getdate(), 101)
Of-course this is an old thread but to make it complete.
From SQL 2008 you can use DATE datatype so you can simply do:
SELECT CONVERT(DATE,GETDATE())
In SQL Server 2008, there is a DATE datetype (also a TIME datatype).
CAST(GetDate() as DATE)
or
declare #Dt as DATE = GetDate()
SELECT CAST(FLOOR(CAST(getdate() AS FLOAT)) AS DATETIME)
...is not a good solution, per the comments below.
I would delete this answer, but I'll leave it here as a counter-example since I think the commenters' explanation of why it's not a good idea is still useful.
Here's yet another answer, from another duplicate question:
SELECT CAST(CAST(getutcdate() - 0.50000004 AS int) AS datetime)
This magic number method performs slightly faster than the DATEADD method. (It looks like ~10%)
The CPU Time on several rounds of a million records:
DATEADD MAGIC FLOAT
500 453
453 360
375 375
406 360
But note that these numbers are possibly irrelevant because they are already VERY fast. Unless I had record sets of 100,000 or more, I couldn't even get the CPU Time to read above zero.
Considering the fact that DateAdd is meant for this purpose and is more robust, I'd say use DateAdd.
SELECT CAST(CAST(GETDATE() AS DATE) AS DATETIME)
I really like:
[date] = CONVERT(VARCHAR(10), GETDATE(), 120)
The 120 format code will coerce the date into the ISO 8601 standard:
'YYYY-MM-DD' or '2017-01-09'
Super easy to use in dplyr (R) and pandas (Python)!
BEWARE!
Method a) and b) does NOT always have the same output!
select DATEADD(dd, DATEDIFF(dd, 0, '2013-12-31 23:59:59.999'), 0)
Output: 2014-01-01 00:00:00.000
select cast(convert(char(11), '2013-12-31 23:59:59.999', 113) as datetime)
Output: 2013-12-31 00:00:00.000
(Tested on MS SQL Server 2005 and 2008 R2)
EDIT: According to Adam's comment, this cannot happen if you read the date value from the table, but it can happen if you provide your date value as a literal (example: as a parameter of a stored procedure called via ADO.NET).
See this question:
How can I truncate a datetime in SQL Server?
Whatever you do, don't use the string method. That's about the worst way you could do it.
Already answered but ill throw this out there too...
this suposedly also preforms well but it works by throwing away the decimal (which stores time) from the float and returning only whole part (which is date)
CAST(
FLOOR( CAST( GETDATE() AS FLOAT ) )
AS DATETIME
)
second time I found this solution... i grabbed this code off
CAST(round(cast(getdate()as real),0,1) AS datetime)
This method does not use string function. Date is basically a real datatype with digits before decimal are fraction of a day.
this I guess will be faster than a lot.
For me the code below is always a winner:
SELECT CONVERT(DATETIME, FLOOR(CONVERT(FLOAT,GETDATE())));
select CONVERT(char(10), GetDate(),126)
Strip time on inserts/updates in the first place. As for on-the-fly conversion, nothing can beat a user-defined function maintanability-wise:
select date_only(dd)
The implementation of date_only can be anything you like - now it's abstracted away and calling code is much much cleaner.
I think you mean
cast(floor(cast(getdate()as float))as datetime)
real is only 32-bits, and could lose some information
This is fastest
cast(cast(getdate()+x-0.5 as int)as datetime)
...though only about 10% faster(about 0.49 microseconds CPU vs. 0.58)
This was recommended, and takes the same time in my test just now:
DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
In SQL 2008, the SQL CLR function is about 5 times faster than using a SQL function would be, at 1.35 microseconds versus 6.5 microsections, indicating much lower function-call overhead for a SQL CLR function versus a simple SQL UDF.
In SQL 2005, the SQL CLR function is 16 times faster, per my testing, versus this slow function:
create function dateonly ( #dt datetime )
returns datetime
as
begin
return cast(floor(cast(#dt as float))as int)
end
How about select cast(cast my_datetime_field as date) as datetime)? This results in the same date, with the time set to 00:00, but avoids any conversion to text and also avoids any explicit numeric rounding.
I think that if you stick strictly with TSQL that this is the fastest way to truncate the time:
select convert(datetime,convert(int,convert(float,[Modified])))
I found this truncation method to be about 5% faster than the DateAdd method. And this can be easily modified to round to the nearest day like this:
select convert(datetime,ROUND(convert(float,[Modified]),0))
Here I made a function to remove some parts of a datetime for SQL Server. Usage:
First param is the datetime to be stripped off.
Second param is a char:
s: rounds to seconds; removes milliseconds
m: rounds to minutes; removes seconds and milliseconds
h: rounds to hours; removes minutes, seconds and milliseconds.
d: rounds to days; removes hours, minutes, seconds and milliseconds.
Returns the new datetime
create function dbo.uf_RoundDateTime(#dt as datetime, #part as char)
returns datetime
as
begin
if CHARINDEX( #part, 'smhd',0) = 0 return #dt;
return cast(
Case #part
when 's' then convert(varchar(19), #dt, 126)
when 'm' then convert(varchar(17), #dt, 126) + '00'
when 'h' then convert(varchar(14), #dt, 126) + '00:00'
when 'd' then convert(varchar(14), #dt, 112)
end as datetime )
end
Just in case anyone is looking in here for a Sybase version since several of the versions above didn't work
CAST(CONVERT(DATE,GETDATE(),103) AS DATETIME)
Tested in I SQL v11 running on Adaptive Server 15.7
If possible, for special things like this, I like to use CLR functions.
In this case:
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlDateTime DateOnly(SqlDateTime input)
{
if (!input.IsNull)
{
SqlDateTime dt = new SqlDateTime(input.Value.Year, input.Value.Month, input.Value.Day, 0, 0, 0);
return dt;
}
else
return SqlDateTime.Null;
}
I, personally, almost always use User Defined functions for this if dealing with SQL Server 2005 (or lower version), however, it should be noted that there are specific drawbacks to using UDF's, especially if applying them to WHERE clauses (see below and the comments on this answer for further details). If using SQL Server 2008 (or higher) - see below.
In fact, for most databases that I create, I add these UDF's in right near the start since I know there's a 99% chance I'm going to need them sooner or later.
I create one for "date only" & "time only" (although the "date only" one is by far the most used of the two).
Here's some links to a variety of date-related UDF's:
Essential SQL Server Date, Time and DateTime Functions
Get Date Only Function
That last link shows no less than 3 different ways to getting the date only part of a datetime field and mentions some pros and cons of each approach.
If using a UDF, it should be noted that you should try to avoid using the UDF as part of a WHERE clause in a query as this will greatly hinder performance of the query. The main reason for this is that using a UDF in a WHERE clause renders that clause as non-sargable, which means that SQL Server can no longer use an index with that clause in order to improve the speed of query execution. With reference to my own usage of UDF's, I'll frequently use the "raw" date column within the WHERE clause, but apply the UDF to the SELECTed column. In this way, the UDF is only applied to the filtered result-set and not every row of the table as part of the filter.
Of course, the absolute best approach for this is to use SQL Server 2008 (or higher) and separate out your dates and times, as the SQL Server database engine is then natively providing the individual date and time components, and can efficiently query these independently without the need for a UDF or other mechanism to extract either the date or time part from a composite datetime type.
I would use:
CAST
(
CAST(YEAR(DATEFIELD) as varchar(4)) + '/' CAST(MM(DATEFIELD) as varchar(2)) + '/' CAST(DD(DATEFIELD) as varchar(2)) as datetime
)
Thus effectively creating a new field from the date field you already have.