I have a column where date is stored as Sep-19,Nov-13,Aug-19 which is in varchar type i have to convert it to date type
SELECT CONVERT(Datetime, '2011-09-28 18:01:00', 120) -- to convert it to Datetime
SELECT CONVERT( VARCHAR(30), #date ,105) -- italian format [28-09-2011 18:01:00]
+ ' ' + SELECT CONVERT( VARCHAR(30), #date ,108 ) -- full date [with time/minutes/sec]
Related
I have a sample code here..
DECLARE d_progcode VARCHAR(6);
DECLARE d_header VARCHAR(30);
DECLARE d_body LONGTEXT;
DECLARE d_date VARCHAR(50);
SET d_progcode = "SAMPLE";
SET d_header = "Sample Confirmation";
SET d_body = "String 1" + DATE_FORMAT(NOW(), '%D of %M, Year %Y') + ". String 2";
INSERT INTO sample_email (`prog_code`,`sto`,`ssubject`,`sbody`,`datecreated`) VALUES (d_progcode,sto_email,d_header,d_body,NOW());
I am trying to combine string and date formatted but when I insert it in table there is an error
Truncated incorrect DOUBLE value
In mysql if you want to concatenate string values you need to use concat
SET d_body = concat("String 1" , DATE_FORMAT(NOW(), '%D of %M, Year %Y') , ". String 2");
I have a date stored as datetime2(7) and a time stored as varchar(5).
e.g.
Date = 2016-11-30 00:00:00.000000 (datetime2)
Time = 09:00 (varchar)
Output should be 2016-11-30 09:00:00.000000 (datetime).
How do I convert or cast these as a datetime. I have tried several ways but have been unsuccessful.
Thank you in advance for your help.
Maybe simple as this?
DECLARE #d DATETIME2(7)='2016-11-30 00:00:00.000000'
DECLARE #t VARCHAR(5)='09:00';
SELECT CAST(CAST(#d AS DATETIME) + CAST(#t+':00' AS DATETIME) AS datetime2(7))
Your time needs just :00 to it, than you can cast this to DATETIME. Two values of type DATETIMEcan be added.
The whole expression can be re-converted to DATETIME2.
How about something like this:
DECLARE #MYDATE DATETIME2;
DECLARE #MYTIME VARCHAR(5);
SET #MYDATE = '2016-11-30'; -- this is equal to 2016-11-30 00:00:00.000000
SET #MYTIME = '09:00';
-- for datetime2
SELECT CAST(CAST(LEFT(#MYDATE,10) AS VARCHAR) + ' ' + #MYTIME + ':00.000000' AS DATETIME2)
-- for datetime
SELECT CONVERT(DATETIME,CAST(LEFT(#MYDATE,10) AS VARCHAR) + ' ' + #MYTIME + ':00') -- for datetime
I have a stored procedure in which I need to implement a conditional WHERE clause.
I have a #name parameter, #startdate and #enddate parameters.
How can I write conditions:
if #name is null and #startdate & #enddate is given then return data for date range.
if #name is given and #startdate & #enddate is null then return data for the name
if #name and #startdate & #enddate is given then return data where name and date range are evaluated
Here is my current procedure
alter procedure dbo.sp_emp
#name varchar(50),
#startdate date,
#enddate date
as
begin
select *
from employee
where
Name = #name
and dob between #startdate and #enddate
end
I think COALESCE solves your problem.
alter procedure dbo.sp_emp
#name varchar(50),
#startdate date,
#enddate date
as
begin
select *
from employee
where Name = (COALESCE(#name, name))
AND dob between COALESCE(#startdate, dob) and COALESCE(#enddate, dob)
end
ISNULL version
alter procedure dbo.sp_emp
#name varchar(50),
#startdate date,
#enddate date
as
begin
select *
FROM employee
WHERE Name = ISNULL(#name, name)
AND dob between ISNULL(#startdate, dob) AND ISNULL(#enddate, dob)
end
You can do it this way:
DECLARE #query NVARCHAR(2048);
SET #query = 'select * from employee where Name = ''' + #name + '''';
IF #startdate IS NOT NULL AND #enddate IS NOT NULL
SET #query += ' AND dob between ''' + #startdate + ''' and ''' + #enddate '''';
EXECUTE(#query);
You should take care yours vars about SQL Injection.
Looking to convert datetime format
20150416 12:29:20:845
to
20150416 12:29:20:84.
I have tried a number of date time conversion but no luck.
DECLARE #Date varchar(22)
SET #Date = CONVERT(Varchar(23), GETDATE(),121) --replace getdate with your value
SELECT #DATE --2015-04-17 10:56:55.29 format you need
SELECT GETDATE() --2015-04-17 10:56:55.297 actual value
SELECT CAST(#DATE as datetime) --2015-04-17 10:56:55.290 if u convert it back
Assuming that you already converted your date as varchar, you can use this query:
DECLARE #V VARCHAR(30) = '20150416 12:29:20:845'
SELECT LEFT(#V,20),STUFF(#V,21,1,'.')
I have table i.e. BookingDetails. BookingDetails containt following fields.
CustomerID
DateFrom
DateTo
TimeFrom
TimeTo
BookingDetails contains n records.
CustomerID DateFrom DateTo TimeFrom TimeTo
11137 2012-08-14 2012-08-16 00:33:46 03:33:46
11138 2012-08-15 2012-08-17 08:00:00 00:31:03
11139 2012-08-16 2012-08-17 22:46:25 00:46:25
I want to select records between given information DateFrom, DateTo, TimeFrom, TimeTo.
I have do following query
declare #fDate date
set #fDate = '2012-08-14'
declare #tDate date
set #tDate = '2012-08-16'
declare #fTime time
set #fTime ='12:33:46 AM'
declare #tTime time
set #tTime='12:31:03 AM'
SELECT BookingDetails.CustomerID
FROM BookingDetails
WHERE (DateFrom between #fDate and #tDate) And (BookingDetails.DateFrom >= #fDate and BookingDetails.DateTo<=#tDate)
and(TimeFrom between CONVERT(varchar(15),cast(#fTime as time) , 108) and CONVERT(varchar(15),cast(#tTime as time) , 108))
and (TimeFrom >=CONVERT(varchar(15),cast(#fTime as time) , 108) and TimeTo <=CONVERT(varchar(15),cast(#tTime as time) , 108))
Time save in database is in 24 hours format. Time used in query is 12Hours format thats why i convert it to 24 hours format in query.
Is this query is correct or I have to change it?
This Query doesn't return any value. I want to select records between #fDate , #tDate, #fTime, #tTime
I expect Result for first two customerID i.e. 11137,11138
Why not pass the start and end as DATETIME instead of as separate values? In fact, why are you storing DATE and TIME separately when it's clear these are points in time and the two values are more important together than apart? Anyway given the current schema you need to stop converting to string. Try this:
DECLARE #b TABLE (
CustomerID INT,
DateFrom DATE,
DateTo DATE,
TimeFrom TIME,
TimeTo TIME
);
INSERT #b VALUES (11137,'2012-08-14','2012-08-16','00:33:46','03:33:46'),
(11138,'2012-08-15','2012-08-17','08:00:00','00:31:03'),
(11139,'2012-08-16','2012-08-17','22:46:25','00:46:25');
declare #fDate date
set #fDate = '2012-08-14'
declare #tDate date
set #tDate = '2012-08-16'
declare #fTime time
set #fTime ='12:33:46 AM'
declare #tTime time
set #tTime='12:31:03 AM'
;WITH x AS
(
SELECT
CustomerID, DateFrom, TimeFrom, DateTo, TimeTo,
[Start] = DATEADD(SECOND, DATEDIFF(SECOND,'00:00',TimeFrom),
CONVERT(DATETIME, DateFrom)),
[End] = DATEADD(SECOND, DATEDIFF(SECOND,'00:00',TimeTo),
CONVERT(DATETIME, DateTo))
FROM #b
)
SELECT * FROM x WHERE [Start]
BETWEEN CONVERT(DATETIME, #fDate) + CONVERT(DATETIME, #fTime)
AND CONVERT(DATETIME, #tDate) + CONVERT(DATETIME, #tTime);