How to get time (hh:mm:ss) from sql query? - sql-server-2008

I have Database with date field.
I see the time like: 1900-01-01 13:38:00.000
How i can format it like: 13:38 ?

SELECT convert(varchar, getdate(), 108) outputs as hh:mm:ss.

If you are using SQL server 2008 then,
Suppose your column name is XTime which is holding time in HH-MM-SS.ms formate. If we want to retrive time in HH-MM-SS format then write following query,
Select ID, XDate, Convert(Time(0),XTime,0) -- For HH-MM-SS format

Try this:-
RIGHT(CONVERT(VARCHAR,'DATE',100),7)
For non-military time.

I know I am late. This has already been answered by #Yes - That's Jake. It works perfect. But to help you more, I have a cheat sheet link so that you can write it in any format.
You can visit: Date and Time conversions using SQL cheat sheet
You will find a list of codes and the format that #Yes - That's Jake mentioned in his answer.

Related

How to Select Data Between Two Dates and Times in SQL Server?

why this query gives me the wrong output although I am trying to print data between '01-May-2022' and '30-May-2022';
why is this query giving me apr data?
It appears that you are storing your dates as text. You should not be doing this, and the best long term fix is to make datee a proper date column. As a short term fix, you may use STR_TO_DATE:
SELECT datee
FROM customer_shopping_details_tbl
WHERE STR_TO_DATE(datee, '%d-%b-%Y') BETWEEN '2022-05-01' AND '2022-05-30';
Thank you all ....
I did a mistake while saving the date, I had saved the date in the string format. That is the reason that the query does not show proper output
What I learned/point to remember
-MySQL date format is yyyy-mm-dd
-Save your date in the form of date NOT IN String
go through the comments for the better idea
I have saved my data in the form of String so i have fired the following query and is working now
Good evening,
Maybe this could help:
SELECT login,datetime FROM log where datetime between
to_date(’01-jan-2004,’mm/dd/yyyy’) and
to_date(’21-jan-2004′,’mm/dd/yyy’)
order by datetime DESC;

Converting date format produce by excel to SQL

Asking for any ideas to convert this kind of date in SQL from May-15-2020 18:03 to 'yyyyMMddHHmiss' or 'yyyyMMdd'.
I am trying this query
select from_unixtime(unix_timestamp('May-15-2020 16:03', 'MM-dd-yyyy
HH:mi'), 'yyyyMMdd') from dual
but it wont work.
Use the right right function STR_TO_DATE, and use a format that matches your date rather than something scraped from a previous answer/blog.
Reference manuals of date and time functions are very useful for solving these basic problems.

Calculating age in conditional split transformation

I've a requirement to split a table contents based on the age and I've a column DOB which is in string format and is of type '19910930'.I've used the following command and it works fine when run
DATEDIFF( YY,CONVERT(date,[DOB]),CONVERT(date, GETDATE()))
and the same gives an error when I try it as condition in Conditional Split transformation as
DATEDIFF( YY,CONVERT(date,[DOB]),CONVERT(date, GETDATE()))<=25
I even tried a Derived Column transformation with the same expression and that too ain't working.
Could you please help me as where I'm missing out in this?
--Vijay
try converting in this way:
DATEDIFF("YY",(DT_DATE)( SUBSTRING(#[User::DOB] ,1,4)+"-"+SUBSTRING (#[User::DOB] ,5,2)+"-"+SUBSTRING (#[User::DOB] ,7,2)) ,GETDATE())
DateDiff by year will not work when the day is between the first of the year and the [DOB]. As long as your [DOB] is in form YYYYMMDD. You can use this formula to get the age...
select
CurrentAge = ((convert(varchar(8), getdate(), 112) - cast([DOB] as int)) / 10000)
the same formula in an SSIS expression would be very clunky with a bunch of DatePart calls to format the current date [System::ContainerStartTime] into the YYYYMMDD form and a conversion of [DOB] into DT_I4. I'd advise to do the calulation in the SQL select if possible.
Thanks Jayvee and dotjoe...I couldn't get it done either way But I instead used your suggestion to calculate age as a new column in the SQL itself while generating Source and could get the required result using condition split.
DATEDIFF(YY,CONVERT(DATE,DOB),CONVERT(DATE,GETDATE())) as Age
I hope there should be a way this can be done in Derived Column though..
Vjai

Format date in mysql query that uses cast

I've got this as the select part of my query:
SELECT cast(cast(exp_channel_titles.edit_date as char(14)) as datetime) AS Edit_Date
That takes data from a db in this format 20130501092128 and returns it in this format 2013-05-01 09:21:28
I can only assume it is some kind of magic as i don't fully understand how this works tbh.
But, i need to change the format of the date that it spits out to this format: %d/%m/%Y %k:%i:%s
I can honestly say i have no idea how to do this in that query, i've tried adding it as a param to datetime (is that even a mysql function?!?) but no joy and many other poor attempts that i wont go into.
If anyone can help, i'd be hugely grateful!
MySql automatically converts 20130501092128 to a date and time field, even if it is a VARCHAR or a INT, and you can just use this:
SELECT DATE_FORMAT(exp_channel_titles.edit_date, '%d/%m/%Y %k:%i:%s')
Please see fiddle here.
You can change output format using DATE_FORMAT() function from MySQL. Here is the documentation post about it.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
You can change the output format into whatever format you want, but if you recieve that data into an application, modifies it and return that data to server (editing a row for example). Remember to reformat it into a valid date for MySQL.
If you dont know how to do it, just have to do this into your query:
SELECT DATE_FORMAT(cast(cast(exp_channel_titles.edit_date as char(14))
as datetime), '%e/%m/%Y %k:%i:%s') AS Edit_Date

Timestamps to display with AM or PM

I store articles in a mysql database with their corresponding timestamps.
I would like to format the timestamps using the date(), and display whether it's AM or PM when the article is retrieved from the database. Any help will be highly appreciated.
Assuming you are using MySQL, you need to use date_format():
SELECT DATE_FORMAT(hour_column, '%r');
This gives you a 12 hour formatted time (hh:mm:ss followed by AM or PM), anyway also have a look at the relvant bits of the MySQL Documentation
Based on some of your other questions, you use PHP.
As described on the PHP Date page, you can use a (am/pm) or A (AM/PM) to get what you want:
$showDate = date("Y-m-d h:iA", strtotime("2013-01-19 15:42:00"));
//string(18) "2013-01-19 03:42PM"