updating only date part from datetime in sql server 2000 - sql-server

I have data in the table like the following.
col1 col2 col3
--------------------------------------------------------
6/5/2010 18:05:00 6/2/2010 10:05:00 Null
6/8/2010 15:05:00 6/3/2010 10:45:00 6/5/2010 11:05:00
6/3/2010 15:05:00 Null 6/7/2010 12:05:00
6/1/2010 15:05:00 6/3/2010 10:45:00 6/1/2010 14:05:00
what my requirement is I want to update the date of there columns with single date without disturbing the time. say for example I want to update the table data with 6/1/2010 where the field data is not null. please let me know the query for updating the table data.
thanks & regards,
murali

I think this should work for you.
create table #t
(
col1 datetime
)
Insert Into #t
values ('2010-06-01 10:00:00')
Insert Into #t
values ('2010-06-06 11:00:00')
Insert Into #t
values ('2010-05-24 12:40:00')
Insert Into #t
values ('2010-05-07 13:00:00')
Insert Into #t
values (Null)
declare #newDate datetime
set #newDate = '2010-07-01'
update #t
Set col1 = DateAdd(day, DateDiff(day, col1, #newDate), Col1)
Where Col1 is not null
select * From #t
drop table #t

You may need to do it via SELECT statement as you dont need to run UPDATE statement each time new data are added to the table

Related

using regex in microsoft sql server management studio

I have a table in which I copy the data based on an condition and I insert it into the same table with a different ID.As follows:
SET IDENTITY_INSERT Table ON
INSERT INTO Table (ID,GroupID,Name,link,etc..)
SELECT
(SELECT MAX(ID) FROM Table) + ROW_NUMBER()OVER (ORDER BY ID),
10500,
Name,
link
FROM Table
WHERE GroupID =10400
SET IDENTITY_INSERT Table OFF
this gives me the following table
**ID | GroupID | Link**
3 | 10400 |/testsDatas/10400/Uploads
4 | 10500 |/testsDatas/10400/Uploads //this is a new entry that the above query will enter.
The question I have is when the above query copies a row how can I change /testsDatas/10400/ to /testsDatas/10500/?
so that it looks like the following
**ID | GroupID | Link**
3 | 10400 |/testsDatas/10400/Uploads
4 | 10500 |/testsDatas/10500/Uploads //desired output
there is mulitple rows of data,with more columns that I did not add.How do I achieve this?
Would using REPLACE work for you? A simple example:]
DECLARE #table TABLE ( ID INT, name VARCHAR(50), link VARCHAR(50) )
INSERT INTO #table
VALUES
( 3, '10400', '/testsDatas/10400/Uploads' )
INSERT INTO #table
SELECT
(
SELECT MAX(ID)
FROM #table
) + ROW_NUMBER() OVER (ORDER BY ID),
10500,
REPLACE( link, name, 10500 )
FROM #table
SELECT *
FROM #table
My results:

Insert row by using loop with condition

I have tbl_emp like that:
empid
1
2
3
4
And tbl_att like that:
empid workingdate
1 2017-05-11
2 2017-05-13
3 2017-05-14
...........
...........
I have a job in SQL Server agent to execute step every Sunday and I want that job to insert a row for each empid with that day into tbl_att. Let's say Sunday is 2017-05-22 so I want it like that:
empid workingdate
1 2017-05-22
2 2017-05-22
3 2017-05-22
It means that I want it to insert into tbl_att for all empid with the same day (task execution day), so can anyone guide me a query that I need to put into my step command?
Try this,
INSERT INTO tbl_att SELECT empid,CAST(GETDATE() AS DATE) FROM tbl_emp;
insert into tbl_att (empid, workingdate)
Select empid,cast(getdate() as date) from tbl_emp
As you run the job on every Sunday, above query will insert the data as per your expectation I believe. FYI, But it depends on the system date
Hope it helps you
CREATE TABLE #tbl_att (empid INT,workingdate DATE)
CREATE TABLE #EmpidTab (empid INT)
INSERT INTO #EmpidTab
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4
SELECT * FROM #EmpidTab
INSERT INTO #EmpidTab
SELECT 5
INSERT INTO #tbl_att
SELECT Empid
,Getdate() AS workingdate
FROM #EmpidTab i
WHERE NOT EXISTS (
SELECT 1
FROM #tbl_att t
WHERE t.empid = i.Empid
) --Eliminate duplicte insertion of empid's
SELECT ##ROWCOUNT AS NoRowsInserted
SELECT *
FROM #tbl_att

Get Sum of BudgetTransactionLine

I'm trying to get sum of budget transaction line by month .Is there an error on my Query.
The result of my query is :
RECIDLine RecIDHeader Date Amount
5637157326 5637149076 2012-08-01 00:00:00.000 850.00
5637157342 5637149079 2012-12-01 00:00:00.000 1000.00
5637157343 5637149079 2012-12-01 00:00:00.000 80.00
5637157344 5637149079 2012-12-01 00:00:00.000 2700.00
But i want to get somthing like this :
RECIDLine RecIDHeader Date Amount
5637157326 5637149076 2012-08-01 00:00:00.000 850.00
5637157342 5637149079 2012-12-01 00:00:00.000 3780.00
This is my query :
IF OBJECT_ID('tempdb..#BudgetTransTmp') IS NOT NULL
DROP TABLE #BudgetTransTmp
Select
BudgetTransactionLine.RECID AS RecIdLine,
BUDGETTRANSACTIONHEADER.RECID AS RecIdHeader,
BudgetTransactionLine.DATE,
SUM(CAST((BudgetTransactionLine.TransactionCurrencyAmount ) as decimal(18,2))) AS Amount
INTO #BudgetTransTmp
FROM MicrosoftDynamicsAX.dbo.BudgetTransactionLine AS BudgetTransactionLine
INNER JOIN MicrosoftDynamicsAX.dbo.BUDGETTRANSACTIONHEADER AS BUDGETTRANSACTIONHEADER
ON BUDGETTRANSACTIONHEADER.RECID = BudgetTransactionLine.BUDGETTRANSACTIONHEADER
AND BUDGETTRANSACTIONHEADER.budgetTransactionType = '3'
AND BUDGETTRANSACTIONHEADER.PARTITION = #Partition
WHERE BudgetTransactionLine.PARTITION =#Partition
AND BudgetTransactionCode.DATAAREAID = 'USMF'
AND BudgetTransactionLine.DATE >= PeriodCalandarTmp.StartDate
AND BudgetTransactionLine.DATE <= PeriodCalandarTmp.EndDate
GROUP BY BudgetTransactionLine.DATE,
BUDGETTRANSACTIONHEADER.RECID,
BudgetTransactionLine.RECID
select * from #BudgetTransTmp
And I need to keep BudgetTransactionLine.RECID in select
You should not include BudgetTransactionLine.RecId (RecIdLine) in your GROUP BY.
If you need this column, then you must use one of Aggregate Function (for example in the SELECT part MIN(BudgetTransactionLine.RecId) AS RecIdLine.
just put you whole data in Simple CTE and just join with your Temp table. Please go through the query carefully insert your code according to that
Below the Sample data
declare #Table1 TABLE
(RECIDLine BIGint, RecIDHeader BIGint, Date varchar(30), Amount DECIMAL(18,2))
;
INSERT INTO #Table1
(RECIDLine, RecIDHeader, Date, Amount)
VALUES
(5637157326, 5637149076, '2012-08-01 00:00:00.000', 850.00),
(5637157342, 5637149079, '2012-12-01 00:00:00.000', 1000.00),
(5637157343, 5637149079, '2012-12-01 00:00:00.000', 80.00),
(5637157344, 5637149079, '2012-12-01 00:00:00.000', 2700.00)
;
;WITH CTE AS (
select RecIDHeader,Date,SUM(Amount)Amount,ROW_NUMBER()OVER(PARTITION BY RecIDHeader ORDER BY Date)RN from #Table1
GROUP BY RecIDHeader,Date )
Select T.RECIDLine,C.RecIDHeader,C.Amount,C.Amount FROM CTE C
INNER JOIN (select MIN(RECIDLine) RECIDLine, RecIDHeader from #Table1
GROUP BY RecIDHeader)T
ON T.RecIDHeader = C.RecIDHeader

How to find the nearest time in SQL Server query?

id Bus arrival time
1 6:30
2 7:00
3 9:00
4 10:00
5 15:00
6 16:00
If one bus comes at 6:40 then nearest time should be 6:30
If bus comes at 9:35 nearest time should be 10:00 like wise I need the solution as a T-SQL query
Thanks In Advance,
Anil
declare #checkTime time = '6:40'
select top 1 * from schedule
order by ABS(DATEDIFF(Second, #checkTime, busArrivalTime))
SQLFIDDLE
you can do something like :
DECLARE #TIME TIME = '6:45'
DECLARE #TEMP TABLE(ID int,start time);
insert into #temp values(1, '6:30')
insert into #temp values(1, '7:00')
insert into #temp values(1, '9:00')
insert into #temp values(1, '10:00')
insert into #temp values(1, '15:00')
insert into #temp values(1, '6:30')
insert into #temp values(1, '16:00')
SELECT top(1)start ,ABS(DATEDIFF(MINUTE , #TIME ,START )) as diff
FROM #TEMP
order by diff

SQL Server : change strings in multiple rows

I have about 2 million rows in my table with a column for dates... this column is of type VARCHAR, and it contains a wrong date format, we have this format dd/mm/YYYY and it must be YYYY-mm-dd...
How can I change it? Remember it is a Varchar column, not a Datetime column (and it can't be datetime field because we manipulate it with our queries)
Thank you in advice.
UPDATE
As I said, I have MILLIONS of rows, so it's not possible to query one by one the date field to a new one, as you said in your responses.... I need some kind of "automatic" mode to transfer every row to it's new format and datatype column.
If your end goal is just cleanup to end up in a datetime column, you only need to add a new column, then transfer the data:
update tbl
set new_datetime_col = convert(datetime, dateinvarchar, 103);
Query:
update tbl
set dateinvarchar = convert(char(10),
convert(datetime,
dateinvarchar, 103), 121)
select * from tbl
Sample table:
create table tbl (dateinvarchar varchar(10));
insert tbl values
('01/02/2012'),
('02/02/2012'),
('03/02/2012'),
('14/12/2012');
Sample Result:
| DATEINVARCHAR |
-----------------
| 2012-02-01 |
| 2012-02-02 |
| 2012-02-03 |
| 2012-12-14 |
Try this
Declare #t table (dt varchar(25));
insert into #t
SELECT '01/01/2012' UNION
SELECT '02/01/2012' UNION
SELECT '03/01/2012'
SELECT convert(varchar(10), CONVERT(datetime, dt, 103), 120) as dt_new
from #t;
CREATE TABLE newT(id, dt varchar(10));
INSERT INTO newT
SELECT id, convert(varchar(10), CONVERT(datetime, dt, 103), 120) as dt_new
from oldT;

Resources