I have multiple date and time fields that I need to combine into a datetime in a SQL Query. I unfortunately cannot change the initial database structure to combine them in the original server.
I can combine the SQL Query data after export, but because of the problem of flat files conversion, and the possibility of data corruption/loss, I am hesitant as I would need to convert the flat file twice.
These are the things I've tried:
SELECT Combine = cast([DateValue] as datetime) + Cast([TimeValue] as datetime)
Unfortunately, this leads to errors where I have fewer columns than original fields....
The original Query looks like this:
SELECT
Ordered_Date.DateValue,
Ordered_TOD.TimeValue,
Start_Date.DateValue,
Start_TOD.TimeValue,
End_Date.DateValue,
End_TOD.TimeValue,
Discontinued_Date.DateValue,
Discontinued_TOD.TimeValue
FROM
warehouse_report...........
WHERE
( ..... )
If I add The Combine command into the SQL query, it seems to working in that it is combining the date/time. The issue is that when I implement this in the SELECT section of the SQL Query, I have 1 less column and it won't let me execute the script...
SELECT
Ordered_Date.DateValue,
Ordered_TOD.TimeValue,
Start_Date.DateValue,
Start_TOD.TimeValue,
COMBINE = CAST(End_Date.DateValue as datetime) + CAST(End_TOD.TimeValue as datetime),
Discontinued_Date.DateValue,
Discontinued_TOD.TimeValue
FROM
warehouse_report...........
WHERE
( ..... )
This gives me one less column on the output of the SQL Query...
How about concatenate the values and parse:
SELECT cast([DateValue]||' '||[TimeValue] as datetime)
Please, provide the information about which SGBD you are using to get a better answer.
Edit1:
To SQL Server:
Select
x.d1
From (
Select
convert(datetime, '2018-01-01 00:00:00.0', 121) d1
) x
Union All
Select
convert(datetime,
substring(convert(varchar, x.d1, 121), 1, 11)
+ substring(convert(varchar, x.t1, 121), 11, 13)
, 121)
From (
Select
convert(datetime, '2018-01-01 00:00:00.0', 121) d1,
convert(datetime, '10:00:22') t1
) x
Related
I have date stored in a varchar column in SQL Server as '19-09-2020'.
I want to convert it to '2020-09-19'.
I have tried this but it's not working:
select convert(varchar, '19-09-2020', 23)
You must specify the source format for convert:
convert(date, '19-09-2020',105)
This results in a DATE, if you actually want a VarChar again (of course, you shouldn't):
convert(varchar(10), convert(date, '19-09-2020',105), 23)
Do you want string functions?
select concat_ws('-', right(mycol, 4), substring(mycol, 4, 2), left(mycol, 2))
from mytable
On the other hand, if you want to generate a date from that string, then:
select convert(date, mycol, 105)
from mytable
I was looking to see if there was a better approach to this query
The date field is formatted - 06-03-2018
I need a column that looks like e.g. Mar-18
e.g
SELECT
CONCAT(P.[Month] + '-', p.[Year]) AS [Month-Year]
FROM
(
SELECT
left(datename(MONTH,[Date]),3) AS [Month]
,right(YEAR([Date]),2) AS [Year]
FROM
Table1
)P
This gives me the result i want but i was wondering if there was anyway to get the same result without a sub-query. Thanks
Why not just format the date as
select format(convert(date, '06-03-2018', 103), 'MMM-yy')
The query you've provided isn't valid, you need a comma (,) between column declarations (which you're missing.
Anyway, the query can be simplified to:
SELECT LEFT(DATENAME(MONTH,[Date]),3) + '-' + RIGHT(YEAR([Date]),2) AS [Month-Year]
FROM Table1;
I haven't used CONCAT here, as there is no need (as either both values will be NULL or have a value).
You Can Do the concatenation directly
SELECT
CAST(LEFT(datename(MONTH, [Date]), 3) AS VARCHAR(20))
+'-'+
CAST(RIGHT(YEAR([Date]), 2) AS VARCHAR(20))
FROM Table1;
I have an SSRS report where I am using below query.
(This query works fine in SQL server, problem is only in SSRS report)
--DECLARE #Range Number = 10;
SELECT * FROM TBL1 WHERE
USERNAME = 'MIKE'
AND
(
#Range = '10'
and
Convert(datetime, MyDate, 120) <= GETDATE()
)
or
(
#Range IN ('20','30')
and
DATEDIFF(DD, Convert(datetime, MyDate, 120) , GETDATE()) <= #Range
)
Unfortunately the myDate Column coming from database is a varchar column.
The SSRS throws an out-of-range exception.
Then I tried converting Getdate to Convert(varchar(10), getdate(), 120) and compare with myDate (without conversion as myDate is already in YYYY-MM-DD format but as a varchar in database)it still throws error. I assume this time coz SSRS is not able to process datediff in varchar columns.
When I run these queries individually, it works fine. i.e -
Declare #Range Number = 10;
Select * from tbl1
where username = 'MIKE' and
(
#Range = '10'
and
convert(datetime, MyDate, 120) <= getdate()
Has anyone faced similar issue in SSRS ???
Try this!
SELECT * FROM TBL1 WHERE
USERNAME = 'MIKE'
AND
(
#Range = '10'
and
cast(myDate as datetime) <= GETDATE()
)
or
(
#Range IN ('20','30')
and
cast(myDate as datetime) , GETDATE()) <= #Range
)
Presumably, your problem is that some values of myDate are not in the correct format. If you are using a more recent version of SQL Server, the function try_convert() can be a big help.
The reason it is failing is because of your logic. The second condition after the or is looking at all records, not just Mike's. I think you intend for the where clause to be:
SELECT *
FROM TBL1 WHERE
WHERE USERNAME = 'MIKE' AND
((#Range = '10' and Convert(datetime, MyDate, 120) <= GETDATE()
) or
(#Range IN ('20','30') and
DATEDIFF(DD, Convert(datetime, MyDate, 120) , GETDATE()) <= #Range
)
);
Note the extra set of parentheses. Now, this will probably help for this particular query for MIKE. But it won't help overall. Finding the value(s) that fail conversion can be daunting. If you are lucky, they fail easily. You can look for them with starting with:
select MyDate
from tbl1
where MyDate not like '[0-9][0-9][0-9][0-9]-[0-2][0-9]-[0-3][0-9]%';
If you are lucky, then this will find the offending values. Otherwise, you'll have to dive more deeply into the date formats, looking at the particular month and day (and potentially hour, minute, and second) values.
Moral: Store dates as dates. Don't store them as varchar().
I have this query
select CONVERT(varchar(5), tdate ,108) AS [Time] from table
which gives me the time in 24 hour format( military)
I wanted to convert it into a 12 hour format so i tried the query below
select SUBSTRING(CONVERT(VARCHAR, tdate, 100),13,2) + ':'
+ SUBSTRING(CONVERT(VARCHAR, tdate, 100),16,2) + ''
+ SUBSTRING(CONVERT(VARCHAR, tdate, 100),18,2) AS T
from table
and i get the 12 hour format but I am just curious if there is a shorter or better way of doing it. any help?
If you want to convert the current datetime for example:
SELECT CONVERT(VARCHAR, getdate(), 100) AS DateTime_In_12h_Format
Instead of getdate() you can put your desired column in a query (such as tdate in your example). If you want JUST the time in 12h and not the date and time use substring/right to separate them. It seems that you already know how to =).
This page lists every datetime conversion. It's really handy if you need other types of conversions.
This will return just the time, not the date.
SELECT RIGHT(CONVERT(VARCHAR, getdate(), 100), 7) AS time
For your table data:
select RIGHT(CONVERT(varchar, tdate ,100), 7) AS [Time] from table
Below code will return only time like 10:30 PM
SELECT FORMAT(CAST(getdate() AS DATETIME),'hh:mm tt') AS [Time]
Get date of server
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))
or
If it is stored in the table
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), datename, 100), 7))
Result:
11:41AM
ifnull(date_format(at.date_time,'%d/%m/%Y'),"") AS date_time,
ifnull(time_format(at.date_time ,'%h:%i:%s'),"") AS date_time
This is how a SQL procedure looks...(for separating date and time)..there is no need of a special column for time/date....
Note:if H instead of h it will show the "hour in 24 hour" format
I have two columns as orderdate(03/02/2011) and ordertime(10.34 am) in which i have to concatenate these two columns and show it in another column as datetime values(03/02/2011 10:34:16.190)....
any suggestion?
In general, the solution is to add days to the time value which has zero for its day portion. You are not clear as the data types of the two values, however one solution would be:
With Inputs As
(
Select '20110302' As DateVal, '10:34 AM' As TimeVal
)
Select DateAdd(d, DateDiff(d, 0, Cast(DateVal As datetime)), Cast(TimeVal as datetime))
From Inputs
A more explicit version assuming the inputs are strings and given your exact inputs:
Set DateFormat MDY
With Inputs As
(
Select '03/02/2011' As DateVal, '10:34 AM' As TimeVal
)
Select DateAdd(d, DateDiff(d, 0, Cast(DateVal As datetime)), Cast(TimeVal as datetime))
From Inputs
Assuming you are working with a DATE and a TIME, you need to convert before you can add.
SELECT [orderdate] + CONVERT(datetime, [ordertime])
you want in programatically or in sql server:
in Sql Server:
select orderdate + ordertime as 'YourColumnName'
hope this help.
In MySQL, it'll work like this
SELECT Concat(orderdate, " ", ordertime) FROM vendors ORDER BY vend_name;