Keep leading zero when inserting a string into a SELECT (MySql) - mysql

Here is my string (that is string NOT int):
string code = "0101";
Here is my SQL:
string select = "SELECT " + code + " AS code" ...
Here is what I get in the table:
code
101
101
101
...
Here is what I need:
code
0101
0101
...
P.S. Tried both CAST and CONVERT.

0101 is a numeric literal, and when displaying numbers leading zeros are removed. You could treat it as a string literal by surrounding it with single quotes ('):
string code = "0101";
string select = "SELECT '" + code + "' AS code" ...

Related

Split Delimited Field To Multi Fields

I have an access 2013 table that houses one field with comma separated values. I have created a second table that I need to parse the results into with a structure like so
uPPID number
value1 short text
value2 short text
value3 short text
value4 short text
I am dynamically creating the table so it will always have enough "value" fields to accommodate for the number that will be parsed out. Sample data is like such:
uppID values
aeo031 boat, goat, hoat, moat
And I would want the field mappings to go like such
uPPID = aeo031
value1 = boat
value2 = goat
value3 = hoat
value4 = moat
How can access vba parse out a csv list from one field to many?
There are probably faster/better solutions than the follwing VBA loop that inserts records one by one in the destination table. But for instance it does the job.
TableCSV is the name of the source table
TableFields is the name of the destination table
The constant maxValues specifies the number of fields values available
The query composes dynamically the INSERT INTO statement after composing the values fields; it completes it to provide all the columns, and adds the surrounding quotes '...'. (p.s. it could be simplified if we can insert without specifying all column values..)
.
Sub splitTable()
Const maxValues As Long = 4 ' <-- Set to number of value fields in destination table
Dim query As String, values As String, rs
Set rs = CurrentDb.OpenRecordset("TableCSV")
Do Until rs.EOF
values = rs!values ' next we add commas to provide all fields
values = values & String(maxValues - UBound(Split(values, ",")) - 1, ",")
values = "'" & Replace(values, ",", "','") & "'" ' 'a', 'b', '', '' etc
query = "INSERT INTO TableFields VALUES (" & rs!uPPID & "," & values & ")"
Debug.Print query
CurrentDb.Execute query
rs.moveNext
Loop
End Sub

Teradata create table auto-increment column error

I am trying to import my CSV file into Teradata using Teradata's Fastload script.
I also tried adding an auto-increment column.
This is my CSV file:
Word,country,sale,week
hi,USA,26.17,11/22/15-11/28/15
bye,USA,16.5,11/22/15-11/28/15
code snippet
String tableName = "my_db.mytable";
String createTable = "CREATE TABLE " + tableName + "," +
"NO FALLBACK," +
"NO BEFORE JOURNAL," +
"NO AFTER JOURNAL," +
"CHECKSUM = DEFAULT" +
"(" +
" id decimal(10,0) NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 NO CYCLE),"+
" word VARCHAR(500) CHARACTER SET UNICODE," +
" country VARCHAR(50)," +
" sale FLOAT," +
" week VARCHAR(30)" +
") " +
"PRIMARY INDEX (id)";
// INSERT statement
String insertTable = "INSERT INTO " + tableName + " VALUES(?,?,?,?,?)";
Error i got:
Row 1 in FastLoad table my_db.mytable_ERR_1 contains the following data:
ErrorCode=2673
ErrorFieldName=F_id
ActualDataParcelLength=55
DataParcel: byte array length 55 (0x37), offset 0 (0x0), dump length 55 (0x37)
This doesn't look like a FastLoad script, is this part of a JDBC-FastLoad?
2673 The source parcel length does not match data that was defined.
Your input data is a comma-delimited text, thus you must define all columns as VARCHAR.
And there are four columns in your input file, but you specify five in the INSERT. As the name implies a GENERATED ALWAYS sequence is automatically created.

Inserting values starting with zero in access database

I have a field called Product_Id(type string), which has length of 7 and starting with 0. But while inserting through VBA into a table field of type text the zeros is not getting inserted.
This is the insert query:
dbs.Execute "INSERT INTO tablename (PROD_NBR)VALUES (" & prodID & ");"
I think I have fixed the error - you need to declare the value in single quotes.
The PROD_NBR is a string type and field in the table is text type, then the inserting variable should be declared inside single quotes then double quotes and between two & symbols:
dbs.Execute "INSERT INTO tablename (PROD_NBR)VALUES ('" & prodID & "');"
Responding to #Cherry's answer, the following method is less tedious than a parameterized query, aka prepared statement. prodID can safely contain quotes and other special characters.
With dbs.OpenRecordset("tablename")
.AddNew
.Fields("PROD_NBR") = prodID
.Update
End With
Regarding the "starting with zero" part of your question, do you want PROD_NBR to always be 7 characters, padded with leading 0's? Then replace prodID with:
Right("0000000" & prodID, 7)

Adding underscore and jpg to the output field

The output from my Parameter is FirstName LastName.
I want the final to be FirstName_LastName.jpg. Below is what I would do in SQL but how can I do the same in my SSRS report?
update #signatures set fullname = REPLACE(REPLACE(filename, ' ', '_'),' ','.jpg')
I tried the following but the result was FirstName LastName_.jpg.
=Format(Parameters!Signature.Value, (Parameters!Signature.Value)&"")&".jpg"
=Replace(Parameters!Signature.Value, " ", "_") & ".jpg"

How do I create a query which displays dots (....) after a certain number of characters within the field

I would like to create a query on a field which after a certain number of characters adds/displays a number of dots to show the user that there is additional text to read. At the moment there is a syntax error using the following code in which it doesn't like the "Left" instruction:
X:IIF(len(description) > 5, Left(description, 5) & "....", description)
Note: "X" is what i am naming the field 'description' in my query screen in Access
You can start with Len() to determine the length of the string value stored in your field. Here is an Immediate window example which uses a variable as a stand-in for the field value:
my_field = "0123456789" ' 10 characters
? Len(my_field)
10
my_field = "012345678901234" ' 15 characters
? Len(my_field)
15
Then you can use that length in an IIf() expression. If the length of the string is greater than your cut-off number (14 in the following example), use Left() to retrieve a substring and append " ..." to that substring. Otherwise return the entire string.
' my_field is still 012345678901234 (15 characters) at this point ...
? IIf(Len(my_field) > 14, Left(my_field, 10) & " ...", my_field)
0123456789 ...
' use the same expression with the shorter string ...
my_field = "0123456789" ' 10 characters
? IIf(Len(my_field) > 14, Left(my_field, 10) & " ...", my_field)
0123456789
' longest string which avoids the "..." is 14 characters ...
my_field = "01234567890123" ' 14 characters
? IIf(Len(my_field) > 14, Left(my_field, 10) & " ...", my_field)
01234567890123
You can use that approach in your query where my_field is actually the name of a text field in your data source.
The exact syntax for this problem is:
X:IIF(len(description) > 5; Left(description; 5) & "...."; description)
Thank you Hans for leading me in the right direction