Why Str(CDbl(Now())) returns 43185.44166666667? - ms-access

Today is 26/03/2018.
Why Str(CDbl(Now())) returns 43185.44166666667?
and
Str(CLng(Now())) returns 43185? I am expecting a string starting with 26032018...

Dates in Access and VBA are defined as doubles where the whole numbers are days, and the fractions are fractions of a day, starting from 30-12-1899.
If you want to cast a date to a formatted string, use Format:
Format(Now(), "ddmmyyyy") will return your expected result.

A Date is a the number days since 31 December 1899, and the time component is the decimal fraction of a day.
That's why you're getting a numeric value.
If you want to format the Date as a ddmmyyyy string, you should use the Format$ function
Debug.Print Format$(Now(),"ddmmyyyy")

Related

Convert date format while using date_add function in SQL

I have a variable (order_date_key) that contains the number of days since 1/1/1900. For example, 42711 represents December 9, 2016 or 42711 days since 1/1/1900. I want to convert that variable to the week of the year (e.g., 2016-50)
I was able to convert the variable to yyyy-mm-dd format using the date_add function, but when I try to use the DATE_FORMAT function with it, it returns an incorrect week number. For example, 2016-12-09 converted to 2016-5, but that date isn't the 5th week of the year.
Here's the code I'm using. The format also doesn't seem to work when I use the '%' symbol.
SELECT order_date_key,
DATE_ADD('1900-01-01', order_date_key) AS Year_month_day,
DATE_FORMAT(DATE_ADD('1900-01-01', order_date_key), 'Y-u') as year_week
Sample data: 42711, 42714, 42715, 42720
Desired output: 2016-50, 2016-51, 2016-51, 2016-52

How to convert an SSIS Date Variable to String In order to return the 2 digit month value?

I have an SSIS variable #[User::SelectedDate] from which I want to return the 2 digit month value to use in an expression. How do you do this?
(DT_STR,4,1252) DATEPART ("yy",#[User::SelectedDate]) returns the year as text, but how do you get the two digit month value?
I tried (DT_STR,4,1252) DATEPART("mm",#[User::SelectedDate]) to return the month but it does not work. It returns "1" while the value is 2021-10-31, so no idea what I am doing wrong. Important to add that I need the two digit value, so "02" for February, not "2"
Note: The variable is not the current date. I have seen many similar questions where getdate() is used as a solution. This wouldn't work in this case, as the date is selected from a database table
Any help will be much appreciated.
Have already wasted an hour on this.
Thanks!
If you are having trouble with datepart. you can always use script component or task.
Component:
string strMonth = Variables.SelectedDate.ToString("MM"); //Note capital MM as mm means minutes
Task:
string strMonth = Dts.Variables["SelectedDate"].Value.ToString("MM");

How to check if a string contains a date?

I'm trying to iterate on a DataSet, this contain a results of query such as SELECT * FROM tb 1, now the first three field contains a date, the format saved in the database table is this:
yyyy-MM-dd HH:mm:ss
but the code return this:
yyyy/MM/dd HH:mm:ss
in particular this:
For z = 1 To ds.Tables(0).Columns.Count - 1
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString())
Next
So I need to recognize if the current string have this format: yyyy/MM/dd HH:mm:ss and parse it into: yyyy-MM-dd HH:mm:ss I tough to a regex pattern for recognize it, but I'm not an expert of regex. Anyway, if there is another solution I'll glad to see. Note that only the first three value and the last one of the table is date, the other values aren't date but contain integer or other string value.
Dates do not have a format. From MSDN:
Represents an instant in time, typically expressed as a date and time of day.
...
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar...For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight.
So, a DateTime is just a Big Number. Representing them as "dd/MM/yyyy" is part of the magic of the DateTime type. Part of the issue is this:
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString())
Row items are Object. It wont act like a DateTime type unless/until you get it into a DateTime variable. That print as a DateTime simple because the DataTable knows the underlying type; but it will use the default format for your Culture. This makes it look like dates have a built in format (or even that the "format changed" if you tried to set it to something), but you are a human and 635882810022222112L would not make sense to most of us.
To change the output style, you first need to get it into a DateTime variable. Apparently, a preliminary step is to determine if an arbitrary column is a Date. Rather than testing the "format" of the output, test the underlying data type. This does assume a proper DateTime column in the DataTable:
If ds.Tables(0).Columns(n).DataType = GetType(DateTime) Then
'...
End If
' Or:
If ds.Tables(0).Rows(x)(z).GetType Is GetType(DateTime) Then
'...
End If
Then to change the display, first get it into a DateTime variable:
Dim dt As DateTime
If ds.Tables(0).Rows(x)(z).GetType Is GetType(DateTime) Then
dt = Convert.ToDateTime(ds.Tables(0).Rows(x)(z))
' cant change "format" but you can change how it displays:
Console.WriteLine(dt.ToLongDateString)
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm tt"))
Console.WriteLine(dt.ToString("dd MMM, yyyy"))
End If
An easier way to get and convert to DateTime is to use the Field(Of T) extension:
Dim dt = ds.Tables(0).Rows(x).Field(Of DateTime)(y)
when I peform the insert usually do this: Date.Now.ToString("yyyy-MM-dd HH:mm:ss") so I apply a format to date to insert... if I don't format correctly the date as I shown I get this value 0000-00-00 00:00:00
That doesn't apply a format to a date. It converts the DateTime to a string. While "yyyy-MM-dd HH:mm:ss" is the correct format to use when passing date data as a string to MySql, it is not needed. The MySQL Data provider knows how to convert a Net DateTime var to the data MySql needs/wants and back again -- that's its job.
' this will work fine
cmd.Parameters.Add("#SomeDate", MySqlDbType.DateTime).Value = myDateTimeVar
The format requirement you read about is the what you need to use in the MySql shell or WorkBench UI because you are entering text/string there...from the keyboard. It does not mean code must convert DateTime variables to string in a specific format for storing.
I ended up using this
Try
Dim theDate As DateTime = dr.Item(colName)
Return theDate
Catch
' do something
End Try
I would be happy to see a better method.
Based off of what you seem to be asking a simple replace would do
For z = 1 To ds.Tables(0).Columns.Count - 1
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString().Replace("/","-"))
Next
if it comes in with / they are changed to - if it comes in with - they remain intact.
Depending on the flexibility you want in this, it may be necessary to TryParse to ensure that the value you're working with is actually a valid datetime.

Convert Date to a number in mysql (Like how a date converts in Excel)

What is the correct function to use when i want to convert a date to a number. Like when you enter a date in excel for example (now()) and then it will show the date but when you click on the number format it will show you a number.
I tried to use Unix timestamp but its not exactly the output i was looking for.
The date i entered is today's date
=now()
and the output i was hoping to get is
42146
what's the correct function to get this result in mysql?
Thank you
Microsoft Excel bases date serial numbers from Jan 1, 1904 or from Jan 1, 1900 (depends on the setting in the workbook.)
To generate a date "serial number" similar to what Excel uses, just calculate the number of days between NOW() (or whatever date you want to convert), and the base date. For example:
SELECT DATEDIFF(NOW(),'1900-01-01') AS excel_serial_date_1900
(You might need to add 1 or subtract 1 to get the exact same value that Excel uses, I've not tested that.) If your Excel workbook is using the 1904 based serial date, use '1904-01-01' as the base date in the expression.
Note: the DATEDIFF function returns integer number of days, operates only on the "date" portion, and doesn't include any fraction of a day.
Reference: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff

Using DateDiff to Calculate the Difference in Days

In the expression below, I am trying to calculate the difference in days between the Created date and today's date. If it is less than 30 days then output "1", otherwise output "0"
=IIF(DateDiff("d",(Format(CDate(Fields!Created.Value), "MM/dd/yyyy")), (Format(CDate(Today()), "MM/dd/yyyy")))<30, "1", "0")
Both values in "Created" and Today() are formatted with the date and time so I use Format and CDate to extract just the date. When I run the report, it displays all "0" and I know that is incorrect. Is there something wrong with the expression?
Yes, there are a number of things wrong with that expression: You take dates, explicitly convert them to dates, then using format convert them to strings then implicitly convert them back to dates to do your date comparison. That's a lot of heavy lifting for no benefit. You are also using SQL syntax in a VBA expression. Your result is also a string when it probably should be an integer.
Your expression should look more like this:
=IIF(DateDiff(DateInterval.Day, Fields!Created.Value, Today) < 30, 1, 0)