Using SQL Server 2005
Table1
ID Date
001 01/12/2010
002 15/12/2010
....
DateFormat: dd/mm/yyyy
I want to change the dateformat like mm/dd/yy by using select query
Select id, date from table1
Expected Output
ID Date
001 12/01/2010
002 12/15/2010
....
How to make a query for the above format.
Need Query Help.
You can use convert to specify a mm/dd format, like 101:
select CONVERT(varchar(30), YourColumn, 101)
If your table stores dates as a string, you'd have to convert the string to a date first, like:
select CONVERT(varchar(30), CONVERT(datetime, YourColumn), 101)
Related
In a table named table there are two columns opendate(datatype-datetime) and Xdate(datatype-int). In result I need value in which the difference between opendate and Xdate should be greater than or equal to 180days.
Table:
Name
opendate
Xdate
AAA
2022-01-01 00:00:00
20210815
BBB
2022-01-02 00:00:00
20211215
Expected result :
Name
opendate
Xdate
AAA
2022-01-01 00:00:00
20210815
Since there is more than 180 days gap difference between Xdate and opendate
also I am using sql server mangement studio.
You need to convert the integer to a CHAR before you can convert to date. DATEDIFF
SELECT *
FROM Table1
WHERE DATEDIFF(day, CONVERT (DATE, CONVERT(CHAR(8), Xdate)), opendate) >= 180;
Try the below SQL script, replace the placeholder 'table' with your table name (without quotes), and syntax is for SQL Server:
SELECT * from 'table'
Where DATEDIFF (d, CONVERT (datetime,convert(char(8),xdate)),opendate) >= 180
I have a query (SQL Server 2012) that combines UNIX timestamps from two different tables into one column, which on its own works fine. The query needs to be sorted (descending) by the combined date column AND converted into a readable format.
If I attempt to sort after the date has been converted, it doesn't work as it is now a VARCHAR and returns:
05/02/2018
06/01/2017
07/03/2016
First I tried including a date conversion in the ORDER BY clause but that clearly doesn't work.
I then thought I could use a subquery to perform the sort then do the conversion in the main query (see below for what I currently have), but this is returning the error:
Invalid usage of the option NEXT in the FETCH statement.
I'm not using a fetch statement, so I'm wondering if there's something in my nesting that SQL Server doesn't like.
Any clues would be terrific.
-- Convert Epoch timestamp format to readable (and unsortable) dd/MM/yyyy format
SELECT CONVERT(VARCHAR(10), DATEADD(second, OuterTable.thedate-DATEDIFF(second, GETDATE(), GETUTCDATE()), CONVERT(DATETIME, '1970-01-01', 103)), 103) AS FinalDate
FROM (
-- Subquery only exists to do a proper date sort
SELECT thedate
FROM
(
SELECT
-- Determine which date to use
CASE WHEN x.dateX IS NOT NULL
THEN x.dateX
ELSE y.dateY
END AS thedate
-- Get date from first table
FROM (
SELECT id, dateX -- date is in Epoch format (BIGINT)
FROM tableX
) AS x
-- Get date from second table
JOIN (
SELECT id, dateY -- date is in Epoch format (BIGINT)
FROM tableY
) AS y
ON x.id = y.id
)
-- Perform sort while date is still in epoch format
ORDER BY thedate DESC
) AS OuterTable
IMO this should work for you:
SELECT CONVERT(VARCHAR(10), DATEADD(second, COALESCE(x.dateX, y.dateY) - DATEDIFF(second, GETDATE(), GETUTCDATE()), CONVERT(DATETIME, '1970-01-01', 103)), 103) AS FinalDate
FROM tableX AS x
INNER JOIN tableY AS y
ON x.id = y.id
ORDER BY COALESCE(x.dateX, y.dateY) DESC;
Not sure when copy/paste the CONVERT function. Is it so complicated to convert epoch to dd/MM/yyyy in MSSQL?
I have a string in this format: yyyyMMdd
and i want to add a Year for every date in this format.
How can i do that?
For SQL Server it should be
select
DATEADD(day, your_number_of_day, convert(datetime, your_date_column, 102))
from
your_table
i have dates stored as chars in db (i know, but its not my db nor my idea..).
One of the dates is stored as MMYYYY (012016,022016...) and the second one is stored as YYYYMMDD (20160101,20160202...).
Is there a way to compare those dates? I need to take one date and select all of the second dates which are at least one year older then the first one...
Thank you for any help !
So for example i have Date1 field with values : 012014,012015,012016
and Date2 field with value: 20141005
And i need only Date1 which is at least one year older then Date2 so in this case it will return only 012016
I make two ctes to show how to work it out. I assume MMYYYY are stored as nvarchar or varchar and YYYYMMDD are the same type. If not - you will need one/few conversions on YYYYMMDD field.
;WITH cte AS (
SELECT '012016' as MMYYYY
UNION ALL
SELECT '022016'
), cte2 AS (
SELECT '20160101' AS YYYYMMDD
UNION ALL
SELECT '20160202'
)
SELECT *
FROM cte c
INNER JOIN cte2 c2
ON YYYYMMDD LIKE RIGHT(MMYYYY,4)+LEFT(MMYYYY,2) +'%'
Output:
MMYYYY YYYYMMDD
012016 20160101
022016 20160202
EDIT:
To find out differences in years use this:
DATEDIFF(year,CAST(YYYYMMDD as date), CAST(RIGHT(MMYYYY,4)+LEFT(MMYYYY,2)+'01' as date))
You can convert both fields into type DATE, then compare them.
For MMYYYY, you can use the function DATEFROMPARTS() to build a date. For example, convert 012014 into date:
-- DATEFROMPARTS(year, month, day)
DATEFROMPARTS(RIGHT('012014', 4), LEFT('012014', 2), 1)
For YYYYMMDD, it is easier, because this is the format ISO with Time Style 112. You can use the CONVERT() function to do the conversion :
CONVERT(DATE, '20141005', 112)
And here's how the final query looks like :
WITH casted AS (
SELECT
DATEFROMPARTS(RIGHT(date1, 4), LEFT(date1, 2), 1) AS d1,
CONVERT(DATE, date2, 112) AS d2,
-- ...
FROM yourTable
)
SELECT *
FROM casted
WHERE d1 <= DATEADD(YEAR, -1, d2)
try this
column1 is stored MMYYYY format and column2 is stored YYYYMMDD.
where column1= case when datepart(month,taskDuedate)>9 then convert(varchar,datepart(month,taskDuedate))else '0'+convert(varchar,datepart(month,taskDuedate))end+convert(varchar,datepart(year,taskDueDate))
I have a column that stores a date as char in the format 'YYYYMMDD'. Now I want to convert it to a real date.
I tried
select cast (DATEFIELD as DATE) as MyDate
But it only returns the old YYYYMMDD format labeled as 'DATE'. What am I doing wrong?
SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as "MyDate"
Use the CONVERT function and the style 112 to get the output in YYYYMMDD
SELECT CONVERT(DATEFIELD, 112) as MyDate
FROM yourtable
Or style 100 for mon dd yyyy hh:mi
SELECT CONVERT(DATEFIELD, 100) as MyDate
FROM yourtable
Simply convert it.
SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as MyDate
FROM <your_table>