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");
Related
I am passing MySQL query using UniQuery in Delphi and it is returing NULL.
I already change the date format in the property of my TDBDateTimeEditEh to match with mysql format yyyy-mm-dd and pass this to my string variable at runtime.
Here's my code:
procedure TfrmPayroll.Button1Click(Sender: TObject);
var
DateRangeQry, StartDate, EndDate : string;
begin
StartDate := dthStart.Text;
EndDate := dthEnd.Text;
DateRangeQry := 'SELECT * FROM mytimesheet WHERE Date >= '+ StartDate +' AND date <= '+ EndDate +'';
//ShowMessage(StartDate +' to '+ EndDate); // result yyyy-mm-dd to yyyy-mm-dd
with dm_u.dmPayroll do
begin
uqMyTimesheet.SQL.Clear;
uqMyTimesheet.sql.Text := DateRangeQry;
uqMyTimesheet.ExecSQL;
cdsMyTimesheet.Refresh;
end;
end;
I did check the value in the showmessage and it is matching the date format in mysql.
I supposed my codes about would generate range of records from the database within the date range specified but no avail.
I will appreciate any help from you guys.
Updates:
Using parameters instead of OLD concatenated queries style, is better, clear and it works ever!
Try this code.
procedure TForm5.Button1Click(Sender: TObject);
var
DateRangeQry, StartDate, EndDate : string;
begin
StartDate:='01/01/2019';
EndDate:='01/01/2020';
DateRangeQry:='SELECT * FROM mytimesheet WHERE Date >= :StartDate AND date <= :EndDate';
uqMyTimesheet.SQL.Text:=DateRangeQry;
uqMyTimesheet.ParamByName('StartDate').AsDate := StrToDate(StartDate);
uqMyTimesheet.ParamByName('EndDate').AsDate := StrToDate(EndDate);
uqMyTimesheet.Open;
end;
Another approach when you have different date formats is convert date to string
StartDate:='2019-01-01';
EndDate:='2020-01-01';
DateRangeQry:='SELECT * FROM mytimesheet WHERE DATE_FORMAT(Date, ''%Y-%m-%d'') between :StartDate AND :EndDate';
UniQuery1.SQL.Text:=DateRangeQry;
UniQuery1.ParamByName('StartDate').AsString := (StartDate);
UniQuery1.ParamByName('EndDate').AsString := (EndDate);
UniQuery1.Open;
Use a parameterized query instead, using TDate instead of string for the parameter values, eg:
procedure TfrmPayroll.QueryTimeSheet(StartDate, EndDate: TDate);
begin
//ShowMessage(DateToStr(StartDate) + ' to ' + DateToStr(EndDate)); // result yyyy-mm-dd to yyyy-mm-dd
with dm_u.dmPayroll do
begin
uqMyTimesheet.SQL.Text := 'SELECT * FROM mytimesheet WHERE Date >= :PStartDate AND date <= :PEndDate';
uqMyTimesheet.ParamByName('PStartDate').AsDate := StartDate;
uqMyTimesheet.ParamByName('PEndDate').AsDate := EndDate;
uqMyTimesheet.ExecSQL;
cdsMyTimesheet.Refresh;
end;
end;
procedure TfrmPayroll.Button1Click(Sender: TObject);
begin
QueryTimeSheet(dthStart.Value, dthEnd.Value);
end;
Could someone look at my code below and let me know where I am going wrong. Trying to pass date parameters through Open Query & getting error - Unclosed quotation mark after the character string
--#PID varchar(11),
#START datetime,
#END datetime
AS BEGIN
SET NOCOUNT ON;
--DECLARE #PID1 varchar(11) = #PID
DECLARE #START1 datetime = #START
DECLARE #END1 datetime = #END
DECLARE #TSQL varchar(8000)
SET #TSQL = 'SELECT * FROM OPENQUERY ("CWSLIVE", ''SELECT * FROM pricing_base_data
WHERE date >= convert(date, ''''' + convert(varchar, #START1, 23)+ ''''', 23)
and date < convert(date, ''''' + convert(varchar, #END1, 23)+ ''''', 23))'
--PRINT (#TSQL)
EXEC (#TSQL)
END
GO
Use your query like this:- double quotes both side:
"SELECT * FROM OPENQUERY ("CWSLIVE", ''SELECT * FROM pricing_base_data
WHERE date >= convert(date, ''''' + convert(varchar, #START1, 23)+ ''''', 23) and date < convert(date, ''''' + convert(varchar, #END1, 23)+ ''''', 23))"
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 am trying to format a date and a time that comes in one column called DATE as DD/MM/YYYY (Varchar) and in another column called TIME as HH:MM:SS into one variable to insert into another column (in Datetime data type). The code below is my procedure.
DROP PROCEDURE IF EXISTS TESTProc;
DELIMITER //
CREATE PROCEDURE TESTproc()
BEGIN
DECLARE LYEAR VARCHAR(45);
DECLARE LMONTH VARCHAR(45);
DECLARE LDAY VARCHAR(45);
DECLARE LTIME VARCHAR(45);
DECLARE LDATETIME DATETIME;
SELECT TIME FROM db.test_table INTO LTIME;
SELECT SUBSTRING(DATE,6,4) FROM db.test_table INTO LYEAR;
SELECT SUBSTRING(DATE,3,2) FROM db.test_table INTO LMONTH;
SELECT SUBSTRING(DATE,1,1) FROM db.test_table INTO LDAY;
SELECT CONCAT(LYEAR,'-', LMONTH,'-','0',LDAY,' ',LTIME) INTO LDATETIME;
INSERT INTO db.test_table(VC19) VALUES (LDATETIME);
END //
Call TESTProc;
When I run the procedure, I get an error code back:
Call TESTProc; Error Code: 1292. Incorrect datetime value: '2013-31-01 16:00:40' for column 'LDATETIME' at row 2
I only have one row in db.test_table. I do not have a column in the table called 'LDATETIME', this is just my local variable. I can see from the error that my format is correct for the DateTime 'YYYY-MM-DD HH:MM:SS'.
why I am getting this error?
Update: Here is how my code looks now:
DROP PROCEDURE IF EXISTS DateProc;
DELIMITER //
CREATE PROCEDURE Dateproc()
BEGIN
DECLARE LTIME VARCHAR(45);
DECLARE LDATE VARCHAR(45);
DECLARE LDATETIME DATETIME;
SELECT TIME FROM db.date_table INTO LTIME;
SELECT DATE FROM db.date_table INTO LDATE;
IF LENGTH(LDATE) = 9 AND SUBSTRING(LDATE,2,1) = '/'
THEN SET LDATETIME = CONCAT(SUBSTRING(LDATE,6,4),'-0',SUBSTRING(LDATE,1,1),'-',SUBSTRING(LDATE,3,2), ' ', LTIME);
ELSE IF LENGTH(LDATE) = 9 AND SUBSTRING(LDATE,3,1) = '/'
THEN SET LDATETIME = CONCAT(SUBSTRING(LDATE,6,4),'-',SUBSTRING(LDATE,1,2),'-0',SUBSTRING(LDATE,4,1), ' ', LTIME);
ELSE IF LENGTH(LDATE) = 10
THEN SET LDATETIME = CONCAT(SUBSTRING(LDATE,7,4),'-',SUBSTRING(LDATE,1,2),'-',SUBSTRING(LDATE,4,2), ' ', LTIME);
ELSE IF LENGTH(LDATE) = 8
THEN SET LDATETIME = CONCAT(SUBSTRING(LDATE,5,4),'-0',SUBSTRING(LDATE,1,1),'-0',SUBSTRING(LDATE,3,1), ' ', LTIME);
END IF;
END IF;
END IF;
END IF;
INSERT INTO db.date_table(table_name) VALUES (LDATETIME);
END //
CALL DateProc;
This seems to work and accounts for any variable date that may end up in my original date column.
Look at the value:
'2013-31-01 16:00:40'
That's trying to use a month of 31.
It's not clear whether that just means your test data is wrong, or whether you need to change these lines:
SELECT SUBSTRING(DATE,3,2) FROM db.test_table INTO LMONTH;
SELECT SUBSTRING(DATE,1,1) FROM db.test_table INTO LDAY;
to:
SELECT SUBSTRING(DATE,1,2) FROM db.test_table INTO LMONTH;
SELECT SUBSTRING(DATE,4,2) FROM db.test_table INTO LDAY;
Note the change from 1 to 2 for the substring starting at 1 anyway, and the change of the second starting position from 3 to 4. You want two-digit month and day values, right? If your data format is actually D/M/YYYY (i.e. only using two digits when they're required) then you won't be able to use fixed substring positions.
Somehow your LMONTH and LDAY values seem to be reversed as LMONTH is getting written as 31 and LDAY as 01 - which obviously incorrect. Check the data within your source table.
In SQL Server, I have a stored procedure that accepts a date parameter. That date paramater is then passed along to another stored procedure. However, the 2nd stored procedure parameter is named as the first parameter. and then I need to I have 2 different scalar variables:
DECLARE #date date, #sql varchar(100);
SET #date = '2012-07-01';
SELECT #sql = ('EXEC pStoredprocedure #date = '''+#date+'''')
I need the #sql scalar to contain this text so the stored procedure can call the other stored procedure:
EXEC pStoredprocedure #date = '2012-07-01'
But, when I run my code above, I get this error:
Msg 402, Level 16, State 1, Line 4
The data types varchar and date are incompatible in the add operator.
It's almost like I need to escape the #date operator. Can this be done?
Side note: Yes, I'm trying to dynamically pass along some variables. I understand the dangers of doing so, but doing it this way is much simpler...
The date variable is being used in string concatenation, so it should be treated as a string, either through its declaration, a convert function, or a cast. I tried this:
DECLARE #date varchar(20), #sql varchar(100);
SET #date = '2012-07-01';
SELECT #sql = ('EXEC pStoredprocedure #date = ''' + #date + '''')
PRINT #sql
and got this:
EXEC pStoredprocedure #date = '2012-07-01'
I am stuck on 2005, so I don't have the date datatype, but when I tried to use datetime, I got this error:
Msg 241, Level 16, State 1, Line 4 Conversion failed when converting
datetime from character string.
You can also try
DECLARE #date datetime, #sql varchar(100);
SET #date = '2012-07-01';
SELECT #sql = ('EXEC pStoredprocedure #date = ''' + CONVERT(varchar(20), #date, 102) + '''')
PRINT #SQL;
and get
EXEC pStoredprocedure #date = '2012.07.01'