Using Access 2003
Date Column datatype is text
Table1
Date
20090528
20090529
20090530
20090502
20090504
Expected Output
28-May-2009
29-May-2009
30-May-2009
02-May-2009
04-May-2009
...,
How to make a query for the Expected Output Date format?
As VBA code - you could wrap it as a function
Dim strMyDate As String
Dim dteDate As Date
strDate = "20090528"
dteDate = DateSerial(Left(strDate, 4), Mid(strDate, 5, 2), Right(strDate, 2))
MyStr = Format(dteDate, "dd-mmm-yyyy")
Debug.Print MyStr
As a Data type in a Table - If you append the data to a table where the field is Date/Time formatted then you can specify the format on the Form / Table i.e. at output time.
I find that Cdate does not work for me.
Format(DateSerial(Left(Field1, 4), Mid(Field1, 2, 2), Right(Field1, 2)), "dd-mmm-yyyy")
CDate should do what you want...
Related
Recently at the company where I work I've needed to import a bunch of orders from excel into our database so that they can be processed and shipped. Many of these excel documents have hundreds of columns. I'm looking for a clean way to convert multiple rows and columns containing order data into a dump of SQL queries.
Here is what I'm working with.
Each order is a single row identified by the OrderID, and the quantity of each product is defined in the cell which corresponds to the ProductID.
I'm using the following formula to convert each cell (if filled) into a value for an SQL query:
=CONCATENATE(IF(ISBLANK(B3),"",("("&B3&", "&B$1&", "&$A3&"), ")))
The result for each filled cell is something like:
(1, 22, 1),
What I want to be able to do, is concatenate each cell (if filled) into one long string per order without having hundreds of columns filled with the same formula. Like this:
(5, 12, 23), (5, 13, 23), (100, 7, 23), (50, 1, 23), (100, 3, 23),
Thanks.
Try This -
VBA Code
Function InsStr(Product As Range, Order As String, Qty As Range)
Dim tmpString As String: tmpString = ""
Dim tmpCount As Integer: tmpCount = 1
For Each cell In Qty
If cell <> "" Then
If tmpCount > 1 Then tmpString = tmpString & ","
tmpString = tmpString & "(" & cell & "," & Product(tmpCount) & "," & Order & ")"
End If
tmpCount = tmpCount + 1
Next cell
InsStr = tmpString
End Function
and use Formula as =InsStr(B1:X1,A3,B3:X3)
I wanted to convert my date format From MMMM dd,yyyy to yyyy-MM-dd.
I tried using the following:
SET #dt_to = STR_TO_DATE(dateTo, '%d-%m-%Y');
but returns a NULL value.
How will I convert my date to yyyy-MM-dd format in MySQL?
EDITED:
I am creating a procedure in which the value of dateTo was received in the parameter. It is a date in MMMM dd, yyyy format. E.g. October 10, 2015.
NOTE:
The whole query does not return NULL when I use:
SET #dt_to = dateTo;
To convert the date format first you need to use STR_TO_DATE to convert the input string to a date value
SET #dt_to = STR_TO_DATE(dateTo, '%M %d,%Y');
and then convert that date value to your required format
SET #dt_converted = DATE_FORMAT(dt_to, '%Y-%m-%d');
or all in 1 go
SET #dt_to = DATE_FORMAT(STR_TO_DATE(dateTo, '%M %d,%Y'), '%Y-%m-%d');
If it's returning null then that means the extracted datetime value is illegal. You can try like below. See MySQL Documentation for more information.
SELECT STR_TO_DATE('October 10, 2015','%M %d,%Y');
I want to generate random number using Date() to format it like for example: ddmmyyyyHhNnSs
How do I achieve that? Is that even possible?
I was kinda hoping I can do it easy way by the expression builder but I seem to fail on each approach ;)
This number need to populate txtID field and it will be unique identifier for each database entry.
That's quite easy - with a twist.
Problem is that Rnd only returns a Single and the resolution of this only allows for 10000000 unique values. As you request a resolution to the second and with 86400 seconds per day, that only leaves a span of 115.74 days while the range of Date spans 3615899 days:
TotalDays = -CLng(#1/1/100#) + CLng(#12/31/9999#)
To overcome this, use Rnd twice which will result in 1E+15 possible values or 11574074074 days - way beyond what's needed:
RandomDouble = Rnd * Rnd
Now, to limit the possible values to fit into the range of data type Date, just follow the documentation:
RandomValue = (UpperValue - LowerValue) * Rnd + LowerValue
and apply the date values:
RandomDouble = (CLng(#12/31/9999#) - CLng(#1/1/100#)) * Rnd * Rnd + CLng(#1/1/100#)
This, however, will result in values containing unwanted milliseconds, thus perform the proper conversion to Date value using CDate which will round to the nearest second, and you have the final expression:
RandomDate = CDate((CLng(#12/31/9999#) - CLng(#1/1/100#)) * Rnd * Rnd + CLng(#1/1/100#))
Use the value as is if your field is of datatype Date or - if text - apply a format to this with Format(RandomDate, "yyyymmddhhnnss") and a sample output will be:
01770317032120
01390126010945
50140322081227
35290813165627
09330527072433
20560513105943
61810505124235
09381019130230
17010527033132
08310306233911
If you want numeric values, use CDec to convert (CLng will fail because of overflow):
RandomNumber = CDec(Format(RandomDate, "yyyymmddhhnnss"))
All said, I'm with #Bohemian - if you just want a unique timestamp and have less than one transaction per second, just use data type Date for your field and use Now:
TimeStamp = Now()
and apply a format to this of yyyymmddhhnnss.
However, Multiplying random numbers together alters the
probablility distribution:
Uniform Product Distribution
Thus, a better method is to create a random date, then a random time, and possibly a random count of milliseconds - I wrote above, that CDate rounds a value to the nearest second; it doesn't, only whenever Access displays a date/time with milliseconds the displayed valued is rounded to the second.
So I modified the function to take care of this:
Public Function DateRandom( _
Optional ByVal UpperDate As Date = #12/31/9999#, _
Optional ByVal LowerDate As Date = #1/1/100#, _
Optional ByVal DatePart As Boolean = True, _
Optional ByVal TimePart As Boolean = True, _
Optional ByVal MilliSecondPart As Boolean = False) _
As Date
' Generates a random date/time - optionally within the range of LowerDate and/or UpperDate.
' Optionally, return value can be set to include date and/or time and/or milliseconds.
'
' 2015-08-28. Gustav Brock, Cactus Data ApS, CPH.
' 2015-08-29. Modified for uniform distribution as suggested by Stuart McLachlan by
' combining a random date and a random time.
' 2015-08-30. Modified to return selectable and rounded value parts for
' Date, Time, and Milliseconds.
' 2015-08-31. An initial call of Randomize it included to prevent identical sequences.
Const SecondsPerDay As Long = 60& * 60& * 24&
Dim DateValue As Date
Dim TimeValue As Date
Dim MSecValue As Date
' Shuffle the start position of the sequence of Rnd.
Randomize
' If all parts are deselected, select date and time.
If Not DatePart And Not TimePart And Not MilliSecondPart = True Then
DatePart = True
TimePart = True
End If
If DatePart = True Then
' Remove time parts from UpperDate and LowerDate as well from the result value.
' Add 1 to include LowerDate as a possible return value.
DateValue = CDate(Int((Int(UpperDate) - Int(LowerDate) + 1) * Rnd) + Int(LowerDate))
End If
If TimePart = True Then
' Calculate a time value rounded to the second.
TimeValue = CDate(Int(SecondsPerDay * Rnd) / SecondsPerDay)
End If
If MilliSecondPart = True Then
' Calculate a millisecond value rounded to the millisecond.
MSecValue = CDate(Int(1000 * Rnd) / 1000 / SecondsPerDay)
End If
DateRandom = DateValue + TimeValue + MSecValue
End Function
Format now() and cast to a long:
select CLng(format(now(), 'ddmmyyyyhhnnss')) as txnId
Although this is not "random", it is unique as long as there are never more than one transaction per second (confirmed in comment above).
I want to migrate dates from integer format to DATETIME.
My dates in my old database have the following format:
olddb.table.date = 20131114 (INT)
olddb.table.time = 900 (INT) (9 AM, 24h clock)
new database:
newdb.table.datetime = 2013-11-14 9:00:00 (DATETIME)
How would I migrate this with purely SQL?
SELECT CAST(CONCAT(DATE(olddb.table.date),' ',TIME(olddb.table.time*100)) AS DATETIME);
You can convert your date part with STR_TO_DATE (MySQL documentation):
STR_TO_DATE(olddb.table.date, "%Y%m%d")
And for the time part, you can use the same function :
STR_TO_DATE(olddb.table.time, "%h%i")
Then, you can concatenate date and time parts and apply function to concatenation result :
STR_TO_DATE(concatenated_value, "%Y%m%d%h%i")
Where concatenated_value is built with :
CONCAT(olddb.table.date, olddb.table.time)
Try this:
cast(
concat(
table.date div 10000, '-',
lpad((table.date mod 10000) div 100, 2, '0'), '-',
lpad(table.date mod 100, 2, '0'), ' ',
table.time div 100, ':',
lpad(table.time mod 100, 2, '0'))
as datetime)
Here's a working example:
http://www.sqlfiddle.com/#!2/1ff75/1
I had string in the order of ddmmyy i.e '231013' which i need to convert it into dd-mm-yy format
I tried following but it is giving yyyy-dd-mm format
I know 105 will give dd-mm-yy format
DECLARE #ITEMS AS NVARCHAR(10)='231013'
SELECT CONVERT(DATE,#ITEMS,105)
but it is returning
2023-10-13
How to convert string in the order of ddmmyy to date dd-mm-yy
Convert string in the order of ddmmyy to date dd-mm-yy
DECLARE #ITEMS AS NVARCHAR(10)='231013'
SELECT CONVERT(varchar(25),
CONVERT(DATE, SUBSTRING(#ITEMS, 1, 2) + '-' + SUBSTRING(#ITEMS, 3, 2) + '-' + SUBSTRING(#ITEMS, 5, 2), 5),5)
result will be
It seems that the mmddyy style is not natively supported by CONVERT; to perform the conversion, I would just adapt the input string to one of the supported styles, like mm-dd-yy:
SELECT
CONVERT(DATE, SUBSTRING(#ITEMS, 1, 2) + '-' + SUBSTRING(#ITEMS, 3, 2) + '-' +
SUBSTRING(#ITEMS, 5, 2), 5)
If you just need to change the format (Not converting to a DateTime type), you could Stuff() it:
Declare #Items as nvarchar(10)='231013' --ddmmyy
Select Stuff(Stuff(#Items,3,0,'-'),6,0,'-') --dd-mm-yy
Fiddle demo