SQL Server time datatype with format - sql-server-2008-r2

Can I pass format for the time or date datatype in SQL Server 2008 R2?
Example: a column with time format like hh:mm ONLY
I searched and found that I can pass fractional second scale with like below but that is will get met a time format with seconds hh:mm:ss and I only want hh:mm
Note
I do not want to avoid this case in the select statement it wont help me in when using the column in Crystal Reports and I am not able to format it in Crystal Reports there is no date-time tab in format object option
_HOUR time(0);

I do not want to avoid this case in the select statement ...
I am not able to format it in crystal report
But you do need a select statement to produce a report, so one way is to use a "style number" (8) with the convert() function - but you only want the leading 5 characters, so use char(5) for the result. i.e.
select convert(char(5), [datetime_or_time_column] ,8) as "hh:mm"
from thetables
You do not indicate at all how you gather the data for the report, but if using a temp table or stored procedure (or even a view) you can use the convert syntax above when producing the result. Note is is possible to use T-SQL's format() like so format(getdate(),'HH:mm') but this is usually slower than using convert().
If you have permission to add calculated columns to your table AND this wanted hh:mm data is deterministic, you could also use the convert syntax shown above for a calculated column.

Related

Custom date format iseries access odbc for SSRS

How to resolve my issue below?
1. I am pulling data from AS/400 DB2 using (iseries access odbc driver) to SSRS.
2. I want to format the column stores in integer to date format.
Sample = 20180612 below is the SQL Query.
Using below query,
SELECT CHAR(DATE(SUBSTR(DIGITS(20180612),1,4)||'-'||
SUBSTR(DIGITS(20180612),5,2)||'-'||
SUBSTR(DIGITS(20180612),7,2)),ISO) AS RESULTSDATE
Output = 2018-06-02
Question: How can I produce a below custom date format like d/m/yyyy
I have used, ISO, USA, LOCAL, JIS, EUR but no yield.
Example: 2/6/2018
If you want to do it in SQL Server, the closest you can CONVERT it to is the USA 110 (mm-dd-yyyy) in SQL Server.
If the your dataset has a field with the 2018-06-02, you can use the CDATE function is SSRS to convert it to a date and then format the text box the way you want with the FORMAT property or the FORMAT function.
Format Function:
=FORMAT(CDATE("2018-06-02"), "M/d/yyyy")
Format Property of Text Box:
/* this is as close as I can come to the desired results there is a leading zero */
select
varchar_format(
timestamp_format(char(20180612), 'YYYYMMDD')
, 'DD/MM/YYYY') as resultdate
from my8digitdatetable
/* for export IRL send out a date */
select
cast(
timestamp_format(char(20180612), 'YYYYMMDD')
as date) as resultdate
from my8digitdatetable
I got it already. I just format it to the date without formatting.
SELECT DATE(SUBSTR(DIGITS(MIN(DTAHRS.HREMCP.EMREDT)),1,4)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HREMCP.EMREDT)),5,2)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HREMCP.EMREDT)),7,2)) AS RESULTSDATE
After that, I put this as sub-query so that I can pass this to the parameter.
Below is my whole query, then at SSRS I can use date/time parameter control to compare with the results.
SELECT x.RESULTSDATE
FROM ( SELECT DATE(SUBSTR(DIGITS(MIN(DTAHRS.HRLVTP.LTLDTE)),1,4)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HRLVTP.LTLDTE)),5,2)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HRLVTP.LTLDTE)),7,2)) AS RESULTSDATE
) as x
WHERE x.RESULTSDATE >= ? AND x.RESULTSDATE <= ?
I hope it will help you too.

Data stored as DD/MM/YYYY in table, but querying in DD/MM/YYYY doesn't work for all dates MS Access 2016

There are probably many questions that are asking about date formats, but I haven't found anything like this.
I have a table, called t_birthday. t_birthday has a field called "DayOfMonth" which currently stores the data in a dd/mm/yyyy format. Lets say the record I have has the Date of 01/12/2016 (Dec 1, 2016).
Now, if I create a query using the "Query Design" option in the Create tab, I select my table t_birthday. For the field option, I select DayOfMonth. In the criteria option, I put =#01/12/2016#. When I click Run, it queries the database and returns the record with that date successfully.
However.. If I check the SQL generated from this Query Design, it is this:
SELECT t_birthday.DayOfMonth
FROM t_birthday
WHERE (((t_birthday.DayOfMonth)=#12/1/2016#));
If I try copy and pasting the DayOfMonth value from the table into that query, it wouldn't work. Notice how the format in the query is mm/dd/yyyy, but in my table it's still dd/mm/yyyy. I never touched any of the date formatting options in my table, or even on my computer. When I actually create this record using a form, I have a date picker which is in the form of dd/mm/yyyy as well.
Questions:
In the query design, when I specify criteria in dd/mm/yyyy, why does it generate sql in the form of mm/dd/yyyy?
I can only query dates using dd/mm/yyyy format if the day number (1-31) is 13 or above, OR if the month value and the day value are the same (October 17, Jan 1, March 3, November 11, December 12, etc). mm/dd/yyyy still works for those dates previously mentioned. I can't query dates like November 7th, Feb 3rd, August 4th, etc using dd/mm/yyyy though. How do I get around this problem? I store the dates, and I use the values directly from the table as conditionals in my queries. I shouldn't have to alter my date value in order to use them.
Why can I write an SQL statement for dates with the day number above 13 in dd/mm/yyyy format or mm/dd/yyyy format? E.g., the WHERE clause can look like: WHERE DayOfMonth=#13/06/2018 or WHERE DayOfMonth=#06/13/2018 and it still returns the same record? Why does access not enforce a specific format?
EDIT:
Currently I run my query in VBA and return it into a recordset using the following:
Dim bdayRecords As RecordSet
Dim sql As String
sql = "SELECT t_birthday.DayOfMonth"
sql = sql & " FROM t_birthday"
sql = sql & " WHERE (((t_birthday.DayOfMonth)=#" & rs("DayOfMonth") & "#));"
bdayRecords = CurrentDb.OpenRecordset(sql)
Where rs in the where clause was a previous recordset with a date value stored in "DayOfMonth". The rs recordset retrieved the date value from a different table in the exact same way bdayRecords was populated.
bdayRecords won't find the records with the date values matching the criteria explained before.
Use a properly formatted string expression for the date value retrieved:
sql = sql & " WHERE t_birthday.DayOfMonth = #" & Format(rs("DayOfMonth").Value, "yyyy\/mm\/dd") & "#;"
The ISO sequence yyyy-mm-dd works everywhere, so make it a habit to use that.
SQL always uses mm/dd/yyyy. That's not dependent on how you format it.
You never actually store a date in a certain format. You display a date in a certain format. All dates in Access are stored as a double-precision floating number containing the number of days elapsed since 30-12-1899, with fractions as time. How dates are formatted has no influence whatsoever on your SQL statement
Always use either mm/dd/yyyy or yyyy-mm-dd in your SQL. VBA only takes mm/dd/yyyy.
However, Access is opportunistic when working with clearly invalid dates, such as 13/1/2018. Because no 13th month exists, it parses it as the 13th of january, even though it's not a valid date.
If you're using values from other queries, there shouldn't be any problems, since the values never get cast back and forth to strings. You only get in trouble when casting a date to a string and then back to a date, which is not something you should do in queries, ever.
To avoid casting back and forth between strings, you can either refactor your code to a single query instead of retrieving a value from a recordset and inserting that value in a string SQL statement, or use parameters, which allows you to use the date value directly in an SQL statement.
For explanations why these design choices are made, ask Microsoft, they wrote the program. This is just how it works.

How to cast date column in SQL Server 2008 R2 to have drill down effect in the Excel format

I'm very new on this blog and probably I am not expressing my self correctly, but what I want is: I have a stored procedure where I am grabbing data and inside there I have a date column. I am using that stored procedure to create a report using SSRS and when I am exporting the report to Excel format and applying filter on date column I'd like to have a drill down effect on filter which shows first year and when I toggle down it should show me the month and then the days.
So how should I convert the date column that I can have a drill down effect?
CONVERT(VARCHAR(11), ORIGINAL_ORDER_DATE.THE_DATE, 103) AS ORIGINAL_ORDER_DATE
This is how i'm casting my date column inside the stored procedure.
This is how my date column look in Excel
This is how I'd like to have it

SAS proc sql - Convert ddmmmyyyy to week-yr and month-yr

I have imported some data into SAS from some Excel spreadsheets sent to me. When I view the output from the imported table, the date appears as "01APR2014" and maintains chronological order. When I view the column properties the type is "Date" and the length is 8. Both the format and informat are DATE9.
I need to be able to convert this date to week-year and month-year, but no matter what I try I always get Jan, 1960.
Using proc sql, I used the below to get the week-year,
"(put(datepart(a.fnlz_date),weeku3.))|| "-" ||(put(datepart(a.fnlz_date),year.)) as FNLZD_WK_YR,"
but all I got was "W00-1960". I've used the formula above successfully many times before with SAS datetime values.
For month-yr, using proc sql, I tried
"datepart(a.fnlz_date) as DT_FNLZD format=monyy.,"
but the only value returned is "JAN60".
I also tried using SUBSTR, but got an error saying it requires a character argument, so SAS must see it as a number at least.
My question; does anyone know a way to get the week-yr and/or month-yr from this format? If so, how? I'm not opposed to using a data step, but I haven't been able to get that to work either.
Thanks in advance for any help or insight provided.
datepart converts datetimes to dates. Not helpful here.
If you're just displaying this, then you have a few options, particularly for month. You can just change the format of the variable (This changes what's displayed, but not the underlying value; consider this a value label).
When you use this like this (again, it looks like you got most of the way there):
proc sql;
select datevar format=monyy5. from table;
quit;
Just don't include that datepart function call as that's not appropriate unless you have a datetime. (Date=# of days since 1/1/1960, Datetime = # of seconds since 1/1/1960:00:00:00).
That will display it with MONYY5. format, which would be MAY10 for May, 2010. You have some other format options, see the documentation on formats by category for more details.
I can't think of a Week format that matches what you want (there are week formats, like WEEKW., as you clearly found, but I don't know that they do exactly what you want. So, if you want to build one yourself, you can either build a custom picture format, or you can make a string.
Building a custom picture format isn't too hard; see the documentation on Picture formats or google SAS Date Picture Format.
proc format;
picture weekyear (default=8)
low-high = 'W%0U-%Y' (datatype=date) ;
quit;
Now you can use that as a normal format.
To get at the week/etc. to build values, you can also use functions week(), month(), etc., if that's easier.
Since the data was already in a date format, I only needed to drop the DATEPART function that only works with datetime values. So, for month-yr,
"a.fnlz_date as fnlz_mnth format=monyy.,"
gives me the results I'm looking for.
Cheers!

date conversion in SQL parameter field in Crystal reports

I have a Crystal report (version XI r3) that uses a SQL command object to retrieve its data. In the command object I have a parameter for a date. My database uses "date" fields stored as numeric values in YYYYMMDD format, so I've specified the parameter as numeric and added a prompt to say "enter date in YYYYMMDD format".
My users don't much care for that; they want to be able to use the date-picker and/or to be able to enter the date in MM/DD/YYYY format.
My investigations so far have led me to believe that if I convert the parameter to a true date datatype, I won't be able to make use of it in the SQL command object because I can't convert it from a date to a number in the SQL statement, so I'd have to do my date-range control in the Crystal Select Wizard rather than in my SQL statement, which could slow my report down by an order of magnitude or two (since I'm hitting a table that is indexed by this date field, and that has a lot of records per day).
Am I wrong? Is there a way to let a user enter a date in MM/DD/YYYY format and still be able to use it as a numeric YYYYMMDD parameter in my SQL command object?
I'm afraid you would have to modify the original Command's to replace the numeric parameter with a date parameter, and do the conversion from date to number within the Command itself.
So, within the Command:
WHERE MyDate = {?MyNumberParam)
would become:
WHERE MyDate = (YEAR({?MyDateParam})*10000) + (MONTH({?MyDateParam})*100) + DAY({?MyDateParam})
The last part will convert 20th April 2012 to (2012*10000 + 4*100 + 20) = 20120420, which I believe is what you'd want.