Select time from datetime in SQL Compact - sql

Much like this question: "How to get Time from DateTime format in SQL?", I am trying to select only the time from a datetime, but unlike the above question, I would like to know how to do it in SQL Server Compact. Does anyone know how to do this?

SELECT DATEPART(hour, OrderDate), DATEPART(minute, OrderDate) FROM MyOrders
Ref. http://msdn.microsoft.com//library/ms173998%28v=sql.90%29.aspx

Solved it. Luigi's answer is not actually the correct one but I have upvoted it, as it helped me find the answer.
To get only the time from a datetime in SQL Server Compact, the proper query is:
select ltrim(str(DATEPART(hour, columnName))) + ':' + ltrim(str(DATEPART(minute, columnName))) + ':' + ltrim(str(DATEPART(second, columnName))) from table

Related

How to Convert String to Date in SQL Server?

I have date column in my table as string type example Feb-18. How do I change it to date type as 01-02-2018?
Use convert() with add one day :
select convert(date, '01-' + 'Feb-18')
SQL Server is pretty good about figuring out dates. But you need a day, so:
select convert(date, '01-' + datecol)
Note: You should be very careful about storing dates as strings. I would recommend that you test the conversion to be sure it works for all values:
select datecol
from t
where try_convert(date, '01-' + datecol) is null and
datecol is not null;
If this returns any rows, then you have bad dates in your data. Oh, it would have been better to catch these by rejecting the insert/updates in the first place. However, you might be able to figure out how to fix them.
You can also try the following way.
Select try_cast('01-' + 'Feb-18' as Date) as [String To Date]

How to return only the Date with formate from a SQL Server DateTime datatype

I know there are many questions related to this. I try to find my answer but can't able to find it.
when I write SELECT CONVERT(date, getdate()) from tblBlogs
this query its give me result like this 2018-09-10
but I want to 10, September, 2018
I hope someone helps me
If your SQL-server version higher than 2012 you can try to use FORMAT function to make it.
SELECT FORMAT(getdate(),'dd,MMMM,yyy')
sqlifddle
Or you can use DATENAME function get the name to be your expect result.
SELECT DATENAME(day,Getdate())+','+DATENAME(month,Getdate())+','+DATENAME(year,Getdate())
sqlfiddle
Result
10,September,2018
try using FORMAT
you can specify your date format whatever you want
SELECT FORMAT(getdate(),'dd,MMMM,yyy') from tblBlogs;
Personally I try to avoid FORMAT it can be awfully slow when applied against a dataset. Another method would be to use DATENAME:
DATE NAME(DAY,GET DATE()) +', ' + DATENAME(MONTH,GETDATE()) + ', ' + DATENAME(YEAR,GETDATE())
However, if it for the display format, often it's best to do that in your presentation layer, rather than the DBMS.
If your SQL Server does not support FORMAT function
SELECT CAST(DAY(#DateTime) AS VARCHAR) + ', ' + DATENAME(MONTH, #DateTime) + ', ' + CAST(YEAR(#DateTime) AS VARCHAR)

SQL Convert dd/mm/yy to yymmdd

I have been trying to achieve this all day, I have followed numerous tutorials and can't seem to crack it, I have been trying things like:
select CONVERT(VARCHAR(10), DATE, 131) from Table
yet it does not seem to change anything.
Any advice or help would be appreciated. Thankyou in advance.
SELECT CONVERT(VARCHAR(10), DATE, 12)
12 is the right code for the format you want. See: https://msdn.microsoft.com/en-GB/library/ms187928.aspx
Try this
declare #TDATE Date = '2015-11-10';
select Convert(varchar,Datepart(Year, #TDATE))+Convert(varchar,Datepart(Month, #TDATE))+Convert(varchar,Datepart(Day, #TDATE))
Output:
20151110
12 is the right code. Use below query to get output as 'yymmdd'
select CONVERT(VARCHAR(10), DATE, 12) from Table
On PostgreSQL the easiest way is to use to_char:
to_char(date, 'yyyymmdd')::int
One method is to construct the value from date parts. Here is a numeric conversion:
select (year(date) % 100) * 10000) + month(date) * 100 + day(date)
It is easy to convert this to a number:
select cast( (year(date) % 100) * 10000) + month(date) * 100 + day(date) as varchar(10))
The slight advantage to this approach over using convert is that a human being can understand the logic without perusing arcane documentation, specific to SQL Server. I don't know why SQL Server doesn't support a simple format-type function similar to most other databases (and programming languages).
select date_format(column_name, '%Y%m%d') from table_name;

How do i show just the time with AM or PM from a field that is a datetime format?

I have a column of data in SQL that is currently in the datetime format. It can be changed if needed. I need to show just the time of day, to the 10th of a second, with either AM or PM attached also. I do not want the date to be shown in this instance.
So instead of '1900-01-01 11:45:59.800' as an example, i need '11:45:59.8 AM'.
Also, this is not a current "getdate" time. It is from a field of data i have called 'Time'
I see all sorts of convert formats on the web, but none will pull this up for me.
Thank you!!!!
As in my comment, I'd advise you to not do this.
SQL Server is a place for data, and converting the data for display purposes is often a blurring of the lines that can come back to haunt you. (One example; what if you need that time as a Time somewhere else in the future. Are you going to convert it back from a string again?)
If it is Necessary, then you have to do some of the formatting yourself.
SELECT
RIGHT(CONVERT(VARCHAR(26), GETDATE(), 109), 14)
Or, more messy...
SELECT
DATEPART(HOUR, GETDATE()) + ':' +
DATEPART(MINUTE, GETDATE()) + ':' +
DATEPART(SECOND, GETDATE()) + '.' +
DATEPART(MILLISECOND, GETDATE()) +
CASE WHEN DATEPART(HOUR, GETDATE()) < 12 THEN 'AM' ELSE 'PM' END
Did I say? You're better doing it on the client side ;)
Rename your table field, Time is a reserved word and it will be a pain to maintain. Make sure you are using the new datetime2 data type if you want millisecond accuracy.
To format the time part use:
SELECT CONVERT(TIME, [Time]) FROM [Your Table]
If you only want a three digits after the period you can use:
SELECT CONVERT(TIME(3), [Time]) FROM [Your Table]
I think this will work if you want to get in HH:MM format
SELECT
RIGHT(CONVERT(VARCHAR(26), GETDATE(), 117), 14)
You can also refer to this page for more formats
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc36271.1570/html/blocks/X41864.htm

Get Time from Getdate()

I would like to take the Getdate() results of,
for example
2011-10-05 11:26:55.000
into
11:26:55 AM
I have looked other places and found
Select RIGHT(CONVERT(VARCHAR, GETDATE(), 100),7)
which gives me
11:26AM
It's so close I can taste it!
select convert(varchar(10), GETDATE(), 108)
returned 17:36:56 when I ran it a few moments ago.
You might want to check out this old thread.
If you can omit AM/PM portion and using SQL Server 2008, you should go with the approach suggested here
To get the rid from nenoseconds in time(SQL Server 2008), do as below :
SELECT CONVERT(TIME(0),GETDATE()) AS HourMinuteSecond
I hope it helps!!
Did you try to make a cast from date to time?
select cast(getdate() as time)
Reviewing the question, I saw the 'AM/PM' at end. So, my answer for this question is:
select format(getdate(), 'hh:mm:ss tt')
Run on Microsoft SQL Server 2012 and Later.
To get the format you want:
SELECT (substring(CONVERT(VARCHAR,GETDATE(),22),10,8) + ' ' +
SUBSTRING(CONVERT(VARCHAR,getdate(),22), 19,2))
Why are you pulling this from sql?
Let's try this
select convert(varchar, getdate(), 108)
Just try a few moment ago
If it's SQL Server 2005 there is no TIME datatype. The easiest way to get only the time component is to set the date to 1/1/1900.
DECLARE #time DATETIME
SET #Time = GETDATE()
SELECT DATEADD(dd,DATEDIFF(dd,#time,'1/1/1900'),#time)
You will be able to get the time using below query:
select left((convert(time(0), GETDATE ())),5)
You can use the datapart to maintain time date type and you can compare it to another time.
Check below example:
declare #fromtime time = '09:30'
declare #totime time
SET #totime=CONVERT(TIME, CONCAT(DATEPART(HOUR, GETDATE()),':', DATEPART(MINUTE, GETDATE())))
if #fromtime <= #totime
begin print 'true' end
else begin print 'no' end