I have a MS SQL Server DateTime field, and Im trying to search all records that are in between a date range:
mySqlString = "select * from users where signupDate >=#from and signupdate <=#to"
The two variables containing the date range come with format MM/dd/yyyy (dataFrom and dataTo, so Im replacing #from and #to at the string as follows:
datefrom = new Date(dataFrom);
dateto = new Date(dataTo);
req.input('from', sql.DateTime, datefrom )
req.input('to', sql.DateTime, dateto )
But I do not get any result.
What's the best approach to get this working properly?
You can always use CONVERT to accommodate your SQL query to your input format. In your case its format 101: select convert(varchar, getdate(), 101) ---> mm/dd/yyyy
So your query should look like
where (signupdate >= CONVERT(date, #from, 101)) AND (signupdate <= CONVERT(date, #to, 101))
This way you won't worry about the time of the stored date
req.input('from', sql.Date, (dataFrom))
req.input('to', sql.Date, (dataTo))
Assuming you checked if dataFrom and dataTo have valid dates.
Related
I want to DELETE every record with a datetime (e.g. 2022-11-10T??????????) using a date (e.g. 2022-11-10) from a variable. For example:
last_Date = datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')
And use SQL like this:
cursor.execute("""
DELETE FROM alpaca_stock_prices_4H WHERE datetime = (?)
""", (last_Date))
The value of the variable last_Date is a formatted as YYYY-MM-DD date string but the column datetime in the table contains timestamps in the format YYYY-MM-DDT?????????? so you can't compare them with the = operator.
Use SQLite's strftime() or date() function to format the datetimes also to YYYY-MM-DD:
cursor.execute("""
DELETE FROM alpaca_stock_prices_4H WHERE strftime('%Y-%m-%d', datetime) = ?
""", (last_Date,))
or:
cursor.execute("""
DELETE FROM alpaca_stock_prices_4H WHERE date(datetime) = ?
""", (last_Date,))
If what you actually want is delete all yesterday's rows you can do it without passing a parameter, by using only SQL code:
cursor.execute("""
DELETE FROM alpaca_stock_prices_4H WHERE date(datetime) = date('now', '-1 day'))
""")
I am trying to compare a string variable in my Python script with a varchar variable in SQL. Following is a snippet from my code:
todaysDate = datetime.now().date().strftime('%d-%m-%y')
read(conn, f'select cou_id from NewCourier where date_created = {todaysDate}'
Where:
todaysDate => variable in python storing current date
cou_id & date_created => columns in a relational DB table NewCourier
I tried:
read(conn, f'select cou_id from NewCourier where CAST(date_created AS INT) = {todaysDate}
I've also gone through a couple of questions and solutions on StackOverflow and other sites and found only this solution similar to my problem but I didn't get a clear idea of how to solve it.
Thanks!
Python 3.8.5
Microsoft SQL Server Management Studio 18
It seems like the column data type is integer and you are trying to compare it to a string. If you can find code in your program that inserts into the table you should be able to figure out how to convert your date time properly
Instead of creating a variable to store the current date in Python, I used the GETDATE() method of SQL and converted it to varchar using the CONVERT() method in SQL which solved my problem
(i.e., it allowed me to get the courier IDs of all the records generated on that particular day).
Here's the updated line:
read(conn, f'select cou_id from NewCourier where date_created = CONVERT(varchar, getdate(), 23)
I need to get the data from MongoDB between two given dates. The same mongo db query is working for ( yy-mm-dd hh:mm:ss.ms ) format but it is not working for ( dd-mm-yy hh:mm:ss) format.
Sample Data in DB
{
"name":"user1",
"Value":"Success",
"Date": "02-06-2020 00:00:00",
"Status":"available",
"Updated_on":"2021-01-09 00:00:00.0000"
}
Python:
start_date = "02-06-2020 00:00:00"
end_date = "11-06-2020 10:16:41"
data = list(db.collection.find({"Date":{"gte":start_date,"Slte":end_date},"Value":"Success"},{'_id':False,"Date":1,"name":1,"Value":1}))
print(data)
I need to get the data based on the "Date" field.
The problem is it is giving extra data than the start_date and end_date.
Example: if my start_date is "02-06-2020 00:00:00"and end_date is "11-06-2020 10:16:41", it is giving data from "02-04-2020 00:00:00" to "11-06-2020 10:16:41"
Any idea to achieve this and please explain why it is not taking dates correctly.
I have a widget called 'filedate' where you can specify a date, were you enter the date in the format 'yyyy-mm-dd', my example will use '2019-10-01' .
I get the value from the widget with the following:
val fileloaddate = dbutils.widgets.get("filedate")
If I print the fileloaddate, it shows 2019-10-01, I need to use it in a query so if I do a 'select to_timestamp(${fileloaddate}, "yyyy-mm-dd")' it errors as it's seeing the variable as '((2019 - 8) -18). If I cast the string to a date, for example
select to_timestamp(to_date(${prundate}), "yyyy-mm-dd")
with the error of 'cannot resolve 'CAST(((2019 - 8) - 18) AS DATE)'
select to_timestamp(to_date('2019-10-01'), "yyyy-mm-dd")
works fine. I have googled around the answer but can't seem to see what I doing wrong.
thanks
Azure DataBrick you can use getArgument to convert date into desire output
dbutils.widgets.text("x","2018-09-12")
select to_timestamp(to_date(getArgument("x")), "yyyy-mm-dd")
hope this helps you
If Scala is disabled then you need to use the legacy method for retrieving sql parameters from widgets
.
select * from database where parameter='$file'
For sql I can pass the date as a text
%sql
create widget 'StartDate' DEFAULT 'YYYY-MM-DD' (this is simply text to remind the user to input the date in a format the sql query can use)
select * from my_table Between '$StartDate' and '$EndDate'
https://docs.databricks.com/notebooks/widgets.html#legacy-input
https://docs.databricks.com/notebooks/widgets.html
i have created a column for date using workbench now i have to update all the values of that column to current date either using sql query or using python code
mydb = mysql.connector.connect(host="localhost", user="root",
password="afif123", db="library")
mycursor = mydb.cursor()
fill = '''UPDATE issued_book SET Today = current_date()'''
mycursor.execute(fill)
#or
today = datetime.datetime.now()
fill = '''UPDATE issued_book SET Today = %s'''
mycursor.execute(fill, [today])
any of above two attempt doesn't helping
i have kept datatype of date column as DATETIME()
To do it in mysql, just modify your first query a bit...
update issued_book set today = now()
instead of current_date(), which is not a mysql function.