I have been provided with a file whereby the dates are in the aforementioned format.
I have never seen this format before so am I going to have to separate the data out and convert each section or is this actually a known format?
Cheers
Most databases have some sort of to_date() or parse_date() functionality . . . except SQL Server.
If you are using SQL Server, then this should work:
select cast(stuff('10JAN2000:00:00:00', 10, 1, ' ') as datetime)
select to_char(to_date('10JAN2000:00:00:00', 'ddmonyyyy:hh24:mi:ss'),'yyyy-mm-dd : hh:mm:ss') from dual;
as suggested by others, knowing which database changes the answer. SQL Server isn't keen on supporting date parse function unlike oracle, please read through below threads, might help with better understanding-
Sql Server string to date conversion
to_date in SQL Server 2005
In oracle of course, we can do something like this-
select to_date('10JAN2000:00:00:00', 'ddmonyyyy:hh24:mi:ss') from dual;
or
select to_timestamp('10JAN2000:00:00:00', 'ddmonyyyy:hh24:mi:ss') from dual;
Related
I know nothing about SQL but am trying to order a memorial keychain for someone who DOES and I'd like to put the date of their pet's passing in SQL form ... Is this correct?
SELECT DATE("2019-11-05");
It depends on the database. In general, date and string constants should use single quotes. The SQL standard for a date literal is:
SELECT DATE '2019-11-05'
However, databases might also prefer:
SELECT DATE('2019-11-05')
SELECT CAST('2019-11-05' as DATE)
Or perhaps something else.
I use Classic ASP and SQL Server 2012. I have a program that inputs into the database using now(). Originally it was formnatdatetime(now(),2).
For the majority of time everything was fine but for some reason (which is why I'm asking) occasionally it would put the date in the database in the wrong format. So instead of ddmmyyyy it would be mmddyyyy.
I cannot see how or why when the code is the same, the database is the same. I assume now() or getdate() in TSQL is server specific.
You could use the format function
SELECT FORMAT(GETDATE(),'ddMMyyyy')
Results:
11042015
MS SQL Server 2012 does not know NOW() function! As Gordon Linoff mentioned, you need to use GETDATE() instead!
I'd suggest to read this: Date and Time Data Types and Functions (Transact-SQL) together with: CAST and CONVERT (Transact-SQL)
SELECT CONVERT(VARCHAR(30), GETDATE(), 103) AS CustomDateFormat
-- returns: dd/mm/yyyy
Note that sometimes, sql server can't convert date stored as a string, so you need to use SET DATEFORMAT, especially when sql server uses different date format:
SET DATEFORMAT dmy;
SELECT CONVERT(VARCHAR(30), '11/04/2015', 121) AS NewDateTime
We use firebird as a local testing database and most of our clients use SQL Server or Oracle.
This works in MS SQL Server, but not in Firebird. (haven't tested it n Oracle yet)
CONVERT(char(8),MAX(p.end_Time)-MIN (p.start_Time),8) as duration
is there a way to acoomplish this same thing for (Firebird, Oracle, and MS Sql Server)?
thanks
For Firebird you need to use DATEDIFF to obtain a difference between two timestamps (datetimes). This BTW is similar to the SQL Server DATEDIFF. If you then want to cast it to char, you can use CAST, eg CAST(DATEDIFF(DAY, MAX(p.end_Time), MIN(p.start_Time)) AS CHAR(8)) should do the trick (or you can simply do CAST(MAX(p.end_Time) - MIN (p.start_Time) AS CHAR(8)) as timestamps are subtractable.
I just noticed though that the option you specify in your CONVERT will convert to hh:mi:ss. There is no such option in Firebird. If you need to convert specifically to hh:mi:ss, you might want to look at UDF libraries like FreeAdhocUDF (specifically F_SECONDS2PERIOD) or rFunc UDF. If all else fails, you could write your own UDF.
there is no CONVERT on firebird.
Use CAST:
select cast(MAX(p.end_Time) as varchar(50)) and so on...
On Oracle (Oracle 8i, Oracle 9i, Oracle 10g, Oracle 11g), you can use to_char function:
select to_char( value, [ format_mask ], [ nls_language ] ) ....
Example:
SELECT TO_CHAR ( SYSDATE - TO_DATE ( '18-10-2012', 'dd-mm-yyyy' ), '9,999.99' ) as duration FROM DUAL;
Here is the link for Oracle/PLSQL: To_Char Function
I have a column in a table where timestamps have been stored in VARCHAR format, but I need to compare these against a column of DATETIME values from another table to find time intervals, so I want to either cast or convert the VARCHAR timestamps to DATETIME. However, both casting and converting are giving me problems.
The format of the VARCHAR timestamp looks like this: "29/07/2012 01:53:36 +12".
Using the query:
SELECT CAST(event_timestamp AS datetime) FROM the_table
produces ERROR: date/time field value out of range: "29/07/2012 01:53:36 +12".
Using the query:
SELECT CONVERT(datetime, event_timestamp, 131) from the_table;
produces
ERROR: syntax error at or near ","
LINE 1: select CONVERT(datetime, event_timestamp, 131) from the_tab...
^ (note: this is pointing at the first comma).
The error with CONVERT actually happens even if you use a generic function such as getdate() for the data source. This db uses ANSI SQL-92 (or so I'm told). Could anyone please help me out with this?
This seems really painful, but the following should work:
select dateadd(hh, cast(right(tv, 3) as int),
CONVERT(datetime, left(tv, 10), 103)+CONVERT(datetime, substring(tv, 12, 8), 108)
)
from (select '29/07/2012 01:53:36 +12' as tv) t
I've never added datetime's before, but this just worked on SQL Server 2008.
Why can't SQL Server just support a flexible notation built around yyyy, mm, mmm, dd and so on?
The actual database is Aster Data, which is based on Postgres (as are most recent database engines). In this database, you would use to_timestamp(). See the documentation here http://www.postgresql.org/docs/8.2/static/functions-formatting.html. The call would be something like:
to_timestamp(val, 'MM/DD/YYYY HH:MI:SS tz') -- not sure if this gets the +12
There are no ANSI functions for date conversion, so each database does its own. Even string functions vary among databases (substr? substring? charindex? instr? location?), so there is no ANSI way to do this.
You are using the wrong syntax, try:
CONVERT(varchar(X), datetimeValue, 131)
Where X is the total number of characters desired.
You will then be able to search for a match with datetimeValue and event_timestamp, assuming each value share the same structure. This will allow you to match string against string.
If I'm not mistaken the standard (ANSI SQL) CAST operator always expect time/date/timstamp literals in ISO format ('YYYY-MM-DD')
But according to the manual for Teradata V12 (can't test it), the format of the CAST operator is
CAST(character_expression AS TIMESTAMP timestamp_data_attribute)
with date_data_attribute being a character value plus an optional FORMAT specifier.
So in your case this would probably be:
cast(event_timestamp AS TIMESTAMP FORMAT 'MM/DD/YYYY HH:MI:SS Z');
I'm not entirely sure about the format definition though. You'll probably need to adjust that
Btw: CONVERT isn't a standard SQL function. It's SQL Server specific.
Suppose I want a particular format of date of birth, like (mm-dd-yyyy).
How can I do that in SQL?
Although some people have listed the proper syntax for this in multiple RDBMSs (you really need to indicate which one you're using), I'd like to point out that formatting your data is typically something that should be done by the front end of your application. That's not to say that there's never a reason to do formatting in SQL, but usually it's best to just pass it as a date/time to the front end and let the front end handle how it will represent it to the user. Hopefully, you understand the difference between a date/time and a string.
Use CONVERT function if it is SQL SERVER
Select CONVERT(VARCHAR(10),Birthdate,110) from YourTable
Assuming your RDBMS is SQL Server, you can do the following:
SELECT CONVERT(VARCHAR(10), [DateOfBirth], 110)
There's more information about date formatting here.
SQL Server
SELECT convert(varchar, getdate(), 110)
SELECT convert(varchar, DateOfBirth, 110) FROM YourTable
How to format datetime & date in Sql Server 2005
If you're using Oracle:
select to_char(your_date_column,'MM-DD-YYYY') from your_date_table;