Convert date time string to date - ms-access

MS Access Table January2015 has a txndate field with the string "2015-01-01 11:48:00"
The field type is text.
The string needs to be converted to date/time i.e. it should appear in the same format but as a time.
Running this query:
SELECT Format(datevalue(txndate), "dd-mm-yyyy hh:mm:ss") FROM January2015;
gives the output:
01-01-2015 00:00:00
(the time part is being ignored).
How can I fix this?

You can get your desired result with one Format() instead of two.
SELECT Format(CDate(txndate),"dd-mm-yyyy hh:nn:ss") AS Expr1
FROM January2015;
Actually Format() will accept your ymd date string without the need to first convert it to Date/Time, so you could eliminate CDate() if you prefer.
SELECT Format(txndate,"dd-mm-yyyy hh:nn:ss") AS Expr1
FROM January2015;
Note however the datatype of that calculated field will be text, not Date/Time because Format() always returns a string.

SELECT Format(DateValue(txndate),"dd-mm-yyyy") & " " & Format(TimeValue(txndate),"hh:nn:ss") AS Expr1
FROM January2015;

Related

How can i do a count of two columns in mysql?

i want to do a count of two columns in mysql. One of the columns is a string but another is a date like 06/08/2017 and when i do my query i get 0 results.
SELECT count(*) FROM `castigos` WHERE inicio_normal=05/06/2017 AND cod_emplazamiento=1
I have entries of that data but its dont show me anything. Maybe the type of data in the date is wrong?
What should i do?
Add the date field to your select and group by it. Otherwise mysql extensions doesn't recognize you want to group by the date and will aggregrate all the results into 1 column. And since you are getting 0 count, you're where clause must not be working.
Your date format seems malformed. usually YYYY/MM/DD format (standard format);
or specify a format using SELECT STR_TO_DATE('17/09/2010','%d/%m/%Y');
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
the below uses the implicit casting and default date format to convert the string date to a valid date.
SELECT inicio_normal, count(*)
FROM `castigos`
WHERE inicio_normal='2017/05/06'
AND cod_emplazamiento=1
GROUP BY inicio_normal
Otherwise its doing math and comparing that date to the number stored for the date.
Understand dates should be stored in a date datatype and when you query dates you're passing in a string that is being cast to a date datatype for comparison. So you need to use the standard format, or cast your string to a date so the db engine knows how to convert your format to a date.
Try this :
SELECT count(*) FROM `castigos` WHERE inicio_normal="05/06/2017" AND cod_emplazamiento=1 GROUP BY inicio_normal
WHERE inicio_normal=05/06/2017
If you divide 3 by 6 then by 2017 you get a very small value indeed. OTOH if you reformat this as a date (e.g. 20170605, if you gave us a European formatted date - dd/mm/yyyy) then your query will find the rows you showed us.

MySQL STR_TO_DATE ends as a "0" (zero)

SELECT STR_TO_DATE('1.1.2000 0:00:00','%e.%c.%Y') will end up like 2000-01-01
but when I'm trying to do the same on column with values 1.1.2000 0:00:00 by running
SELECT
FirstDisplayedDate,
FirstDisplayedDate = STR_TO_DATE(FirstDisplayedDate,'%e.%c.%Y')
FROM product
I will get zero (NOT NULL!) in every row.
What am I doing wrong? :(
You get what you write: FirstDisplayedDate = STR_TO_DATE(FirstDisplayedDate,'%e.%c.%Y') compares FirstDisplayedDate with STR_TO_DATE(FirstDisplayedDate,'%e.%c.%Y') and if they are not equal returns zero. I think you want to use alias here so:
SELECT
FirstDisplayedDate,
STR_TO_DATE(FirstDisplayedDate,'%e.%c.%Y') AS FirstDisplayedDate2
FROM product
The STR_TO_DATE() function is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format.
STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts.
If the date, time, or datetime value extracted from str is illegal, STR_TO_DATE() returns NULL and produces a warning.
The server scans str attempting to match format to it. The format string can contain literal characters and format specifiers beginning with %. Literal characters in format must match literally in str. Format specifiers in format must match a date or time part in str.
And, the reason to get zero is that unspecified date or time parts have a value of 0, so incompletely specified values in str produce a result with some or all parts set to 0, for example:
mysql> SELECT STR_TO_DATE('abc','abc');
-> '0000-00-00'
mysql> SELECT STR_TO_DATE('9','%m');
-> '0000-09-00'
mysql> SELECT STR_TO_DATE('9','%s');
-> '00:00:09'
Check in your query which format to use for date/time matching and make sure your database rows contains strings, which can be converted as dates and do not assign output values from the function to the same database field:
SELECT
FirstDisplayedDate,
STR_TO_DATE(FirstDisplayedDate,'%e.%c.%Y') AS Converted
FROM product
Here and here is more information how to use the function properly.

SSIS derived column expression for date difference

I have an column in my source table as [Valyyyymmdd] [nvarchar](24) NULL:
Valyyyymmdd
=================
20130503
20120403
00000000
20110523
20100715
I want to get the difference with getdate(), so I used the below query in my source
DATEDIFF(DAY, IIF( [Valyyyymmdd] = '00000000', CONVERT(VARCHAR(8), GETDATE(), 112), [Valyyyymmdd]) , getdate()) as SalesStageAging
but I need to get the Valyyyymmdd and do ssis derived column to get the difference in date resulting in int value.
Kindly provide me the expression which has to be written in derived column expression
Try this:
DATEDIFF("d",[Valyyyymmdd] == "00000000" ? GETDATE() : (DT_DBDATE)(SUBSTRING([Valyyyymmdd],1,4) + "-" + SUBSTRING([Valyyyymmdd],5,2) + "-" + SUBSTRING([Valyyyymmdd],7,2)),GETDATE())
Assuming [Valyyyymmdd] is a string in the format "yyyyMMdd", we split it into the year, month and date parts, insert hyphens between parts to format it as "yyyy-MM-dd" and then cast it as a date i.e. datatype DT_DBDATE. Finally, we use the conditional operator to check if value equals "00000000", and compare either current date, or the date parsed from input with current date to return the result.

MySQL varchar timestamp column stored two different ways

I have a table with device data, one of the columns created_ts -> varchar(30)
The problem: this data in this column contains both linux timestamps and varchars for example:
1381148885
and
2012-09-17 22:13:17
How can I query this column for all records with created_ts > 2013-10-01
I'd opt for distinguishing between the string formats (either 'YYYY-MM-DD' or unix timestamp integer) by checking for a dash character.
I'd consider explicitly converting both of those formats to the DATE datatype, using an appropriate conversion. I'd compare the resulting DATE value with the date literal.
Something like this:
WHERE IF(LOCATE('-',t.created_ts,5), -- which format (yyyy-mm-dd or integer)
STR_TO_DATE(t.created_ts,'%Y-%m-%d %T'), -- convert yyyy-mm-dd string to date
FROM_UNIXTIME(t.created_ts) -- convert string as integer to date
) >= '2013-10-01' -- compare to date literal
Another option would be to convert the string column and the date literal to integer values, and do an integer comparison. (Again, two different conversions for the string column, depending on the format.)
NOTE: I included the hh:mm:ss portion in the conversion with the %T.
When no time component is supplied, the time components is assumed to be midnight (zeros) 00:00:00, and that comes into play depending on whether or not we want to consider
'2013-10-01 07:34:55' > '2013-10-01 00:00:00'
OP query has a greater than comparison. I used a greater than or equal to comparison.
This could all be adjusted, depending on the requirements. We want to be aware that if we aren't careful, some values will get "rounded down" to the previous midnight, and then when we do a greater than comparison, what we're really getting is equivalent to >= '2013-10-02'.
My preference is to make it more explicit. It makes it easier for the reader to understand what the query is actually doing.
UPDATE
I had the arguments in the LOCATE function backwards... the string to search for should be the first argument, the string to be searched is second. That's been corrected in the query above.
Something like this:
select * from yourTable
where created_ts > '2013-10-01'
or from_unixtime(created_ts) > '2013-10-01';

Change datatime to date dd/mm/yy

Im trying to query some data and one of them is a datetime format. I want that is shows dd/mm/yy with no time on it directly form the select. Is this possible?
My query is and the join_date is datetime that i need to be changed to short date:
$query = mysql_query("SELECT id,username,join_date,is_active FROM members",$con)
or trigger_error(mysql_error());
This query goes directy in a Json output array. So i want to convert it directy form the query.
Use the MySQL DATE_FORMAT function for this:
SELECT id, username, DATE_FORMAT(join_date, '%d/%m/%y') AS join_formatted, is_active
FROM members
In this example, the column name for the formatted date will be join_formatted, and its type will be VARCHAR.
The format string returns the date as dd/mm/yy as requested, but I'm personally more comfortable when the date includes the full century. To get the full century, use uppercase Y in the format string: %d/%m/%Y.
try this :
"SELECT id, username, DATE_FORMAT(join_date,'%d/%m/%y'), is_active FROM members"
Use DATE_FORMAT:
SELECT id, username, DATE_FORMAT(join_date, "%d/%m/%y") AS date FROM members;
%Y Year, numeric, four digits
%y Year, numeric (two digits)
For details about date format link