how to auto update the database according to date?
Message ID | Message | StartDate | EndDate | Status
1 | Hello | 07/7/2012 | 08/7/2012 | Expired
2 | Hi | 10/7/2012 | 12/7/2012 | Ongoing
3 | Hi World | 11/7/2012 | 18/7/2012 | Pending
How to auto update the status according to the date today?
More information : I'm using SQL-Server Management Studio. Sorry for not stating.
I would create a SP that sets the status to "Expired" for all messages that have EndDate > GETDATE() and schedule it using a job in Sql Server:
CREATE PROCEDURE UpdateMessages
AS
UPDATE Messages SET Status = 'Expired' WHERE EndDate > GETDATE()
GO
The best thing you can do is to create store procedure that update records on your table based on your date time and create SQL server job then schedule it on your desired time when to execute it.
I don't think there is a way for the table to update itself. You should consider scheduling a Job in SQL Server.
See this MSDN article here
The job would run daily and consider each row and update the status where appropriate.
Related
I have a existing table like below in SSMS (SQL Server Management Studio).
Students_Table
UID | ID | Name | Location |
-----+------+---------+-----------+
1 1 Jogn US
2 2 Alia UK
where in UID is an auto increment and ID,Name,Location are my data columns
So I have an excel with data like below:
ID | Name | Location |
----+---------+-----------+
3 Jk Santa
4 Lima PS
Now the above two columns should be added to the bottom of the above existing Students_Table in SQL Server, but how to handle the auto increment column now?
Am I suppose to add the that column in my excel and import by considering the next incremented number.
Please help.
To be able to copy and paste the data from Excel to SQL Server, you will have to include an empty column in Excel for the identity (UID) column:
You can check out my blog article that describes how to copy data from Excel to SQL Server for further details. Section 2b includes a step-by-step instruction with screenshots for your case when having an identity column.
My database has this table
| Name | Score | Date_time |
---------------------------------
| A | 100 | 20200601000000 |
| B | 120 | 20200615000000 |
| C | 110 | 20200629000000 |
| B | 150 | 20200701000000 |
...
I want get 'Name' and 'Score' by 'Date_time' week day.
Example, when week day is Monday(= 2), I get two data.
| A | 100 |
| B | 120 |
And I tried to use the following sql statement.
select Name, Score
from SCORE_DATA
where
datepart(dw, format(cast(Date_time as bigint),'####-##-## ##:##:##')) = 2
converting
20200601000000 to 2020-06-01 00:00:00 and get week day (dw) and compare is it 2.
But I get an error:
'format' is not a recognized built-in function name.
I can't understand why.
https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql?view=sql-server-ver15&viewFallbackFrom=sql-server-2014
It explain format function.
Doesn't it apply in SQL Server 2014? What can I do?
Technically, Gabriele's answer is correct - He is explaining why you're getting the error message you're getting.
However, there is a better way to get your results, that doesn't involve Format.
First thing's first: You should seriously consider converting the [Date_Time] column you have now from (What I guess based on the code in the question is) a string to a proper DateTime.
That will solve not only this specific problem but a bunch of many other problems, some of them you probably have encountered before.
Even if this involves a lot of work it will be worth it in the long run, trust me.
However, considering that's not an option, I would suggest a couple of improvements to your existing code:
Use cast to convert the first 8 chars directly to a date, instead of converting the entire thing to a bigint and then using format and implicit conversion like your code is attempting to do now.
Use WeekDay instead of dw in the DatePart function. It's just more readable.
Calculate the week day number based on the ##DateFirst value - because SQL Server does exactly that, meaning if someone changes the date first settings your current code will produce the wrong results.
Having said all that, let's see how that looks like in code.
First, create and populate sample table (Please save us this step in your future questions):
CREATE TABLE SCORE_DATA (
[Name] varchar(1),
[Score] int,
[Date_time] char(14)
);
INSERT INTO SCORE_DATA ([Name], [Score], [Date_time]) VALUES
('A', 100, '20200601000000'),
('B', 120, '20200615000000'),
('C', 110, '20200629000000'),
('B', 150, '20200701000000');
The query (with explanatory comments):
SELECT [Name]
, [Score]
FROM SCORE_DATA
-- using full name for readability
WHERE (DATEPART(WeekDay,
-- ISO8601 format is unambiguous and will always be converted correctly
CAST(
-- A string representation of date in ISO8601 Format: yyyyMMdd
LEFT([Date_Time], 8)
AS Date) -- You don't need DateTime, just the date...
-- taking datefirst settings into consideration to get a fixed number
) + ##DATEFIRST) % 7 = 2
Results:
Name Score
A 100
B 120
C 110 -- June 29th 2020 was a Monday too...
You should check your compatibility level as below:
SELECT compatibility_level
FROM sys.databases
WHERE name = 'YourDb';
GO
110 is SQL Server 2012
120 is SQL Server 2014
130 is SQL Server 2016
140 is SQL Server 2017
150 is SQL Server 2019
To change it you can use the following script:
ALTER DATABASE YourDb
SET COMPATIBILITY_LEVEL = 120;
GO
FORMAT function require compatibility level >= 110
I have a SQL Server server running on AWS, since yesterday the GETDATE() function has been returning the hour with one more hour as if it were in time zone -02:00. I have already checked on Linux and the date and time is correct and the time zone is set to America/Sao_Paulo.
Executing the following command EXEC MASTER.dbo.xp_regread 'HKEY_LOCAL_MACHINE', 'SYSTEM\CurrentControlSet\Control\TimeZoneInformation', 'TimeZoneKeyName' returns E. South America Standard Time, which in table sys.time_zone_info is with following values:
+-----------------------------+
| CURRENT_UTC_OFFSET | -02:00 |
| IS_CURRENTLY_DST | 1 |
+-----------------------------+
How can I correct this time difference?
As Larnu mentioned, it would appear that the OS hosting your SQL Server has not been updated for Brazil's end to DST.
For Linux, ensure that the tzdata package is current. (This change was made in IANA TZDB 2019b.)
For Windows, ensure that the updates mentioned in this article from Microsoft have been applied.
I am very new to sql (3 days) and im using pgAdmin. Within pgAdmin, I can query any data from any availabe database's as normal, but from Qt after connecting successfully, I can only query data from the default database 'postgres' that I created a table for. Attempting to query data from any other database I created does nothing from Qt. My guess is that it could be an access privilege problem, so I used the grant wizard to grant all access to the table I want to view, but still no luck:
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+--------+-------+----------------------------------+---------------- ---+----------
public | tTable | table | postgres=a*r*w*d*D*x*t*/postgres+| |
| | | =arwdDxt/postgres | |
(1 row)
Like I said I am really new and kinda lost with this issue. Any help appriciated. Thanks
I have a simple table in sql server 2008 which lists the start and end times of certain events simplified below:
| StartTime | EndTime |
| 2016-09-05 07:17:23.447 | 2016-09-05 07:36:20.613 |<br/>
| 2016-09-02 12:49:40.103 | 2016-09-04 14:49:48.327 |<br/>
| 2016-09-02 12:13:31.833 | 2016-09-02 12:32:29.790 |<br/>
| 2016-09-02 10:48:39.513 | 2016-09-02 10:54:53.697 |<br/>
| 2016-09-01 07:19:06.153 | 2016-09-01 18:53:51.407 |<br/>
What I want to do is using a known StartTime extract the EndTime of the previous run
Using the above table as an example,
If I have a StartTime of 2016-09-02 12:13:31.833 I want to get back 2016-09-02 10:54:53.697 where I am using
select top 1 EndTime from myTable where StartTime < '2016-09-02 12:13:31.833
My query works perfectly every time in SQL server however I need to run it as part of an SSIS package and it doesn't return the correct answer. Instead it returns 2016-09-04 14:49:48.327
It seems that when I run my 'Execute SQL task' is SSIS, only the date part of the StartTime is being used in the comparison. The parameter for the SQl task is set up using datatype DATE (there doesn't seem to be an option for datetime!!)
Is there any way to get it to use the whole DateTime as none of the other datatypes (DB_Date,DB_Time,DB_Timestamp etc) seem to work?
I would use the lag function to return the previous row.
DECLARE #MyStartTime datetime = '2016-09-02 12:13:31.833'
SELECT
[PreviousEndTime]
FROM (
SELECT
[StartTime]
,LAG([EndTime]) OVER (ORDER BY [StartTime]) [PreviousEndTime]
FROM [MyTable]
) [tbl]
WHERE [StartTime] = #MyStartTime