format date to 105 in SQL Server after casting - sql-server-2008

I have a problem: I have a datetime and I need the date to specific format
So I just casted datetime to time
SELECT CAST (GETDATE() AS DATE) -- result (2011-06-08)
and for formatting I use convert
SELECT CONVERT(DATE, CAST (GETDATE() AS DATE), 105) --result (2011-06-08)
105 format (dd-mm-yy)
but, the result of both is same,
CONVERT is not working for 105 formatting,
Any ideas?
thanks

To get the results you're looking for you need to convert the DATE to a VARCHAR like this:
SELECT CONVERT(VARCHAR(10),CAST (GETDATE() AS DATE),105)
If you cast to a DATE, you will always get the full DATE.
You can truncate the date by re-casting to the DATE type.
SELECT CAST(CONVERT(VARCHAR(10),CAST (GETDATE() AS DATE),105) as DATE)

You can use as
SELECT Convert(varchar, getdate(), 105)

select convert(varchar(50),date,105) as Date

Related

Converting string to datetime split into date and time

I am trying to convert string '2022-12-28T22:28:43.260781049Z' to datetime format.
I have such query:
SELECT date(str_to_date('2022-12-28T22:28:43.260781049Z','%Y-%m-%d')) as date,
hour(str_to_date('2022-12-28T22:28:43.260781049Z',"%H:%M:%S")) as hour
FROM transaction
And such output:
date
time
'2022-12-28'
NULL
How to get time as well?
You can directly use a CAST on your string value to TIMESTAMP, then extract the date and the time with the hononimous DATE and TIME MySQL functions.
SELECT DATE(CAST(timestamp_ AS DATETIME)) AS date_,
TIME(CAST(timestamp_ AS DATETIME)) AS time_
FROM transactions;
Check the demo here.
Use timestamp instead of str_to_date:
SELECT hour(timestamp('2022-12-28T22:28:43.260781049Z')) as hour

Query a date from a concatenated attribute in SQL

Okay SQL gurus.....I need some help. I have an attribute ReportBatchID that is a concatenated value of the date and some other numbers. For example, all of the ReportBatchID values are in the format "201105115485452652". I need to SELECT LEFT off of the first 8 digits of that value to get the date and then select all of those dates that are more than 90 days old. I am able to use
select Left (ReportBatchID, 8) from [Table]
to pull it as a date, but when I use
where ReportBatchID < CONVERT (VARCHAR(10), DATEADD(DAY, -90, GETDATE()),121)
following my select I get the error "Error converting data type varchar to numeric." Anybody know how to do this?
When You are using Dates in where clause Always use Proper casting on both sides
where CAST(LEFT(ReportBatchID ,8) as DATE) <
CAST(DATEADD(DAY, -90, GETDATE()) as DATE)
select CAST( DATEADD(DAY, -90, GETDATE()) as DATE)
select CAST(LEFT(201105115485452652,8) as DATE)
OUTPUT
2014-08-10
2011-05-11

How to convert date in MYSQL to correct format

My current date formats are 01/01/2013 .... DD/MM/YYYY
How can I convert them all into MYSQL dates? I'm under The impression they are in the format YYYY-MM-DD
I don't know where to start.
my problem is that the dates are being ordered in the american way whilst they are in british format :(
Use the following query.
update tbl_name set your_field_name= str_to_date(your_field_name, '%d/%m/%Y');
It will update the value of your date from DD/MM/YYYY to YYYY/MM/DD.
Then you can change your filed type to date.
You can extract each part in php and concat the dd, mm, yyyy and save it to the DB
Why not DATE_FORMAT
DATE_FORMAT(date,'%Y-%m-%d')
How to convert date in MYSQL to correct format:
SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename
or
SELECT t.id, DATEDIFF(STR_TO_DATE(t.carddate, '%m/%d/%Y'), CURDATE)
FROM TABLE t
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
I have a very similar situation. I converted American date format "%Y-%d-%m" to correct format '%Y-%m-%d'. This is how I did it...
update table_name set my_date = DATE_FORMAT( STR_TO_DATE( my_date, '%Y-%d-%m' ) , '%Y-%m-%d' );
The first date string format '%Y-%d-%m' needs to be how the date is currently formatted in your table. The second date string is what you want to convert it to.

Most efficient way of flooring todays date

This seems like overkill but is the only way I have been able to floor todays datetime to 00:00:00.000 at database level:
select CAST(FLOOR(CAST(CURRENT_TIMESTAMP AS float)) AS DATETIME)
I have tried using:
select FLOOR(getdate())
But get the following message:
Implicit conversion from data type datetime to float is not allowed. Use the CONVERT function to run this query.
Can anyone recommend another way of doing this?
Since you are using SQL Server 2008 you could make use of the date data type.
declare #Today date
set #Today = getdate()
select #Today
Or without the variable.
select cast(getdate() as date)
If you need to have the value as a datetime just cast it back to a datetime.
select cast(cast(getdate() as date) as datetime)
There are a lot of ways of doing this i have seen the floor one before. Here are a few more.
select cast(cast(CURRENT_TIMESTAMP as date) as datetime)
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)
SELECT CAST(CAST(CURRENT_TIMESTAMP - 0.50000004 AS int) AS datetime)
I normaly do the Cast to date version.

MySQL Order By Query for varchar saved date

I try to order the results of a query by the date which is in the format yyyy/mm/dd and I am using this query to no avail.
SELECT * FROM table ORDER BY STR_TO_DATE(date, '%y/%m/%d')
I can't change the type of field that the date is stored in so I am hoping I can order the date post data entry.
Any help and advice appreciated.
If the date is in that format, you don't need to convert it to a date, surely? That format would sort alphabetically, assuming it is 0 padded... (i.e. July is 3 July 2012 is 2012/07/03...)
So you can just go:
select * from table order by date
What type of field is your date field: are you sure it is a varchar?
Assuming it is a varchar, you can work out what is going wrong by going:
select str_to_date(date, '%y/%m/%d') from table
You (should) get all NULL's, because the %y is wrong. Try:
select str_to_date(date, '%Y/%m/%d') from table
and it should work. But as noted, you don't have to convert to sort.
Try with capital Y:
SELECT * FROM table ORDER BY STR_TO_DATE(date, '%Y/%m/%d')
Lowercase Y is for yy, uppercase for yyyy.
SELECT * FROM table ORDER BY date