Update partial Date time column value in sql server - sql-server

I want to update date time column in my SQL server.Consider the date time column contains following values,
2015-02-13 00:00:00.000
2015-02-18 00:00:00.001
2015-02-21 00:05:00.002
2015-02-13 00:03:00.003
2015-02-19 01:00:00.004
2015-01-13 00:00:00.005
2015-01-11 00:01:00.006
2015-01-13 00:02:00.007
I want to change the only the day to 14 without making any change in other values(year,month,time). Could you please help me to write a update statement for this?

Depending on your where conditions you can use DATEADD function, like DATEADD(day, 1, column). This will not affect other parts.
If the day parts differ, you can use
column = DATEADD(day, 14-DATEPART(day, column), column)

want to update only specify date with out affecting HH:MM:SS then in filter condition use Substring also
Assume your table like this :
update table
set datecol = DATEADD(DAY,1,DATECOL)
where
datecol = SUBSTRING(DATECOL,0,CHARINDEX(' ',DATECOL))

update table1
set column1 = dateadd(dd,14, convert(datetme, CONVERT(VARCHAR(25), DATEADD(dd, -(DAY(column1)-1),column),101),112))

Related

How do I use GETDATE in a WHERE clause using single quotes?

How do I use GETDATE() in a WHERE clause using single quotes? I have the following script:
SELECT DISTINCT
pm_requisition.a_requisition
,pm_requisition.a_req_description
,pm_requisition.rq_position_loc
,pm_requisition.rq_status_of_emp
,pm_applicant_rec.a_applicant_no
,pm_applicant_rec.aq_status_of_appl
,pm_applicant_rec.aq_apply_date
FROM
pm_requisition
LEFT OUTER JOIN pm_applicant_rec
ON pm_requisition.a_requisition = pm_applicant_rec.a_requisition
WHERE
pm_requisition.rq_status_of_req NOT IN (N'6', N'7')
AND pm_applicant_rec.aq_status_of_appl = N'N'
AND rq_position_loc = '3300'
AND pm_applicant_rec.aq_apply_date = CAST(GETDATE()AS datetime2) --('2021-03-30 00:00:00.000')
The dataset does not yield any data using the GETDATE() clause, but if I use today's date (commented out) with single quotes I get the correct dataset. I obviously can't put single quotes around CAST(GETDATE()AS datetime2). Am I missing something simple?
The reason your query does not work is that GETDATE() returns the current date and time, not the current date. As a result, the last line will never match, as you are comparing a date only with a date and time.
Your solution CAST(GETDATE() AS DATE) works is because it removes the time, which means you are now comparing a date with a date.
If you simplify your SQL to something like this :-
SELECT TOP 1 aq_apply_date,
GETDATE() [GETDATE]
FROM pm_applicant_rec;
you will see a result like this, so two different values, and clear why they do not match :-
aq_apply_date GETDATE
----------------------- -----------------------
2021-04-04 00:00:00.000 2021-04-04 14:32:15.507
You might see something slightly different depending on the datatype used for aq_apply_date.

Is there a way to create a date column from other columns?

As the title says, can I create a new column from two fields? For example, I created a new column from the master values table to represent the months, and I have another column with the year. Can I combine them to create a date with whatever day? What I have is on the left, what I want is on the right:
Month Year NewDate
---- ---- -------
3 2015 2015-03-01
4 2015 2015-04-01
Whenever I tried to cast the month as datetime, it would look like this:
1900-01-03
This is probably a dumb question, but I created a timeline that lists out the months, and the trouble lies whenever the end date parameter chosen is less than the start date parameter, such as December 2015 to January 2016. I created the report in SSRS and the parameters aren't quite working correctly. Thanks guys!
Also, here's a snapshot of what the timeline looks like:
SELECT
DATEFROMPARTS ( [Year], [Month], 1 ) AS NewDate
FROM
YourTable
;
select dateadd(mm, t.Month -1, dateadd(yy,t.Year-1900,'19000101'))
from table1 t

SQL update based on query results with two parameters

I've created a virtual table in SQL Server that has 28 days from the current date and each date has rows for time that range from 12-10 pm incremented by 15 min and another value to indicate that it's turned on/off for availability, so it would be something like this:
date time onoff
-------------------------------------------------
2015-04-08 12:00 1
2015-04-08 12:15 1
....continue until 22:00 then start next day
2015-04-09 12:00 1
..... continue for 28 days
I'd like to update the availability based on a query from another table which would return the date, start and end time...
So far I came up with this
update table1
set onoff = 0
where tbl1date in (select tbl2date from table2 where userid = 1)
The problem I'm having is adding in the between certain hours part of the equation and I'm not sure how to do something like this in SQL or how to even search for the answer based on not being able to word it properly...
Can someone help or point me in the right direction?
use a DATETIME, don't use separate DATE and TIME fields.
I think you should take a look at DATEDIFF (https://technet.microsoft.com/pl-pl/library/ms189794(v=sql.110).aspx) function.
Your where query could look like this:
update table1 set onoff = 0
where
DATEDIFF(minute, <MIN_RANGE>, tbl1date) >= 0 and
DATEDIFF(minute, tbl1date, <MAX_RANGE>) >= 0
How you calculate MIN_RANGE and MAX_RANGE depends on your table2 structure.
As suggested, if you have control over the structure, use datetime fields as they are easier to do the comparisons on. I'm going to assume you don't have control over the structure.
In order to compare the datetimes you need to create them from your separate date and times. You can either parse the time field for the hours and minutes and use DATEADD to add the appropriate offsets to the date, or you can use CONVERT to interpret a date time string as a date. Something like
CONVERT(datetime, SUBSTRING(CONVERT(varchar, tbl1date, 121), 1, 10) + ' ' + tbl1time, 121)
What this does is to convert the date to odbc cannonical format and throwaway the time part as it takes only the first 10 characters. Then it appends the time and interprets the whole string as a odbc cannonical datetime string. That format is yyyy-mm-dd hh:mi:ss.mmm. The hours are based on 24 hours. So if your times are in AM/PM format you're going to have to convert them.
If your other table has separate date and times you'd use a similar expression to combine them.
Once you have the datetimes you can do something like this
UPDATE table1
SET onoff = 0
WHERE <expression above> BETWEEN (SELECT min_value FROM table2) AND (SELECT max_value FROM table2)

filter a sql statement using its column values

I have a table named 'Table1'.
It has 3 columns namely
1. vehicle Number
2. startdatetime
3. enddatetime
the table is as below->
vehicleno | startdatetime | enddatetime |
1 2013/07/16 00:00:00 2013/07/17 00:00:00
2 2013/07/16 00:00:00 2013/07/18 14:00:00
3 2013/07/17 12:19:00 2013/07/20 17:35:00
4 2013/07/19 10:24:56 2013/07/19 20:14:00
5 2013/07/15 08:10:00 2013/07/18 09:10:00
Now i want a o/p such that ,At the time of executing the query if the present datetime is between the startdatetime and enddatetime then the record should be displayed in my o/p table.
I have tried with the query..
select * from Table1 where startdatetime between '2013/07/17 00:00:00' and '2013/07/17 23:59:59' or enddatetime between '2013/07/17 00:00:00' and '2013/07/17 23:59:59'
but i didn't get the result i want.
Please help me out..
If you are looking for the current date why not use 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.
Something like
select *
from Table1
where GETDATE() BETWEEN startdatetime AND enddatetime
SQL Fiddle DEMO
I would think you'd want to do:
select vehicleno ,startdatetime, enddatetime
from table1
where getUTCDate() between startdatetime and enddatetime
Then if you aren't in GMT you can do a dateAdd(hour, 5, getUTCDate()) if you are say EST.
It's always more correct to specify the columns you want to use in the select statement.
And at this point since we live in a global community I feel it's more appropriate to use getUTCDate() instead of just regular getDate() because getDate() just does the timezone that the server is located in and it will depend on where the server is if this works for your situation.
You can see the definition of GetUTCDate()
Try the following:
select *
from Table1
where GetDate() between startdatetime and enddatetime
GetDate() gets the current datetime
Just for the record, if you want to use literal dates in SQL, use ISO date format (YYYY-MM-DD hh:mm:ss). That is the only format guaranteed to be interpreted correctly regardless of your locale settings.

Ignoring the time element of a datetime field

I have a datetime field in my table. I want to delete records based on a date but I am not interested in the time element. How would I write the SQL for this ? I am using MS SQL 2008.
For best use of indexes, I'd go for this kind of approach:
To delete all records for 1st December:
DECLARE #DateToDelete DATETIME
SET #DateToDelete = '20091201'
DELETE FROM MyTable
WHERE MyDateField >= #DateToDelete AND MyDateField < DATEADD(dd, 1, #DateToDelete)
The alternatives include:
DELETE FROM MyTable
WHERE CAST(CONVERT(VARCHAR(10), MyDateField, 120) AS DATETIME) = #DateToDelete
which converts each datetime value to just it's date part.
But I'd still go with my original way as it allows for more efficient execution.
If you use MS SQL 2008 then you could convert to a new DATE type
DELETE FROM table WHERE date_filed >= CONVERT(DATE,GETDATE())
Is the time relevant in any other place? If not, then you should use a DATE column instead. If you cannot, then the best way to seek a date part of a datetime in a WHERE clause is to use a range:
... WHERE dateColumn BETWEEN '20091221' and '20091222';
Note that given the datetime accuracy of 3ms a datetime like 20091221 23:59:59.999 may be aproximated to 20091222 00:00:00.000 and this can sometime create problems.
There is a great collection of blog posts on the topic of datetime at T-SQL Tuesday #001 (Date/Time Tricks): The Roundup
Try this:-
declare #date datetime
set #date = '2006-11-09'
select #date, dateadd(ms, -1, DATEADD(dd,1,#date))
delete from login
where datecreated between #date AND dateadd(ms, -1, DATEADD(dd,1,#date))
This is what datediff is for:
delete from Table where datediff(day,'2009-12-09',date_filled) = 0

Resources