Datetime field using military time - need time only in standard time - sql-server

I have a datetime field in SQL Server 2008 which stores the date and time in military format (or international format if you prefer)
examples:
2011-02-15 10:00:00.000
2011-02-15 15:30:00.000
2011-02-15 17:30:00.000
I need to retrieve the time only portion of this in standard U.S. time format.
So
2011-02-15 10:00:00.000 needs to become 10:00 AM
2011-02-15 15:30:00.000 needs to become 3:30 PM
2011-02-15 17:30:00.000 needs to become 5:30 PM
I am looking for the best way to accomplish this in T-SQL please.

One way is:
Convert to varchar, which will give you:
Select CONVERT(varchar, #dt, 100) -- where #dt is your date time
Feb 15 2011 3:30PM
Subsequently, to remove the date, and get 7 chars from the right, which is the most, and TRIM is added just for extra safety, but probably isn't needed.
Select LTRIM(RIGHT(CONVERT(varchar, #dt, 100),7))
Will give you 3:30PM
Side Note: Denali makes this easier, no more magic numbers

As you requested this in T-SQL, you might want to look at the CAST and CONVERT syntax which specifically lists various date and time formats.
For example:
select convert(varchar, getdate(), 100)
Would give you:
Feb 3 2012 3:26PM

DateTime is stored in an internal representation - there is no format associated. When you need to display the DateTime, specify the formatting you want for converting the DateTime into a string.
It is much better to let the application decide on formatting than formatting in SQL.
See standard and custom Date and Time Format Strings for more information.

Related

I want to convert DATEADD("d",- (DAY(GETDATE())) + 1,GETDATE()) to YYYY-MM-DD format

The expresssion
DATEADD("d",- (DAY(GETDATE())) + 1,GETDATE())
is correct and its giving me
2/1/2019 3:45:02 PM
But i want my answer to be as 2019-02-01 in SSIS
PLease help me
For some reason, SSIS is a bit of a pain when it comes to handling dates. Basically, you have to double cast it...
(DT_DATE)(DT_DBDATE)DATEADD("d",- (DAY(GETDATE())) + 1,GETDATE())
Note: The evaluated value is 2/1/2019 12:00:00 AM. But, when you click OK on the Expression Builder, you will see the value displayed as 2/1/2019.
Edit To my knowledge, it is not possible to change the date format, even with converting it to string and then back to date. I wouldn't consider this a problem - leave the internal date format alone so that the back-end can handle it. The date format should only really be of any concern for display purposes, which can be manipulated during the SQL SELECT phase.
Is there a reason that you require the date format to be changed for back-end processing purposes?
If you are looking to get the value 2019-02-01 (format yyyy-MM-dd) then you should convert the value to a string and use the following expression:
LEFT((DT_WSTR,50)DATEADD("d",- (DAY(GETDATE())) + 1,GETDATE()) ,10)
Output
2019-02-01
Based on the Cast (SSIS Expression) official documentation:
When a string is cast to a DT_DATE, or vice versa, the locale of the transformation is used. However, the date is in the ISO format of YYYY-MM-DD, regardless of whether the locale preference uses the ISO format.
If you need the returned value of type DT_Date then just add a CAST operation:
(DT_DATE)LEFT((DT_WSTR,50)DATEADD("d",- (DAY(GETDATE())) + 1,GETDATE()) ,10)
Note that in Date data type there is no formatting, values are not stored in the same way they are visualized.
Output
2/1/2019 12:00:00 AM
Which is equivalent to
2/1/2019 00:00:00

How do I get the name of the month (char) and the Year (date) to appear in my data item and column in Cognos

My Accounting Period Date is using a Numeric data type
1990-01-01
2008-08-01
2008-07-01
So I applied CAST function in the data item.
And the results appeared like this.
Jan 1, 1990
Aug 1, 2008
Jul 1, 2008
Is there a way to make it appear like this?
Jan 1990
Aug 2008
Jul 2008
I tried using a new data item using Replace Function like this but it does not work.
This is the error I get.
Could someone help me please.
Part of the problem you will run into is Cognos's auto formatting of date types. That's what you are seeing with the Jan 1, 1990 format. The data still exists as a date but Cognos chooses to reformat it when running. Thus, you cannot use a substring function to extract out the components you want as it doesn't exist as a string. Straight casting to a string reverts it to the 'YYYY-MM-DD' format in string form, obviously not what you want.
There are three options.
If you just want the date to display in the desired format on the report, then the simplest solution is to use the 'Data Format' property on the column itself in the report definition (list, crosstab, singleton etc). Within the Data Format property sheet there is a property called 'Pattern'. You can use this property to define how you want the column displayed. Enter 'MMM YYYY' in this property to create the output you desire.
If you need the actual data item to change to match the format you require then you will have to build a string:
CASE extract(month,[Date])
WHEN 1 THEN 'Jan'
WHEN 2 THEN 'Feb'
WHEN 3 THEN 'Mar'
WHEN 4 THEN 'Apr'
WHEN 5 THEN 'May'
WHEN 6 THEN 'Jun'
WHEN 7 THEN 'Jul'
WHEN 8 THEN 'Aug'
WHEN 9 THEN 'Sep'
WHEN 10 THEN 'Oct'
WHEN 11 THEN 'Nov'
WHEN 12 THEN 'Dec'
END || ' ' || cast(extract(year,[Date]),char(4))
Your last option is to use a data source vendor-specific cast function. For example, DB2's to_Char() function allows you to precisely define how the date is converted to a char. Other database servers have their own functions that do similar things. You'll have to figure out if there is an equivalent in your data source vendor's solution.
The down side to this approach is that if you change data sources to another vendor you will have to adjust the report as well or it will likely throw an error. When you use Cognos's own functions, Cognos automatically converts the function to the equivalent vendor-specific function for you. You gain portability and maintenance at the possible expense of flexibility and power.
Maybe something like this?
SELECT CAST(DATENAME(MONTH,GETDATE()) AS VARCHAR(3)) + ' ' + CAST(DATEPART(YEAR,GETDATE()) AS VARCHAR(4)) AS mystring

SQL Server Convert datetime to mm/dd/yyyy format but still be a datetime datatype

I would like to keep my dates as datetime datatype by also be in MM/DD/YYYY format. I know how to do this by converting them to a varchar, but want to keep the datetime format. Can anyone help with this?
Currently I have tried
SELECT CONVERT(datetime, GETDATE(), 101)
which is not working...
There is a basic misunderstanding in your question. Repeat after me: Datetimes don't have a format.
It helps if you think of them as just an array of seven integers (year, month, day, hours, minutes, seconds, milliseconds) with certain constraints. That's not in any way accurate, but it helps to get the notion out of your head that something akin to 12/31/2015 is stored in your database.
Datetimes only get a format when (implicitly or explicitly) being converted to strings. You already know how to set the format when explicitly converting to string, now all that is left to do is to find the implicit conversion that is obviously bothering you and replace it with an explicit one.
Date and datetime Values stored in the database are NOT in any recognizable format. They are stored in binary (1s and 0s) in a proprietary format where one part represents the number of days since a defined reference date (1 jan 1900) in SQL server). and the other part represents the time portion of the value. (in sql server, its the number of 1/300ths of a second since midnight.)
ALL formatting of dates and date times, no matter what format you wish for, is done only after the values have been extracted from the database, before you see them on screen, in whatever application you are using.
You can find all the formats that the SQL Server convert function can use on this MSDN Convert Link

Convert String to datetime in SQL Server 2008

How will I convert date in the format Sat Mar 29 00:00:00 EST 1975 to datetime in TSQL?
I get the date in this format from an old table which defined the date of birth column as NVARCHAR and stores the data as Mon Jun dd hh:mm:ss GMT yyyy format. I need to read another table which has the dob in datetime using this value.
So basically I want to convert, say Sat Mar 29 00:00:00 EST 1975 to 1975-03-29 00:00:00.000
Is there a way in T-SQL to do this conversion? I tried the CONVERT function, but I am unable to locate the correct 'style' to use.
Examining the data format, it appears to be a fixed length string.
The first portion is the day of week, which can be discarded as it isn't needed for parsing. Next you have the month and day information, which we need. After that is the time, which can be retained or discarded depending on whether you want a date or datetime as output.
Since you are looking for a date of birth, the time zone information can most likely be safely discarded.
Finally, there is the year.
If we eliminate the day of week and the time zone, sql server will parse the rest of the string with no problem.
I recommend cast(substring(#difficultTime,5,7) + substring(#difficultTime,25,4) as date), where #difficulteTime is the column name you are converting.
If you wanted to retain the time information, the following format will work cast(substring(#difficultTime,5,16) + substring(#difficultTime,25,4) as datetime)
This assumes that your strings will be of a fixed length. The first conversion shown eliminates the day of week, the time, and the time zone from the string, leaving a parseable date.
The second conversion eliminates the day of week and the time zone, leaving a parseable datetime.

Converting number to date in oracle

I have a numeric field in my Oracle database that represents the number of days since Dec 28, 1800. However I am trying to select it (for another application) as the current date it represents. I'm not too familiar with Oracle commands (I'm used to SQL), so I was wondering if anyone could provide some assistance. Thanks.
ex: 77650 = Saturday, August 3, 2013
Firstly, get this out of the way, your life would be easier if you stored dates in a date data-type.
However, to answer your question to add days to a date in Oracle you can use the + operator.
Firstly though you have to have a date so I'll convert the 28th December 1800 into a date using to inbuilt to_date function then add the number. In your case you would want:
select to_date('1800/12/28','yyyy/mm/dd') + 77650 from dual
I've set up a little SQL Fiddle to demonstrate for you.

Resources