Replace/Convert via sql while querying csv - sql

I'm trying to query a CSV via ADODB/SQL and the results need to be ordered by date. The challenge is that the date is in dd-mon-yy format. I tried to use CONVERT and REPLACE but Excel 2003 keeps throwing Automation Error at me. I've tried both of the following separately.
With CONVERT:
"SELECT CONVERT(datetime,[Business Date],106) from [filename.csv]"
With REPLACE:
"SELECT REPLACE([Business Date],'-',' ') from [filename.csv]"
I don't have control over the CSV so manually correcting the dates is not an option.

How about:
SELECT CDate(Right([Business Date],2) & "/" _
& Mid([Business Date],4,3) & "/" _
& Left([Business Date],2)) from [filename.csv]
Depending, of course, on your locale. I do not believe Convert and Replace are available to ADO.

Related

Managing Dates Query in ACCESS with SQL Server background

I am doing my project on ACCESS for the "front" part with the forms, and in SQL Server for the "back-end" part with the databases, everything works fine exect for one thing : I don't understand how to filter Access's forms with SQL Server's date type.
I have looked for everything on the web and all the solutions don't seems to work for my case (maybe because of the ACCESS/SQL Server env), things like :
Me.Form1.Filter = "[date born] = #" & Format("11/04/2022", "dd/mm/yyyy") & "#"
'with VBA directly
SELECT * FROM dbo_Person WHERE [date born] = Format('11/04/2022','dd/mm/yyyy') ;
-- with SQL Request from ACCESS on the table
but everytime I have the same issue: nothing is displayed even if there is rows who have this date in the right column. It is like if no row respected this condition even though it is not the case.
I'm guessing it may be an issue with SQL Server Date type who doesn't convert well on ACCESS.
That is how is desplayed by default in my ACCESS the dates of the column (my ACCESS software is setuped in French btw)
The data is well recognize as "not a String" because when I try to filter with a String an error appears.
Can someone help me to know where I missed something or misstyped something please ?
Edit : the Date column seems actually to be recognized as Short String in ACCESS, it may be the issue but still don't know how to fix because it is indeed a date type on sql server
"Texte court" can be translated to "short text" or short String
First, if you use data type DateTime2 in SQL Server, that will be read as text in Access.
So, change that to DateTime.
Next, use the universal format yyyy-mm-dd when filtering date values:
FilterDate = DateSerial(2022, 4, 11)
Me.Filter = "[date born] = #" & Format(FilterDate, "yyyy\/mm\/dd") & "#"
' Linked table in Access:
Sql = "SELECT * FROM dbo_Person WHERE [date born] = #" & Format(FilterDate, "yyyy\/mm\/dd") & "#;"

Surround Number to interpret as Text

I have a CSV files and one of the fields named period stores number. Now within SSRS, I need to make use of a multi-value parameter, due to the workings of SSRS to use this with a CSV I need to do Join(Parameters!Period.Value, ",").
This issue with this is then my data set has the following query:
="SELECT WarehouseZone, RevenueStream,ClientID,CONVERT(varchar(10),Period) FROM BudgetCSV.csv
WHERE WarehouseZone IN ('" & Join(Parameters!Warehouse.Value,"', '") & "')
AND (RevenueStream IN ('" & Join(Parameters!AnalysisCode.Value,"', '") & "'))
AND (ClientID IN ('" & Join(Parameters!Customer.Value,"', '") & "'))
AND (CONVERT(varchar(10),Period) IN ('"& Join(Parameters!Period.Value, "','") & "'))"
This code works fine for the zone, stream and client, I get a data type mismatch of period.
Is there any way using Powerquery to have the csv think that period is text. I have seen somewhere that by making the column say ="1" it would interpret that as text instead of an integer.
You can either use Text.From or Number.ToText to convert a number value to text.
Text.From(Period)
or
Number.ToText(Period)
EDIT: THE FOLLOWING DOES NOT WORK - Using this method only works on an individual level, luckily there is a better solution
So I mentioned in an earlier post that I could get around using the Join by creating a single parameter for each period, but this was causing an issue where if I selected 10, 1 would also be return because, strings.
This was due to me using the following code on each of these parameters
=iif(Join(Parameters!Period.Value,",").ToLowerInvariant().Contains("10"), "10",-999)
Simple changing that code to the following
=iif(Join(Parameters!Period.Value,",") = 1, 1,-999)
So the issue is solved, but any input on how I could get a more dynamic solution working would be great as there may be times in the future when I need to use a longer list of numbers.
Adding as another answer for posterity to see my mistakes.
There is in fact a much easier way to achieve this.
By adding the above filter onto the dataset you can achieve this without the need for any SQL or any extra parameters. This is probably obvious to anyone who has been working with SSRS for any length of time, but I'm rather new to it myself.

Conversion failed when converting date and/or time from character string

i have been getting this error "Conversion failed when converting date and/or time from character string." on a code that previously worked untill i migrated the database to a 2008 server.
Here is the code;
query1 = "INSERT INTO RequisitionSummary (RequisitionDate, RequisitionAmount, IssuingAmount) VALUES('" & dtpRequisitionDate.value & "','" & txtRequisitionAmt.Text & "','" & txtIssuingAmount.Text & "')"
I will appreciate if the error can be spotted because i have tried different approaches including converting the value in a variable befor inserting.
Thanks
Nelson
depending on the regional setting of your server vs your computer, you will get this error if it does not match.
You might need to convert into a date and then apply a formatting to be sure it match the setting in your database.
Or you can make a SQL Convert to regional setting in the sql (look at : http://msdn.microsoft.com/en-us/library/ms187928.aspx for more details on convert)

Access/VBA and SQL WHERE clause format issue

I have read a huge pile of problems and solutions, and I just can't figure out what I'm doing wrong.
BounceDate = DateValue(txtBounceDate.Value)
bncSql = "DELETE _BounceMaster.* FROM _BounceMaster" & _
" WHERE _BounceMaster.DateCheck >= #" & BounceDate & "#;"
DoCmd.RunSQL bncSql
_BounceMaster.DateCheck is in Date/Time format, which I think may be the issue, but I can't figure out what different format it should be in, or how to get there. As best as I can tell, BounceDate is correct - even using CDate didn't make a idfference. I have gotten both data mismatch errors, and currently, with code as above, I am getting syntax errors. What am I doing wrong?
I suppose the problem comes from date formatting. The BounceDate variable is DateTime type, so when you concatenate with string type variable, VBA automatically casts DateTime variable into String type using date format from your regional settings.
As I correctly remember, SQL interpreter from MS Access feels comfortable only with mm/dd/yyyy date format, so please try this:
BounceDate = DateValue(txtBounceDate.Value)
bncSql = "DELETE _BounceMaster.* FROM _BounceMaster" & _
" WHERE _BounceMaster.DateCheck >= #" & Format(BounceDate, "mm/dd/yyyy") & "#;"
DoCmd.RunSQL bncSql
It should be
DELETE FROM _BounceMaster
not
DELETE _BounceMaster.* FROM _BounceMaster
You should be using parameterized queries, as your code is subject to SQL injection attack.

dd/mm automatically got changed to mm/dd

I coded something using Date statement in Access VBA. It was working fine until the start of this month, but now I am seeing that the Date has automatically changed the format from dd/mm/yyyy to mm/dd/yyyy. Has anyone else encountered the same problem?
The default Access SQL date format, regardless of locale, is mm/dd/yyyy. If you use an invalid date format, it will 'helpfully' try to convert that to a valid date for you.
So, if you use '30/09/2008', it will recognize you're using dd/mm/yyyy, and convert it appropriately. However, a value like '10/01/2008' is a valid mm/dd/yyyy value to begin with, so it will not be converted, and stored incorrectly in case you actually meant dd/mm/yyyy....
The solution is to always convert your date values to a mm/dd/yyyy string prior to using them in Access SQL statements. You have to be a bit careful here, as using VBA date format masks may not work entirely as you'd expect on non-US locales (e.g. 'helpfully' interpreting "mm/dd/yyyy" as "the localized short date format"), so please test carefully using your particular Access/VBA version.
Access requires a date to be unambiguous. It is generally recommended that you use yyyy/mm/dd, regardless of locale. For example:
strSQL="SELECT SomeDate FROM tblT WHERE SomeDate=#" & Format(DateVar, "yyyy/mm/dd") & "#"
Try this code:
stLinkCriteria = "[ProjectDate] Between #" & Format(CDate(Me![txtDateFrom]), "mm/dd/yyyy") & "# And #" & Format(CDate(Me![txtDateTo]), "mm/dd/yyyy") & "#"
It works for me.
This works:
sentenciaSQL = "UPDATE Numeraciones " & _
"SET Valor = " & Valor & ", " & _
"Fecha = #" & **Format(fecha,"mm/dd/yyyy HH:nn:ss") & "#, " &** _
"Id_Usuario = " & Id_Usuario & _
" WHERE Nombre = '" & Nombre & "'"
I have been very successful using the datevalue() function. When getting dates from unbound controls it seems to be clever enough to interpret the format "dd/mm/yyyy" correctly. Thus, a Jet SQL query like
"Select * from DateTable where StartDate = datevalue(" & me!TxtStartDate & ");"
seems to work every time.
I was experiencing same issue while trying to build a SQL string through VBA.
My locale settings use dd/mm/yyyy and I was trying to put into SQL statement data taken from an unbound textbox in a form.
The issue was due to the fact I was declaring, let's say, varMyDate as "date" type, so the engine reverted the format back even after the format.
Since you are really building a string the logical and proper data type is "string", and that solved the problem.