I am storing the data details in the database including the date and time of storing the data.
The date and time stored in the database shows 2017-08-01 15:40:38.153 but the same data get displayed in the winforms has the different time i.e. 2017-08-01 8:40 PM. Rest of the data matches to the data from the database and displayed in exact manner.
I'm sending request to the server, and storing input value like this:
request.date_from = dateTimePickerFromDate.Value.Date;
request.date_to = dateTimePickerToDate.Value.Date;
And my query in the database is
SELECT * from TableName where (CONVERT(date, Transaction_Date) >= #date_from and (CONVERT(date,Transaction_Date) <= #date_to))
order by Transaction_Date desc
Related
I am working with a database that also has its build-in BI tool. My work involves the following stages:
Step 1: Write SQL query (let's suppose I extract two columns, date and average daily revenue)
Step 2: Use the retrieved data to create a visualization
Step 3: Add the visualization to the dashboard
I am writing the following query:
SELECT date, AVG(revenue) as revenue
From table1
Group by date;
I want the dashboard to be dynamic which needs the SQL query to be dynamic first.
The starting date is fixed, Oct 1, 2019 for example, but the ending date should be dynamic i.e. it should capture the data after every one day so that the dashboard gets updated daily.
How should my SQL query look like to achieve this purpose?
Use DATE_ADD to add a day as INTERVAL to start date.
SELECT rev_date,AVG(revenue)
FROM table1
WHERE rev_date BETWEEN "2020-06-15" AND DATE_ADD("2020-06-15",INTERVAL 1 DAY)
GROUP BY rev_date;
PS: Using DATE as column_name is not best practice,DATE being a datatype in MYSQL.
Please tell me I am duplicating a question that I haven't found yet.. Here is my question.
I have a DB with all the DateTime columns stored in UTC. I want to know if I do this:
select CONVERT_TZ(event.startingTime,'UTC', city.timezone) as startingTime
left join city on event.cityid = city.id
where startingTime between '2017-01-01 00:00:00' and '2017-01-01 23:59:59'
Is this between filter applying to the processed timezone or not? For example, imagine I have an event starting at 23:00 on UTC time, but the city is GMT+2. Will that event be listed or not? (the "raw" data in the DB is 23:00 so it should appear according to that, but once processed with the timezone is then 1:00 of the following day, so it should not appear according to this other
).
I did a test and it looks like the filter is among the "raw" data before processing, I find it hard to believe, that is why I am asking here (my mysql version is 10.0.21-MariaDB-1~wheezy)
To be suere, you can make a test and try:
select CONVERT_TZ(event.startingTime,'UTC', city.timezone) as startingTime
left join city on event.cityid = city.id
where startingTime
between CONVERT_TZ('2017-01-01 00:00:00','UTC', '+2:00')
and CONVERT_TZ('2017-01-01 23:59:00','UTC', '+2:00')
If you put the convertion in the where clause it will filter correctly
Store as TIMESTAMP, not DATETIME. Then, when fetching, the date&time will be converted according to the timezone specified in the client. No need for CONVERT_TZ.
I'm connecting from MS SQL Server 2014 to a (ServiceNow) MySQL database via OpenQuery(). I would like to filter out records more than 24 hours old.
When I set a static date, it returns the thousands of rows I expect to see. However, when I try to use a calculated field, it runs but returns zero records.
select number, sys_updated_on
from OPENQUERY(ServiceNowUAT,
'Select number, sys_updated_on
FROM DATABASE.[SCHEMA].[TableName]
WHERE sys_updated_on > DATEADD(d, -2, NOW()) ')
I have also used the DATE_SUB() function, and various other forms of syntax. I've tried casting the calculated date as date, datetime, timestamp, varchar, and more. I've tried this in MS Query and SSIS as well. All fail to return results with this query, and other, similar queries once I add the "sys_updated_on > DATEADD(d, -2, NOW())" segment.
If I cast the sys_update_on field as timestamp, it works, but cranks up the processing time from about 10 seconds to 30+ minutes, which, of course, is not ideal (there are a few million rows in the table
The sys_update_on field is in the format "2015-02-10 10:24:17.000000".
The other relevant part is that I am pulling from a ServiceNow MySQL database using ODBC drivers provided by ServiceNow, not MySQL. I do not have a data map, so I cannot say for sure what the data type is. At this point, I'm guessing it's a string of some sort, and not a true timestamp/datetime, but I can't confirm this.
Does anyone have any ideas how to make this work so that it
a. returns results
b. does not take half an hour to run?
SELECT *
FROM OpenQuery(ServiceNowUAT,
'SELECT name FROM DATABASE.[SCHEMA].[TableName]
WHERE CAST(sys_updated_on as TIMESTAMP) BETWEEN DATEADD(DAY, -2, now()) and now()'
)
Spark Interactive SQL Reference.pdf
I have a table that stores some sensitive information but I would like that information to change to NULL when it exceeds 24 hours. How can I do that? I have a column named "last_updated" and stores value like this "2014-02-26 16:25:58".
How can I compare the last_updated value with the current time and if it exceeds 24 hours, the other field will change to "NULL".
Should I put something like UPDATE table SET info=NULL WHERE last_updated > 24hour? I don't know how to compare the last_updated when its 24 hours later.
or is there a function inside MySQL to check automatically without running the query using phpmyadmin?
You can do this with a scheduled job that resets the data. Of course, the time span would be 24-48 hours to the change, if you run the job only once per day.
There is another option. That is to do all the data access via views. Then the view could say:
create view v_table as
select (case when last_updated > now() - interval 1 day then col1 end) as col1,
. . .
from table;
Then, you can then update the data at your leisure -- if you still find that necessary. Access to the data won't be dependent on a job and job scheduler. If all accesses to the data are through the view, then after 24 hours, the data will appear as NULL.
I have the following tables and relevant columns:
state
id int
name text
utc_offset int (minutes)
sale
id int
state_id int
created datetime
amount decimal(8,2)
The value put into the created column is the local server time, which is in state id = 1.
How can I get the total amount of sales for each day for each state?
Is there a better way to handle timezone data?
I don't know MySQL syntax by heart, but is this on the right track?
SELECT sale.name AS name,
adddate(sale.created. interval (state.utc_offset - (select utc_offset from state where id = 1)) minute) AS date,
SUM(amount) AS total_amount
FROM sale
JOIN state ON sale.state_id = state.id
GROUP BY name,date;
NOTE: Storing a utc_offset like this is sloppy, at best, as the utc_offset changes twice a year in many parts of the world. It is usually much better practice to store a timezone name, and use an actual timezone manipulation library to do your date calculations for you.
EDIT
My suggestion for timezone handling would be to modify your state table as follows:
state
id int
name text
tz varchar
Then update your query as follows:
SELECT sale.name AS name,
CAST(CONVERT_TZ(sale.created,'utc',state.tz) AS DATE) AS date,
SUM(amount) AS total_amount
FROM sale
JOIN state ON sale.state_id = state.id
GROUP BY name,date;
Your solution won't work for states with more than one timezone.
Generally, best practice is to store every datetime as UTC in your database. You then use the client's timezone (usually readily available in web apps) to format it for display to the user in their own local timezone.
So you wouldn't be storing state time offsets and your sales per day by state query would be far more straightforward.