T-SQL: How to update just date part of datetime field? - sql-server

In SQL Server 2008 I need to update just the date part of a datetime field.
In my stored procedure I receive the new date in datetime format. From this parameter I have to extract the date (not interested in time) and update the existing values date part.
How can I do this?

One way would be to add the difference in days between the dates to the old date
UPDATE TABLE
SET <datetime> = dateadd(dd,datediff(dd,<datetime>,#newDate),<datetime>)
WHERE ...

Related

HSQLDB inserting datetime format for SQL Server throws exception

I am trying to run unit tests for my SQL Server query. Query is simply inserting date to the table. I tried two different formats but didn't work:
parameters.addValue(STUDY_DATE, getDate(studyEvent.getStudy().getStudyDate()));
Timestamp timestamp = new Timestamp(getDate(studyEvent.getStudy().getStudyDate()).getTimeInMillis());
parameters.addValue(STUDY_DATE, timestamp);
And this is getDate() method that returns Calendar object:
private Calendar getDate(long time) {
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"));
calendar.setTimeInMillis(time);
return calendar;
}
I am not sure if the problem is that SQL Server's datetime format issue or hsqldb issue. Here is my hsqldb create table:
SET DATABASE SQL SYNTAX MSS TRUE;
DROP TABLE event_study IF EXISTS;
CREATE TABLE event_study
(
STUDY_ID INT,
STUDY_DATE DATE
)
Is my hsqldb setup wrong? or should I use different datetime format for SQL Server?
Error I am getting is:
data exception: invalid datetime format; nested exception is java.sql.SQLDataException: data exception: invalid datetime format
and SQL query that I am running is:
INSERT INTO event_study(study_id, study_date)
SELECT x.*
FROM (VALUES(:study_id, :study_date))
AS x(study_id, study_date)
WHERE NOT EXISTS (SELECT 1 FROM event_study s WHERE s.study_id = x.study_id)
As you are not using strings for dates, this is not actually a formatting issue, but a Java type issue. With your table definition, DATE does not have time information. You can create and use a java.sql.Date object for the parameter value. If you want a datetime column, which includes information on time of the day, then use TIMESTAMP in your table definition and a java.sql.Timestamp for the parameter value.
In either case, you cannot use a Calendar object as parameter value.

SQL Server convert datetimeoffset to timestamp

I have a datetimeoffset column DateEntry in my SQL Server table. When I want to convert it to a timestamp format with this query :
SELECT CAST(Table1.[DateEntry] AS timestamp)
FROM Table1
I get the following error :
Error : 529- Explicit conversion from data type datetimeoffset to
timestamp is not allowed.
TIMESTAMP in SQL Server has absolutely nothing to do with a date and time, therefore you cannot convert an existing date&time into a TIMESTAMP.
TIMESTAMP or more recently called ROWVERSION is really just a binary counter that SQL Server updates internally whenever row has been modified. You cannot set a TIMESTAMP column yourself, you can just read it out. It is used almost exclusively for optimistic concurrency checks - checking to see whether a row has been modified since it's been read, before updating it.
According to MSDN:
The timestamp data type is just an incrementing number and does not
preserve a date or a time. To record a date or time, use a datetime
data type.
If your are absolutely sure, you can use indirect conversion:
DECLARE #dto datetimeoffset = '2016-01-01 12:30:56.45678'
SELECT CONVERT(timestamp, CONVERT(varbinary(12), #dto))
See also #marc_s's answer.
Try the following script if this this is what you are trying your side
SELECT CAST(CAST(Table1.[DateEntry] AS datetime) as timestamp) FROM Table1

Use sql server Convert Function to convert hijri to gregorian date

I have simple table on my sql server and in my table have a date field,and into date field save a hijri date,i want use the sql server convert function to convert hijri date to gregorian date.
how can i do this?
i use this query in sql server:
update k
set time=CONVERT(datetime,GETDATE(),101)
and i insert this "1392/4/21" in time field ,and when convert sql server return this "2014/11/5",when i use online date convertor,and insert this "2014/11/5",convert date is "1393/2/14" !!is this the correct resualt?
Here is a sample how you could do it:
select convert(datetime, value, 131)
from (
values ('13/01/1436 9:54:59:767AM')
) samples(value)
There are several blogs about this, likethis one:
http://blogs.msdn.com/b/wael/archive/2007/04/29/sql-server-hijri-hijra-dates.aspx
and this one:
http://raresql.wordpress.com/2013/05/08/sql-server-how-to-convert-gregorian-dates-to-hijri-date-with-formatting/

Inserting CDate to SQL Server 2012 Flip Day and Month

I have a problem inserting a date from a VB.net Program to a SqlServer2012 instance.
First here is how i generate the data (Vb.net)
ExitTime = CDate("1.1.1970 00:00:00").AddSeconds(currentField).ToLocalTime
We add this value to a stored procedure (Vb.net)
With comsql5.Parameters.AddWithValue("#ExitTime", ExitTime)
In the Sql Server stored procedure
#ExitTime datetime, [...]
[...]
Insert into [table] ([ExitTime]) VALUES (#ExitTime)
Here is the output of the exit time in the vb.net
Exit Time : 08/07/2014 2:06:31 PM
Here is the same row in the Sql server database
2014-08-07 14:06:31.000
What I would like to see in the database is 2014-07-08 14:06:31.00
Because another part in the program does a check on the field but as a String... and it does not match because it flip the month and day
EDIT: TO be clear, I can't change the other part that does the comparison as a string. I know this is a poor way to compare datetime.
Thank for your time
Have you tried using the Convert function?
SELECT CONVERT (VARCHAR, getdate(), 121);
Check this links for more information MSDN - CAST and CONVERT and SQL Server Datetime Format

Add a Specific Time to a Date Value

I have a stored procedure that receives a date value like this:
#AsOf date
In the query I am using this date to filter a datetime field. I need to convert the date passed in to a datetime field with the time portion set to 09:00:00 PM so I can add a filter in the WHERE clause like this:
(DateCreated < #TimeAsOfNinePM)
I know this is easy, but it's just escaping me at the moment. I'm using SQL Server 2008.
Try this out:
DECLARE #TimeAsOfNinePM datetime
DECLARE #AsOf date
SET #AsOf = '2010-10-01'
SET #TimeAsOfNinePM = dateadd(hh,21, cast(#AsOf as datetime))
PRINT #TimeAsOfNinePM
The trick is you need to convert the date datatype to a datetime datatype to add hours to it.

Resources