Date conversion - sql-server

I have a table with a datetime column I want to retrieve the date in hh:mm:ss where hours is 12hour.
I have tried the following.
convert(varchar(10), t2.CSM_START_TIME,108),
convert(varchar(2),datepart(hour,t2.CSM_START_TIME))
+':'+convert(varchar(2),datepart(mm,t2.CSM_START_TIME))
+':'+convert(varchar(2), datepart(SECOND,t2.CSM_START_TIME))
as START_TIME

SELECT LTRIM(RIGHT(CONVERT(CHAR(20),GETDATE(),22),11));
Result:
11:40:15 PM
Or if you really don't want the AM/PM (which I don't understand):
SELECT LTRIM(LEFT(RIGHT(CONVERT(CHAR(20), GETDATE(),22),11),8));
Result:
11:40:15
Much, much, much better to format this at the client. For example, if you are using C#, look at .Format() and .ToString(). Don't make SQL Server do your dirty presentation work, especially when you have much more flexible features in a more powerful language.

This got me what I wanted.
,substring(convert(varchar(40), t2.CSM_START_TIME,109),12,9)+' '+substring(convert(varchar(40), t2.CSM_START_TIME,109),25,2)

you should try this
DATE_FORMAT(NOW(),'%h:%i %p')
or you will find more here http://www.w3schools.com/sql/func_date_format.asp.

Related

Dynamic dates in SSRS

I need to make a report of all patients who had an appointment last week. This report will be added to another excel with some lookups and then put into Power BI because we don't have way of connecting our sql server.
I'm trying to reduce the amount of manual work I have to do by instead of using parameters with dates, adding a dynamic date.
I have tried using TODAY, CURRENT_DATE and they all come back with an error.
I just need it to give me data for 7 days prior to the current date
Any help would be greatly appreciated.
This is what the first part looks like:
SELECT
PM.vwApptDetail.Patient_Last_Name
,PM.vwApptDetail.Patient_First_Name
,PM.vwApptDetail.Patient_DOB
,PM.vwApptDetail.Appointment_DateTime
,PM.vwApptDetail.Appt_Type_Desc
,PM.vwApptDetail.Resource_Desc
,PM.vwApptDetail.Status
FROM
PM.vwApptDetail
WHERE
PM.vwApptDetail.Appointment_DateTime >
I ended up using:
WHERE Appointment_DateTime BETWEEN GETDATE() AND DATEADD(DAY, -7, GETDATE())
and it seems to have worked.

change operator output type and value in hsqldb

I have hsqldb source with me i want to modify date operations in source code, in such way date1 - date2 operation should give output type as "INTEGER" instead of "INTERVAL DAY TO SECOND" type and also it should give date difference.
Please let me know how and where i need to modify in hsqldb source.
Note: I do not want to use datediff method.
It sounds like you are only interested in the number of days between the two dates.
extract( day from( date1 - date2 ));
I have hsqldb installed but it's not running at the moment so I can't test this but it should be really close to what you want.

Display date in MM-dd-yyyy format SSRS report

I have a SQL query: select ModifiedDate from Person.Person and this returns the date as 2/24/1998 12:00:00 AM
I'm trying to display this in MM/dd/yyyy format in SSRS report. I have used the expression =Format(Fields!ModifiedDate.Value,"MM/dd/yyyy")
But, still it's coming as 2/24/1998 12:00:00 AM
I want to display this as 2/24/1998.
How can I do this?
I would recommend using the format codes:
Right click - properties on the cell, select format, click the ellipsis "...", and you can see the date formats from there. This will be converted into a date code when you OK the dialog. This is useful as it sets the date in the fomat the user wants to see it in.
To convert the data within SSRS and not the data source you could try using something like:
=Format(Cdate(Fields!ModifiedDate.Value),"dd/MM/yyyy")
Another sample for you without Cdate:
=Format(Fields!ModifiedDate.Value,"dd/MM/yyyy")
There is a special function for formatting Dates in SSRS:
=FormatDateTime(Fields!ModifiedDate.Value, DateFormat.ShortDate)
It will return your date the way that you want it.
As per your question,
To convert the date in SSRS you should try like this,
=FORMAT(Fields!ModifiedDate.Value,"MM-dd-yyyy")
Output would be,
12-06-2010 -- 12(Month)-06(Date)-2010(Year)
Set the language of the entire Report to the language of the country the report is going to be run in.
For example for US it's en_US and for Australia it's en_AU.
Simple !
If you want to get date in format of 'MM/DD/YYYY' use the following query andand you have to convert in varchar datatype.
select CONVERT(varchar(20),GETDATE(),101)
I had this problem and i solve that by this code :
FormatDateTime('MM/dd/yyyy',ADOqueryname.fieldbyname('ModifiedDate').AsDateTime);
I did two things to make it work:
1st, In Data set property Query - select CONVERT(varchar(20),ModifiedDate,101) from Person.Person
2nd, In expression - =Format(Fields!ModifiedDate.Value,"dd/MM/yyyy")
This worked for me.Thanks for the hints.
You can use:
- FormatDateTime(Fields!ModifiedDate.Value, 2)
or:
- FormatDateTime(Fields!ModifiedDate.Value, DateFormat.ShortDate)
You will get the same result.

With Hibernate/JPA query inHQL/JQL only how do I set where clause to last x days?

I am looking for the HQL equivalent of converting x amounts days from current timestamp to a queriable value.
So like this sudo-HQL :
from Newspaper as newspaper
where newspaper.published < current_timestamp - days(:daysparam)
And then daysparam is injected as query parameter. And published is date field.
Is this in anyway doable in HQL only, without writing your own hibernate dialect or using criteria in actual code? It seems such as standard feature to not be supported by plain HQL seems strange.
I am using Spring batch's HibernatePagingItemReader which is xml only, so I wanted to avoid the yakshaving of extending that class or creating my own custom dialect etc.
Similar question seems to only suggest calendar critera or new dialect:
Performing Date/Time Math In HQL?
How to perform date operations in hibernate HQL
Something doesn't look quite right: you said that published is a date field (in the database, I suppose). Then, you are using current_timestamp minus an integer value to compare with the date field. As result, you are not getting the timestamp for the date in the parameter, you are just getting current_timestamp - 2, which I don't believe represents "two days ago" ;-) If you have used current_date, I guess it might work.
from Newspaper as newspaper where newspaper.published < current_date - :daysparam
But still, I'd prefer to leave this calculation to the Java side, so that the query would be:
from Newspaper as newspaper where newspaper.published < :start_date
This won't work only if you are not using UTC in your servers (which you should).

How do you convert the number you get from datepart to the name of the day?

Is there a quick one-liner to call datepart in Sql Server and get back the name of the day instead of just the number?
select datepart(dw, getdate());
This will return 1-7, with Sunday being 1. I would like 'Sunday' instead of 1.
select datename(weekday, getdate());
It actually took me more searching than I thought it would to find this answer. It's funny how you can use a technology for ages and never know about simple functions like this.
select datename(dw, getdate())
I'm not sure how localization would work with this function. Getting the name client-side is probably the answer, but it would be nice to do it on the database. Would Sql Server use the collation setting to determine the output for this?
This is not possible without using the result to select the day yourself. For one thing the textual representation of the day is locale-dependent. For another the returned value depends upon the 'datefirst' setting.
If you want a localizable solution, just join the result against a table with the names and numbers.

Resources