I could use some help deriving a year quarter from a text string that represents a date. I have a string, say '20121230', to represent 12/30/2012. Somehow, someway, I need to convert this value into '4Q12.' I get stuck after converting the 20121230 into a date:
CONVERT(date,datestringfield,111)
I need help deriving the quarter and year from this date and then converting the quarter and year to a string format 4Q12. Any help would be greatly appreciated
If you are using SQL Server, then the particular format of datestringfield can be used as a date unambiguosly. So, you can do:
SELECT DATENAME(QUARTER,datestringfield) + 'Q' +
RIGHT('00'+DATENAME(YEAR,datestringfield),2)
Related
My date is stored in a type date column in dd/mm/yyyy format. I want to print the date in yyyymmdd format.
When i used the following formula
tonumber(totext(db.colname,'YYYYMMDD'))
It gave me a "the string is non numeric" error when previewing the report.
Secondly,
My time is stored in a string column in 12 hour format. I want to display it as hh24miss format.
How do i do that ?
First of all, you should NOT store dates/times as text in your database.
Use the apropriate datatype of your DBMS.
Otherwise you will very likely have further problems because of this.
When you've changed the datatype you could just drag&drop the database-field inside your report and use the formatting options of Crystal Reports to get the desired format.
If for any reason (I doubt there's a good one) you can't change the datatype, use the following formula.
ToText(Date({db.colname}), "yyyyMMdd")
This formula converts the string to a date and then formats the date with yyyyMMdd format.
Notice the uppercase M which is used for the month. Lowercase m is used for minutes.
I am loading a flat file to a database table, and need to change the format of the date from YYYY-MM-DD in the flat file, to MM/DD/YYYY in the database table. I tried using the following statement in Derived Columns as shown below, but not sure how to configure the statement, so I got an error message stating that SSIS could not parse the expression.
Derived Column Name: EFF_DATE
Derived Column: Replace EFF_DATE
Expression: TOKEN( MONTH([EFF_DATE]),"//|",DAY([EFF_DATE]),"//|",YEAR([Copy of EFF_DATE]) )
DATA TYPE: databasetimestamp[DT_DBTIMESTAMP]
Can anyone help me determine how to change the format of the column in Derived Column? Otherwise, please let me know if there is another way to do it. Thank you.
This question was different from the last one. In the last question, the date column was data type DateTime. But in this question, the date is a string, and when I used the Derived Column to change the date from YYYY-MM-DD to MM/DD/YYYY, it kept the leading zeroes in MM and DD. The issue then became, not just changing the date format, but also removing the leading zeroes from the Month and Day.
However, I researched and came up with a better solution in SSIS for changing the date value with data type string, as the database I am working with stores the date in that format.
I removed the Derived Column from my Data Source Task, and added an Execute SQL Task in the Control Flow, then added the following Update statement which not only changes the format from YYYY-MM-DD to MM/DD/YYYY, but also removes the leading zeroes from Month and Day. The CONCAT function I used the sample SQL below changes the format from YYYY-MM-DD to MM/DD/YYYY, while the Convert function changes the MM and DD values to data type INT which removed any leading Zeros. This solution allowed the date to remain a string, as that was the table format I had to work with.
UPDATE [StagingTable]
SET START_DATE =
CONCAT( CONVERT(INT, SUBSTRING(START_DATE, 6,2)), '/', CONVERT(INT, RIGHT(START_DATE, 2)),'/', LEFT(START_DATE,4) )]
Thanks to everyone for their comments, as it helped me to think outside the box and determine this solution.
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
I'm a bit rusty and seem to have completely forgot how to do this. I am receiving data and the datatime column is in string format char(12).
Example : 201411061900
How would I write the query (view) to convert that to DateTime?
I've used CONVERT (datetime, dbo.KWH.mr_dtm,120), but I get the error Conversion failed when converting date and/or time from character string.
I haven't had much luck. Thank you very much in advance.
If you want to suggest -5 time zone also, I wouldn't mind. :)
Magnus answered my question. I wanted to post it here incase it helps anyone in the future.
Here is the link to the answer.
There is no output format identifier (like for example 120) that lets you convert a char(12) into a datetime in SQL Server. But you could use the Substring function:
Select Cast(Substring(dbo.KWH.mr_dtm,1,8) + ' ' + Substring(dbo.KWH.mr_dtm,9,2)+':'+ Substring(dbo.KWH.mr_dtm,11,2) as DateTime)
Thank you to everyone! It is very much appreciated.
Oh, and this post help me solve my timezone setup.
Convert Datetime column from UTC to local time in select statement
Many thanks to Michael Goldshteyn for the timezone convert.
I have a datetime field .for e.g. 2013-08-22 12:00:00 AM. I want to concatenate the year and month and i want the output as 201308.
When i try year (datetime_field)+month(datetime_field) what i get get is 2013+08=2021 .. i.e it adds instead of conactenating. Can someone pl tell how to get the output as 201308?
something more like
str(year(datetime_field)) + str(month(datetime_field))
should give you what you want.
the str function converts your numeric data to a string, setting up the + to be a string-concatenation operation instead of an arithmetic-addition operation.