Convert to Time Format Only - sql

I hv the following table which I hv converted:-
by using the following SQL command:-
How can I convert it to this format:-
I believe it has something to do with the "convert(datetime, '7:30:00',120)".
Thanks

Use CONVERT(time,'07:30:00') instead - but you also need to come up with a more appropriate default value for the ELSE clause of your CASE expression, maybe '00:00:00'? 0 isn't a time.

Try
SELECT CONVERT(VARCHAR(8),GETDATE(),108) as TimeIn

Related

SQL: Casting text to date

I have a TEXT column of dates and need to convert them to dates, but the two methods I'm using are not working correctly. See below.
SELECT CAST("12/01/2009" as date);
12
This only returns the first digit before stoping at the '/'.
SELECT DATE("12/01/2009");
Returns nothing
I also tried CONVERT, but I'm using SQLite and it doesn't appear to support it as I'm getting a syntax error. Any suggestions on how to solve this?
Try using STR_TO_DATE function
SELECT STR_TO_DATE('12/01/2009','%m/%d/%Y');
SQL FIDDLE DEMO
SqLite doesn't have date type. You need to do string manipulation do achieve this.
SELECT CAST('2009-01-12' AS DATE);
Use it.
It returns 2014-02-28

SQL query to convert Date to another format

Thanks for your help. I am not able to make out the type/format of the "Value" in a Date column.I guess its in Julian Date format.
The Column is paid_month and the values are below.
200901
200902
So,please help in writing SQL query to convert the above values(Mostly in Julian Format) in the Date Column to normal date (MM/DD/YYYY) .
Thanks
Rohit
Hi,
I am sorry for missing in giving the whole information.
1)Its a Oracle Database.
2)The column given is Paid_Month with values 200901,200902
3)I am also confused that the above value gives month & year.Day isnt given if my guess is right.
4)If its not in Julian format ,then also please help me the SQL to get at least mm/yyyy
I am using a Oracle DB and running the query
THANKS i GOT THE ANSWER.
**Now,i have to do the reverse meaning converting a date 01/09/2010 to a String which has 6 digits.
Pls help with syntax-
select to_char(01/01/2010,**
It looks like YYYYMM - depending on your database variant, try STR_TO_DATE(paid_month, 'YYYYMM'), then format that.
Note: MM/DD/YYYY is not "normal" format - only Americans use it. The rest of the world uses DD/MM/YYYY
For MySQL check
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example:
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y')
For MySQL, you would use the STR_TO_DATE function, see http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE(paid_month,'%Y%m');
Sounds like the column contains some normal dates and some YYYYMM dates. If the goal is to update the entire column, you can attempt to isolate the YYYYMM dates and update only those. Something like:
UPDATE YourTable
SET paid_month = DATE_FORMAT(STR_TO_DATE(paid_month, '%Y%m'), '%m/%d/%Y')
WHERE LENGTH(paid_month) = 6
SELECT (paid_month % 100) + "/01/" + (paid_month/100) AS paid_day
FROM tbl;
I'm not sure about how oracle concatenates strings. Often, you see || in SQL:
SELECT foo || bar FROM ...
or functions:
SELECT cat (foo, bar) FROM ...

Netezza SQL syntax to convert numeric YYYYMMDD format into date

We have a Netezza table that contains dates stored in a numeric YYYYMMDD format (eg 20090731).
What is the best Netezza syntax to use to convert this into date format?
eg
SELECT somefunction(20090731) as NZDATE
?
Easiest way to convert number to date would be
select date(to_char(20090731,'99999999')) as Number_As_DATE;
You can use this one as it's the best one.
SELECT TO_DATE('20090731','YYYYMMDD') as NZDATE
to_date (sk_dim_time ,'YYYYMMDD')
My efforts were thwarted originally due to invalid dates. The code bellow does work as long as you wrap it in a statement to catch bad dates.
select to_date(substring(20090731 from 1 for 8),'YYYYMMDD') as NZDATE
Obviously 20090731 should be replaced with the name of the numeric variable.
select to_date(20090731,'YYYYMMDD') as Number_As_DATE
This will work without converting to char.

MS Access - Select Char as Date and doing a date diff

I have two columns. ColA and ColB contains char(10) with data "20090520" and "20090521".
I want to select and get the date difference in days. I have tried using Format() and CDate()
but MS Access always display as #ERROR.
Access prefers its dates in this format:
#2009-12-01#
You can convert your date to something Access understands with:
CDate(Format([ColA], "0000-00-00"))
Or alternatively:
DateSerial(Left([ColA],4),Mid([ColA],5,2),Right([ColA],2))
And to display the result in your preferred format:
Format(<date here>, "dd-mm-yyyy")
Try using DateSerial() to convert the dates:
DateSerial(Left([FieldName],4),Mid([FieldName],5,2),Right([FieldName],2))
If at all possible, change the datatype to a date datatype. You should not store dates as character data.
I am connecting to another database which I have no control on. That is why this problem occurred. Thanks for the feedback.

Mysql strip time component from datetime

I need to do a date comparison in Mysql without taking into account the time component i.e. i need to convert '2008-11-05 14:30:00' to '2008-11-05'
Currently i am doing this:
SELECT from_days(to_days(my_date))
Is there a proper way of doing this?
Yes, use the date function:
SELECT date(my_date)
select date(somedate) is the most common.
If you need to accommodate other formats, you can use:
SELECT DATE_FORMAT(your_date, '%Y-%m-%d');
In PostgreSQL you use the TRUNC() function, but I'm not seeing it for MySQL. From my brief Googling, it looks like you'll need to cast your DATETIME value to be just DATE.
date_col = CAST(NOW() AS DATE)
See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html
Just a simple way of doing it date("d F Y",strtotime($row['date'])) where $row['date'] comes from your query
You could use ToShortDateString();